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 ...
随机推荐
- jquery 直接访问图片路径
jQuery("#img").attr("src",function(){return this.src+"?"});
- Object C学习笔记6-如何在Windows环境搭建Object C开发环境
1. 安装编译环境 Object C和其他很多语言一样,都需要有一个编译器.Object C 是在GCC下编译的.GCC(GNU Compiler Collection,GNU编译器集合),是一套由 ...
- jvm系列 (一) ---jvm内存区域与溢出
jvm内存区域与溢出 目录 jvm系列(一):jvm内存区域与溢出 jvm系列(二):垃圾收集器与内存分配策略 为什么学习jvm 木板原理,最短的一块板决定一个水的深度,当一个系统垃圾收集成为瓶颈的时 ...
- 【PaPaPa】实现缓存决策 - 让你的缓存变的有智慧
我有话说 本来这一篇我打算放到后面再说,可是之前泄漏了一点关于缓存决策的代码后被好多人催更了. 在此感谢大家的支持,让我更有动力的写这个系列.你们的关注让我觉得我的决定是对的,我会坚持下去把这个项目做 ...
- css学习杂记
1.css中的&语法 &是sass的语法,代表上一级选择器. 例如: .el-row { margin-bottom: 20px; &:last-child { margin- ...
- php快速上手总结
PHP作为现代热门主流的开发语言,对于那些想加入新手PHPer,从哪学起,如何学习?你必须要需要掌握PHP的基础知识,基础知识相当于重点,是不可忽视的知识.常用的功能模块,面向对象的,MVC等相关技能 ...
- Flink架构分析之RPC详解
主要抽象 Flink RPC 框架主要抽象了RpcService,RpcEndpoint,RpcGateway,RpcServer这几个接口,具体实现可以采用多种方式,比如:akka,netty Rp ...
- easyui的tab标签链接aspx页面引发全局刷新的问题解决方案
通过tree组件和tabs组件结合加载子页面窗体aspx,点击按钮页面全部重新加载,或整个跳到子窗体页面,解决方案:换一种结合iframe的方式做系统界面:在tree组件出替换掉设置href属性处为下 ...
- smash:一个类unix内核
前言 每一个蹩脚的C++程序员都有一颗做操作系统内核的心.我从大学毕业开始就对操作系统内核感兴趣,将其看作是术之尽头,可惜那时候一直在无忧无虑的忙着玩网游,也就搁置了.随着时间的推移,逐渐就将其淡忘了 ...
- java按照字节切割字符串,解决汉字的问题
编写一个截取字符串的函数,输入为一个字符串,截取开始地址,截取字节数,输出为按字节截取的字符串. 但是要保证汉字不被截半个, 如“我ABC”,0,4,应该截为“我AB”,输入“我ABC汉DEF”,1, ...