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 ...
随机推荐
- CentOS7.4 部署 Django + Python3 + Apache + Mod_wsgi
安装环境 Remote: CentOS 7.4 x64 (django.example.com) Python: Python3.6.5 Apache: Apache 2.4.6 Mod_wsgi: ...
- TCP/IP 协议簇 端口 三次握手 四次挥手 11种状态集
第1章 概念介绍 1.1 VLAN 1.1.1 什么是VLAN VLAN(Virtual LAN),翻译成中文是“虚拟局域网”.LAN可以是由少数几台家用计算机构成的网络,也可以是数以百计的计算机构成 ...
- springmvc框架开发中解决产生的乱码情况
一:解决post请求方式产生的乱码情况 示例代码如下: <!-- 解决post乱码 --> <filter> <filter-name>CharacterEncod ...
- 用xpo实现dc技术的关键点-XPO是如何处理接口类型与真实类型的对应关系的
https://www.devexpress.com/Support/Center/Question/Details/Q487000/xpodatamodel-and-model-interfaces ...
- 用人工智能学习,凡亿推出PCB问题解答智能搜索机器人:pcb助手
对于学习者,你是不是经常遇到这样的问题:在我们狠狠下定决心学习PCB技术的时候,我们常常遇到很多大大小小的问题,遗憾的是身边没有一个能及时给自己解答问题的高手指点,通过论坛.群等方式询问可能半天也得不 ...
- Mysql启动失败解决方案 - 个人经验可能不适合所有场景
以前一直用的Mysql5.5,安装程序是一个exe程序,安装完了相应的服务也给我注册好了,然后直接启动连接即可. 最近升级到了8.0.15,发现和以前不一样了. 8.0.15下载地址 安装解压之后目录 ...
- 2.深入解析数据类型与变量——《Excel VBA 程序开发自学宝典》
2.1 数据类型 数据类型 所占字节 Byte 1 Boolean 2 Integer 2 Long 4 Single 4 Double 8 Currency 8 Decimal 14 Date 8 ...
- 基于Ubuntu+kodexplorer可道云的私有云网盘
1.可用的服务器:组装PC机一台,操作系统为Ubuntu 14.04 LTS,无桌面环境,放在机房,使用远程终端进行访问.有安装了Apache2,运行着svn服务.内网IP地址为192.168.0.1 ...
- KClient——kafka消息中间件源码解读
目录 kclient消息中间件 kclient-processor top.ninwoo.kclient.app.KClientApplication top.ninwoo.kclient.app.K ...
- 阿里云解析记录应对家里动态IP
<?php #需要配置的项 define('ACCESSKEYID',''); #阿里云用户密钥ID 获取方法 https://help.aliyun.com/knowledge_detail/ ...