I'm using Plink to connect ssh with C#. But when I send the command to server, Plink sends the command before it is logged-in. Therefore some commands does not work.
I send command from outside and these codes are,
cd /ssh/fd/
./myscript.sh
You can kindly find below the example code;
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"; }
}
}
This is the output:
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".
I think it is the synchronization problem but I do not know how can I solve this.
You can kindle find the SSH.NET example which I tried.
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();
}
Thanks.