C# sshClient 必须先登陆一层,才能进入下一层,进入abc用户后,执行runcommand后无反应
private static void GetSshClient()
{
string conntCommand = "su - abc";
string conntPasdCommand = "abc";
string remoteFirstCommand = "cd xxx/xx/";
string remoteSecordCommand = "cd ~";
string remoteThirdCommand = "cd /xxx/xxx/";
string CopyCommand = "cp -rp test.dat /xx/xx/";
string DeleteCommand = "rm test.dat";
List<FileInfo> lstEffectedFile = new List<FileInfo>();
SshClient sshClient = GetSSHClient();
try
{
sshClient.Connect();
if (sshClient.IsConnected)
{
IDictionary<Renci.SshNet.Common.TerminalModes, uint> termkvp = new Dictionary<Renci.SshNet.Common.TerminalModes, uint>();
termkvp.Add(Renci.SshNet.Common.TerminalModes.ECHO, 53);
ShellStream shellStream = sshClient.CreateShellStream(sTSName, 80, 24, 800, 600, 1024);
string rep = shellStream.Expect(new Regex(@"[$>]")); //Get logged in expect user prompt
sshClient.RunCommand(conntCommand);
if (rep.Contains(":"))//check to send password
{
shellStream.WriteLine(conntPasdCommand);//send password
rep = shellStream.Expect(new Regex(@"[$#>]")); //expect user,root or other prompt
sshClient.CreateCommand(remoteFirstCommand);
sshClient.RunCommand(remoteSecordCommand);
sshClient.RunCommand(remoteThirdCommand);
sshClient.RunCommand(CopyCommand);
sshClient.RunCommand(DeleteCommand);
GetDatFiles();
}
sshClient.Disconnect();
sshClient.Dispose();
}
else
{
RecordLog("Lost connection with server.");
}
}
catch (Exception ex)
{
RecordLog("Parsing error!!");
}
}