c# 连接操作linux
0.背景
现在linux重要性是显然易见的,学习linux是必须,通过程序来来控制linux 也能发挥很大的作用。比如我们可以做一个自动化部署的程序,来发布程序到linux上面。
1.在项目中添加SSH.NET

2.如何使用SSH.NET 直接上来代码
private static string host = "xxx.xxx.xxx.xx";
private static string username = "root";
private static string password = "pwd"; // Setup Credentials and Server Information
public static ConnectionInfo ConnNfo = new ConnectionInfo(host, , username,
new AuthenticationMethod[]{
// Pasword based Authentication
new PasswordAuthenticationMethod(username,password),
// Key Based Authentication (using keys in OpenSSH Format)
//new PrivateKeyAuthenticationMethod("username",new PrivateKeyFile[]{
// new PrivateKeyFile(@"..\openssh.key","passphrase")
//}),
}
); static void Main(string[] args)
{ ExecuteCommand1();
ExecuteCommand2();
Upload();
Console.ReadLine();
} /// <summary>
/// 开始
/// </summary>
public void Start()
{
using (var client = new SshClient(host, username, password))
{
try
{
string command = "ls";
client.Connect();
string result = client.RunCommand(command).Execute();
Console.WriteLine(result);
client.Disconnect();
}
catch (Exception e1)
{
Console.WriteLine(e1.Message);
}
}
} /// <summary>
/// 执行 command
/// </summary>
public static void ExecuteCommand1()
{
using (var sshclient = new SshClient(ConnNfo))
{
sshclient.Connect();
using (var cmd = sshclient.CreateCommand("cd /local && ls"))
{
string result = cmd.Execute();
Console.WriteLine("Command>" + cmd.CommandText);
Console.WriteLine("Return Value = {0}", cmd.ExitStatus);
Console.WriteLine("result>" + result);
}
sshclient.Disconnect();
}
} /// <summary>
/// 执行 command
/// </summary>
public static void ExecuteCommand2()
{
// Execute (SHELL) Commands
using (var sshclient = new SshClient(ConnNfo))
{
sshclient.Connect();
// quick way to use ist, but not best practice - SshCommand is not Disposed, ExitStatus not checked...
Console.WriteLine(sshclient.CreateCommand("cd /local && ls -lah").Execute());
Console.WriteLine(sshclient.CreateCommand("pwd").Execute());
Console.WriteLine(sshclient.CreateCommand("cd /local/soft && ls -lah").Execute());
sshclient.Disconnect();
}
} /// <summary>
/// 上传
/// </summary>
public static void Upload()
{
using (var sftp = new SftpClient(ConnNfo))
{
string uploadfn = "Renci.SshNet.dll"; sftp.Connect();
sftp.ChangeDirectory("/local/soft");
using (var uplfileStream = System.IO.File.OpenRead(uploadfn))
{
sftp.UploadFile(uplfileStream, uploadfn, true);
}
sftp.Disconnect();
}
}
c# 连接操作linux的更多相关文章
- 远程连接Kali Linux使用PuTTY实现SSH远程连接
远程连接Kali Linux使用PuTTY实现SSH远程连接 本书主要以在Android设备上安装的Kali Linux操作系统为主,介绍基于Bash Shell渗透测试.由于在默认情况下,在Andr ...
- SecureCRT 连接虚拟机Linux
SecureCRT 连接虚拟机Linux 最近在学习linux,在学习中遇到了一些问题,现总结一下. 虚拟机我用的是VirtualBox,完美支持中文,可以在电脑中创建虚拟机环境,上手非常简单.具 ...
- 其他综合-使用Xshell远程连接管理Linux实践
使用Xshell远程连接管理Linux实践 1. Xshell整体优化 1)点击 工具 ,然后选择 选项 2)在 常规 选项中,下面的存放路径根据个人爱好修改(可选默认) 3)在 更新 选项中,将 √ ...
- 其它综合-使用Putty远程连接管理Linux实践
使用Putty远程连接管理Linux实践 1.获取putty 获取 putty有很多方法,以下是我为大家提供的下载地址: 个人网盘地址,提取码:tz83 官方下载地址 解释: 官方下载的是 zip 压 ...
- java使用Jsch实现远程操作linux服务器进行文件上传、下载,删除和显示目录信息
1.java使用Jsch实现远程操作linux服务器进行文件上传.下载,删除和显示目录信息. 参考链接:https://www.cnblogs.com/longyg/archive/2012/06/2 ...
- plsql 连接 虚拟机Linux下的Oracle数据库 失败
plsql 连接 虚拟机Linux下的Oracle数据库 失败2017年09月09日 09:10:23 守望dfdfdf 阅读数:187 标签: oraclelinux 更多个人分类: 工具 软件编辑 ...
- Linux课程---3、Linux远程登录和传输(操作Linux服务器软件)
Linux课程---3.Linux远程登录和传输(操作Linux服务器软件) 一.总结 一句话总结: xshell:Xshell是一个强大的安全终端模拟软件 Xshell是一个强大的安全终端模拟软件, ...
- 如何用 putty 连接远程 Linux 系统
如何用 putty 连接远程 Linux 系统 Putty 简介 Putty 是一个免费的.Windows x86 平台下的 Telnet.SSH 和 Rlogin 客户端,但是功能丝毫不逊色于商业的 ...
- 保持SSH连接的linux服务器不断线
目录 1. secureCRT和putty连接设置 2. 命令行设置 3. 服务器配置修改 4. 参考 使用ssh连接远程服务器的时候,如果长时间没有操作,远程连接就有可能中断.正在执行的程序和vim ...
随机推荐
- doc2vec使用笔记
#!/usr/bin/env Python # coding:utf-8 #improt依赖包 # import sys # reload(sys) # sys.setdefaultencoding( ...
- 传统路由和OVS区别
本文主要描述了一种将三层路由变成二层交换转发(以及二层转发变成三层路由)的实现方式,以应对OVS(OpenFlow)跨网段路由复杂的问题:当然技术本身是客观的,具体应用还要看场景. 随着SDN技术不断 ...
- Dbzoj#3188. [Coci 2011]Upit
写道数据结构练练手哈哈哈 // It is made by XZZ #include<cstdio> #include<algorithm> #include<cstdl ...
- 如何把项目通过git上传之github完整教程
作为一个有追求的程序员,需要撸点自己的开源项目,虽然我现在只是在学着造轮子,但这并不影响我成为大神的心.Github是基于git实现的代码托管,很多程序员在上面托管自己的开源项目,我使用Github也 ...
- PSR编码规范
PSR-1 代码风格规范(1)常量命名:类中的常量所有字母都必须大写,单词间用下划线分隔(2)类命名:类的命名必须遵循 StudlyCaps 大写开头的驼峰命名规范(3)方法命名:方法名称必须符合 c ...
- Python之NMAP详解
一.NMAP简介 NMap,也就是Network Mapper,最早是Linux下的网络扫描和嗅探工具包. nmap是一个网络连接端扫描软件,用来扫描网上电脑开放的网络连接端.确定哪些服务运行在哪些连 ...
- 从Web抓取信息的几个常用方法
1.Response 对象有一个 status_code 属性,可以检查它是否等于requests.codes.ok. 2.raise_for_status()方法是一种很好的方式,确保程序在下载失败 ...
- IOS上z-index和fixed定位无效
IOS上z-index和fixed定位无效 在该元素上加: -webkit-transform:translateZ(1px); -moz-transform:translateZ(1px); -o- ...
- 常用DB2命令
建库 db2 territory CN on 建库到指定位置 db2 create database OADB on D: using codeset GBK territory CN 列出所有数据库 ...
- BugPhobia开发篇章:Scurm Meeting-更新至0x02
0x01 :目录与摘要 If you weeped for the missing sunset, you would miss all the shining stars 索引 提纲 整理与更新记录 ...