c# 使用ssh连接远程主机(ssh.net演示)
本教程使用的是ssh.net这个库。项目地址:https://github.com/sshnet/SSH.NET
使用ssh客户端连接远程主机执行命令,并拿到输出结果:
using (var sshClient = new SshClient("host", port,"username", "password"))
{
sshClient.Connect();
using (var cmd = sshClient.CreateCommand("ls -l"))
{
var res = cmd.Execute();
Console.Write(res);
}
}
使用sftp客户端上传文件:
using (var sftpClient = new SftpClient("host", port,"username", "password"))
{
sftpClient.Connect();
sftpClient.UploadFile(File.Open(@"D:\index.html", FileMode.Open),"/root/index.html");
}
下载文件:
using (var sftpClient = new SftpClient("host", port,"username", "password"))
{
sftpClient.Connect();
using (var stream = File.Open(@"F:\index.html", FileMode.OpenOrCreate))
{
sftpClient.DownloadFile("/root/index.html", stream);
}
}
c# 使用ssh连接远程主机(ssh.net演示)的更多相关文章
- ssh连接远程主机免密登入
核心思想: 1.本地主机生成公钥私钥,私钥自己存着,公钥传到远程主机.ssh文件夹下authorized_keys文件(默认是这个,用追加的方式) 2.本地连接远程主机,公私钥对上就可以免密登入了. ...
- 使用ssh连接远程主机
在linux系统中,ssh是远程登录的默认工具,因为该工具的协议使用了RSA/DSA的加密算法.该工具做linux系统的远程管理是非常安全的. ssh登录远程主机(服务器)一般有两种方式:无密钥方式 ...
- 强制断开ssh连接出现ssh崩溃问题
出现原因 finalshell意外终止,导致ssh连接意外终止 之后怎么都连不上虚拟机的ssh,一看是虚拟机的ssh已经被意外暂停,可能是跟finalshell的意外终止有关 解决 chmod 600 ...
- 解决ssh连接超时(ssh timeout)的方法
echo export TMOUT=1000000 >> /root/.bash_profile (可设置为-1为永不超时) cat /root/.bash_profile source ...
- [ 转载 ] ssh连接远程主机执行脚本的环境变量问题
近日在使用ssh命令ssh user@remote ~/myscript.sh登陆到远程机器remote上执行脚本时,遇到一个奇怪的问题: ~/myscript.sh: line n: app: co ...
- ssh连接远程主机执行脚本的环境变量问题
近日在使用ssh命令ssh user@remote ~/myscript.sh登陆到远程机器remote上执行脚本时,遇到一个奇怪的问题: ~/myscript.sh: line n: app: co ...
- Linux中网卡相关命令以及SSH连接远程主机
ifconfig 命令 查看与配置网络状态命令 关闭与启动网卡 ifdown 网卡设备名 禁用该网卡设备 ifup 网卡设备名 启用该网卡设备 查询网络状态 netstat 选项 -t 列出TCP协议 ...
- SSH登录远程主机执行脚本找不到环境变量
这是因为在Linux上,bash会有四种模式,根据不同的case,Linux会加载不同模式的bash.一般如果你自己直接登录主机,能看到环境变量,但是使用ssh 远程登录执行脚本就找不到环境变量,那么 ...
- Linux - ssh 连接问题
SSH 连接方式 ssh -p 22 user@192.168.1.209 # 从linux ssh登录另一台linux ssh -p 22 root@192.168.1.209 CMD # 利用ss ...
随机推荐
- [Java] Java常见错误
1.处理java错误"编码 GBK 的不可映射字符" (1)首先记事本打开java源文件 (2)然后另存为,选择ANSI编码 (3)覆盖 (4)再试一下,ok,编译通过.
- hdu 3486 Interviewe (RMQ+二分)
Interviewe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- PowerShell收发TCP消息包
PowerShell收发TCP消息包 https://www.cnblogs.com/fuhj02/archive/2012/10/16/2725609.html 在上篇文章中,我们在PSNet包中创 ...
- [洛谷P1420]最长连号
题目大意:输入$n$个正整数,($1\leq n\leq 10000$),要求输出最长的连号的长度.(连号指从小到大连续自然数) 题解:考虑从小到大连续自然数差分为$1$,所以可以把原数列差分(后缀自 ...
- [ZJOI2006]物流运输 DP 最短路
---题面--- 题解: 设f[i]表示到第i天的代价,cost[i][j]表示第i天到第j天采取同一种方案的最小代价.那么转移就很明显了,直接$n^2$枚举即可. 所以问题就变成了怎么获取cost数 ...
- WM_CTLCOLOR消息
文章参考地址:http://blog.csdn.net/hisinwang/article/details/8070393 在每个控件开始绘制之前,都会向其父窗口发送WM_CTLCOL ...
- [Leetcode] Balanced binary tree平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【HDU 4300 Clairewd’s message】
Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important ...
- Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) C
C. Little Artem and Matrix time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- HDU2571--命运---DP
http://acm.hdu.edu.cn/showproblem.php?pid=2571 #include "iostream" #include "cstdio&q ...