Valerian Banu
2010-09-06 15:18:58 UTC
Hi all,
I'm trying to use paramiko to run several remote commands on different
machines with different operating systems. I have a problem running commands
on a Windows XP machine which has Cygwin installed on it. Here is an
example:
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('labws01', username='test', password='test')
stdin, stdout, stderr = ssh.exec_command("uname; whoami; pwd")
print ('stdout:')
data = stdout.readlines()
for line in data:
print (line.rstrip())
print ('stderr:')
data = stderr.readlines()
for line in data:
print (line.rstrip())
ssh.close()
The above code runs without a problem using Linux machines:
stdout:
Linux
test
/homes/test
stderr:
When i replace the hostname with my windows machine i get the following
output:
stdout:
stderr:
'uname' is not recognized as an internal or external command,
operable program or batch file.
My expected output was:
CYGWIN_NT-5.1
test
/cygdrive/c/Documents and Settings/test
tell it to connect to a Cygwin shell so I can have the same functionality as
with the Linux machines?
I have also tried this approach:
# t - Transport object attached to a socket, authenticated...
chan = t.open_session()
chan.get_pty()
chan.invoke_shell()
chan.send('uname; whoami; pwd\n')
time.sleep(2)
chan.close()
stdout = chan.makefile('rb', -1)
print ('stdout: ')
for i, line in enumerate(stdout):
print (str(i) + ': ' + line.rstrip())
stderr = chan.makefile_stderr('rb', -1)
print ('stderr: ')
for i, line in enumerate(stderr):
print (str(i) + ': ' + line.rstrip())
This approach works in a way. chan.get_pty() and chan.invoke_shell() do open
a Cygwin shell and I am able to send my command to be executed. But there
are two problems here:
1. I do not know when the command that i send using chan.send() has finished
executing. And I must wait for the command to execute in order to get the
output.
2. The output I receive from Cygwin is awful and really hard to interpret.
Is there an easier way to do this? Something more similar to the SSHClient()
method above?
Thank you for your help,
Vali
I'm trying to use paramiko to run several remote commands on different
machines with different operating systems. I have a problem running commands
on a Windows XP machine which has Cygwin installed on it. Here is an
example:
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('labws01', username='test', password='test')
stdin, stdout, stderr = ssh.exec_command("uname; whoami; pwd")
print ('stdout:')
data = stdout.readlines()
for line in data:
print (line.rstrip())
print ('stderr:')
data = stderr.readlines()
for line in data:
print (line.rstrip())
ssh.close()
The above code runs without a problem using Linux machines:
stdout:
Linux
test
/homes/test
stderr:
When i replace the hostname with my windows machine i get the following
output:
stdout:
stderr:
'uname' is not recognized as an internal or external command,
operable program or batch file.
My expected output was:
CYGWIN_NT-5.1
test
/cygdrive/c/Documents and Settings/test
From what I understand it tries to run the command i send using the Windows
command prompt instead of the Cygwin shell. Is there a way to specificallytell it to connect to a Cygwin shell so I can have the same functionality as
with the Linux machines?
I have also tried this approach:
# t - Transport object attached to a socket, authenticated...
chan = t.open_session()
chan.get_pty()
chan.invoke_shell()
chan.send('uname; whoami; pwd\n')
time.sleep(2)
chan.close()
stdout = chan.makefile('rb', -1)
print ('stdout: ')
for i, line in enumerate(stdout):
print (str(i) + ': ' + line.rstrip())
stderr = chan.makefile_stderr('rb', -1)
print ('stderr: ')
for i, line in enumerate(stderr):
print (str(i) + ': ' + line.rstrip())
This approach works in a way. chan.get_pty() and chan.invoke_shell() do open
a Cygwin shell and I am able to send my command to be executed. But there
are two problems here:
1. I do not know when the command that i send using chan.send() has finished
executing. And I must wait for the command to execute in order to get the
output.
2. The output I receive from Cygwin is awful and really hard to interpret.
Is there an easier way to do this? Something more similar to the SSHClient()
method above?
Thank you for your help,
Vali