我使用Plink将ssh与C#连接。但是当我将命令发送到服务器时,Plink会在登录之前发送命令。因此,某些命令不起作用。
我从外面发送命令,这些代码是,
cd /ssh/fd/
./myscript.sh
您可以在下面的示例代码中找到;
void connect()
{
ProcessStartInfo psi = new ProcessStartInfo()
{
FileName = Application.StartupPath + @"\_Components\plink.exe",
Arguments = String.Format("-ssh -hostkey {3} {0}@{1} -pw {2}", Users, Host, Password, Hostkey),
RedirectStandardError = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true
};
Process p = Process.Start(psi);
m_objLock = new Object();
m_blnDoRead = true;
AsyncReadFeedback(p.StandardOutput);
AsyncReadFeedback(p.StandardError);
StreamWriter strw = p.StandardInput;
string[] lstCommands = commandList.ToArray();
foreach (string cmd in lstCommands)
{
strw.WriteLine(cmd);
}
strw.WriteLine("exit");
p.WaitForExit();
return m_szFeedback;
}
private String m_szFeedback;
private Object m_objLock;
private Boolean m_blnDoRead;
public void AsyncReadFeedback(StreamReader strr)
{
Thread trdr = new Thread(new ParameterizedThreadStart(__ctReadFeedback));
trdr.Start(strr);
}
private void __ctReadFeedback(Object objStreamReader)
{
StreamReader strr = (StreamReader)objStreamReader;
string line;
while (!strr.EndOfStream && m_blnDoRead)
{
line = strr.ReadLine();
lock (m_objLock) { m_szFeedback += line + "\r\n"; }
}
}
这是输出:
Feedback from: xxx.xx.xxx.xx
cd /ssh/fd/
Last successful login: Thu Nov 19 12:45:42 EET 2015 xxxxxxxxxxxxx
Last authentication failure: Fri Dec 18 19:40:55 EET 2015 xxxxxxxxxxxxxxx
Last login: Thu Nov 19 12:45:42 2015 from xxxxxxxxxxx
(c)Copyright 1983-2006 Hewlett-Packard Development Company, L.P.
(c)Copyright 1979, 1980, 1983, 1985-1993 The Regents of the Univ. of California
(c)Copyright 1980, 1984, 1986 Novell, Inc.
(c)Copyright 1986-2000 Sun Microsystems, Inc.
(c)Copyright 1985, 1986, 1988 Massachusetts Institute of Technology
(c)Copyright 1989-1993 The Open Software Foundation, Inc.
(c)Copyright 1990 Motorola, Inc.
(c)Copyright 1990, 1991, 1992 Cornell University
(c)Copyright 1989-1991 The University of Maryland
(c)Copyright 1988 Carnegie Mellon University
(c)Copyright 1991-2006 Mentat Inc.
(c)Copyright 1996 Morning Star Technologies, Inc.
(c)Copyright 1996 Progressive Systems, Inc.
Confidential computer software. Valid license from HP required for
possession, use or copying. Consistent with FAR 12.211 and 12.212,
Commercial Computer Software, Computer Software Documentation, and
Technical Data for Commercial Items are licensed to the U.S. Government
under vendor's standard commercial license.
You have mail.
./myscript.sh
exit
LANG has been set to C, but you can choose from C or ja_JP.SJIS or ja_JP.eucJP or ja_JP.utf8
hknnzb11:/home/def : ./myscript
-bash: ./myscript.sh: No such file or directory
hknnzb11:/home/def :
hknnzb11:/home/def : exit
logout
logout
Using username "def".
我认为这是同步问题,但我不知道如何解决这个问题。
您可以找到我尝试过的SSH.NET示例。
using (var client = new SshClient(Host, Users, Password))
{
client.Connect();
var cmd = client.RunCommand("cd /ssh/fd/ ; ./myscript.sh");
MessageBox.Show(cmd.Result);
client.Disconnect();
}
感谢。
答案 0 :(得分:0)
这比您的代码更像是SSH服务器的问题。
无论如何,您最好使用-m
command-line switch向Plink提供命令脚本。不同之处在于-m
指定的命令以更加受控的方式在“exec”通道中执行,然后在重定向输入时在您正在使用的“shell”通道中执行。
更好的是,使用本机C#库来执行“exec”频道中的命令:
E.g。使用SSH.NET:
using (var client = new SshClient(Host, Users, Password))
{
client.Connect();
client.RunCommand("cd /ssh/fd/ ; ./myscript.sh");
client.Disconnect();
}
信用:C# send a simple SSH command
如果上述方法不起作用,则是SSH服务器中的错误。
你可以做的就是解决它,在发送命令之前等待几秒钟。为此,您需要继续使用“shell”频道,就像在原始代码中一样。
在“exec”频道(我的解决方案)中,您无法延迟命令。只有在会话完全初始化后,服务器本身才应执行命令。