using System;
using System.IO;
using Renci.SshNet;
using Renci.SshNet.Sftp;
namespace weiCmd
{
public class WeiSSH
{
public static string _host;
public static string _username;
public static string _password;
public static int _port = 22;
public static string _command;
public static string _serverdir = "/tmp/";
public static string _clientdir = @"D:\";
public static string _filename = "测试表.csv";
public static ConnectionInfo WeiCreateConnectionInfo(string _host, int _port, string _username)
{
//安装凭据和服务器信息
ConnectionInfo WeiConnInfo = new ConnectionInfo(_host, _port, _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")
//}),
}
);
return WeiConnInfo;
}
/// <summary>
/// 不依赖ConnectionInfo直接try连接并执行命令
/// </summary>
public void WeiStart()
{
using (var client = new SshClient(_host, _username, _password))
{
try
{
string command = _command;
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 WeiExecuteCommand()
{
var WeiConnInfo = WeiCreateConnectionInfo(_host, _port, _username);
using (var sshclient = new SshClient(WeiConnInfo))
{
sshclient.Connect();
using (var cmd = sshclient.CreateCommand(_command))
{
string result = cmd.Execute();
Console.WriteLine("Command>" + cmd.CommandText);
Console.WriteLine("Return Value = {0}", cmd.ExitStatus);
Console.WriteLine("result>" + result);
}
sshclient.Disconnect();
}
}
/// <summary>
/// 上传
/// </summary>
public static void WeiUpload()
{
var WeiConnInfo = WeiCreateConnectionInfo(_host, _port, _username);
using (var sftp = new SftpClient(WeiConnInfo))
{
string uploadfn = _clientdir + _filename;
sftp.Connect();
sftp.ChangeDirectory(_serverdir);
using (var uplfileStream = System.IO.File.OpenRead(uploadfn))
{
sftp.UploadFile(uplfileStream, _filename, true);
}
sftp.Disconnect();
}
}
/// <summary>
/// 下载
/// </summary>
public static void WeiDownload()
{
var WeiConnInfo = WeiCreateConnectionInfo(_host, _port, _username);
using (var sftp = new SftpClient(WeiConnInfo))
{
string downloadfn = _serverdir + _filename;
sftp.Connect();
string destinationPath = string.Format(@"{0}\{1}", _clientdir, _filename);
//读取server的SftpFileStream文件流
using (SftpFileStream sftpStream = sftp.OpenRead(downloadfn))
{
if (sftpStream.Length > 0)
{
//定义写入本地的FileStream文件流
FileStream fileStream = File.Create(destinationPath, (int)sftpStream.Length);
//定义buffer大小200K
byte[] buffer = new byte[200 * 1024];
int count;
//每次读取的buffer是200K,读取的偏移量是0,实际读取的长度
//每次吧实际读取的长度赋值给count
//如果count<=0,终止读取
//同时终止写入
//偏移量0说明每次流读或流写的时候,读过或写过的就自动默认跳过了
while ((count = sftpStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, count);
}
}
}
}
}
}
/// <summary>
/// WeiSSH类使用示例
/// </summary>
public class WeiSSHUse
{
public static void WeiSSHFuncUse()
{
WeiSSH._host = "1.1.1.8";
WeiSSH._port = 22;
WeiSSH._username = "root";
WeiSSH._password = "830";
WeiSSH._command = "cd /tmp/; ls -lrt";
WeiSSH.WeiExecuteCommand();
WeiSSH._serverdir = "/tmp/";
WeiSSH._clientdir = @"D:\";
WeiSSH._filename = "测试表.csv";
WeiSSH.WeiUpload();
WeiSSH._command = "mv /tmp/测试表.csv /tmp/测试表linux.csv";
WeiSSH.WeiExecuteCommand();
WeiSSH._serverdir = "/tmp/";
WeiSSH._clientdir = @"D:\";
WeiSSH._filename = "测试表linux.csv";
WeiSSH.WeiDownload();
}
}
}
大伟C# SSH类
最新推荐文章于 2024-06-14 13:03:29 发布