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的更多相关文章

  1. 远程连接Kali Linux使用PuTTY实现SSH远程连接

    远程连接Kali Linux使用PuTTY实现SSH远程连接 本书主要以在Android设备上安装的Kali Linux操作系统为主,介绍基于Bash Shell渗透测试.由于在默认情况下,在Andr ...

  2. SecureCRT 连接虚拟机Linux

    SecureCRT 连接虚拟机Linux   最近在学习linux,在学习中遇到了一些问题,现总结一下. 虚拟机我用的是VirtualBox,完美支持中文,可以在电脑中创建虚拟机环境,上手非常简单.具 ...

  3. 其他综合-使用Xshell远程连接管理Linux实践

    使用Xshell远程连接管理Linux实践 1. Xshell整体优化 1)点击 工具 ,然后选择 选项 2)在 常规 选项中,下面的存放路径根据个人爱好修改(可选默认) 3)在 更新 选项中,将 √ ...

  4. 其它综合-使用Putty远程连接管理Linux实践

    使用Putty远程连接管理Linux实践 1.获取putty 获取 putty有很多方法,以下是我为大家提供的下载地址: 个人网盘地址,提取码:tz83 官方下载地址 解释: 官方下载的是 zip 压 ...

  5. java使用Jsch实现远程操作linux服务器进行文件上传、下载,删除和显示目录信息

    1.java使用Jsch实现远程操作linux服务器进行文件上传.下载,删除和显示目录信息. 参考链接:https://www.cnblogs.com/longyg/archive/2012/06/2 ...

  6. plsql 连接 虚拟机Linux下的Oracle数据库 失败

    plsql 连接 虚拟机Linux下的Oracle数据库 失败2017年09月09日 09:10:23 守望dfdfdf 阅读数:187 标签: oraclelinux 更多个人分类: 工具 软件编辑 ...

  7. Linux课程---3、Linux远程登录和传输(操作Linux服务器软件)

    Linux课程---3.Linux远程登录和传输(操作Linux服务器软件) 一.总结 一句话总结: xshell:Xshell是一个强大的安全终端模拟软件 Xshell是一个强大的安全终端模拟软件, ...

  8. 如何用 putty 连接远程 Linux 系统

    如何用 putty 连接远程 Linux 系统 Putty 简介 Putty 是一个免费的.Windows x86 平台下的 Telnet.SSH 和 Rlogin 客户端,但是功能丝毫不逊色于商业的 ...

  9. 保持SSH连接的linux服务器不断线

    目录 1. secureCRT和putty连接设置 2. 命令行设置 3. 服务器配置修改 4. 参考 使用ssh连接远程服务器的时候,如果长时间没有操作,远程连接就有可能中断.正在执行的程序和vim ...

随机推荐

  1. meterpreter lhost设置

    如果要监听kali本地的话,设置 0.0.0.0

  2. ipa包兼容性大作战!WeTest iOS深度兼容测试全新升级

    2018年,移动端适配话题热闹无比,有iOS新版本新机型发布,全面屏.异形屏.曲面屏争相斗艳,从而产生了各类特殊的屏幕分辨率设备. 正是因为这些特殊分辨率,导致2018年手机设备频繁出现适配问题,如屏 ...

  3. css布局笔记(三)圣杯布局,双飞翼布局

    圣杯布局和双飞翼布局都是三列布局,两边定宽,中间自适应布局,中间栏要在放在文档流前面以优先渲染. 圣杯布局如下 <!-- 圣杯布局 --> <!DOCTYPE html> &l ...

  4. JUC——线程池

    线程池本质的概念就是一堆线程一起完成一件事情. Executor package java.util.concurrent; public interface Executor { void exec ...

  5. 006 --MySQL索引原理

    一 .索引的概念? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句的优化 ...

  6. mysql以zip安装,解决the service already exists(转载)

    喵喵亲测可用: 转自:https://www.cnblogs.com/dichters/p/5929209.html mysql以zip安装, mysqld -install 报错:​The serv ...

  7. GIT问题(二)——add报错

  8. Workbook对象的方法总结(一)

    import openpyxlwb=openpyxl.Workbook()print('1.添加前所有工作簿的名称是:',wb.get_sheet_names())wb.create_sheet('F ...

  9. hadoop之计数器和管道的mrunit测试

    引言 hadoop的调试真心让人灰常恼火,而且从企业实际出发,集群的资源是有限的,不可能在集群上跑一遍又一遍根据log去调试代码,那么使用MRUnit编写测试单元,显得尤为重要.MRUnit中的Map ...

  10. VMware启动Centos时出现错误Cannot open the disk 'xxxxxxx.vmdk' or one of the snapshot disks it depends on. .

    今天拔装虚拟机的硬盘的时候,没有关掉虚拟机,导致虚拟打开的时候出现:Cannot open the disk 'xxxxxxx.vmdk' or one of the snapshot disks i ...