python connect to server using SSH protocol

Simon Anders simon.anders at uibk.ac.at
Tue Feb 8 08:26:18 EST 2005


Hi

Laszlo Zsolt Nagy wrote:
> ajikoe at gmail.com wrote:
> 
>> How can python connect to server which use SSH protocol?
>> Is it easy since my python has to run third party vendor, write data,
>> read data inside the server (supercomputer).
>>  
>>
> In advance, I'm not sure if I understood your problem.  SSH is clearly a 
> remote
> shell. You will be able to execute other programs on the remote computer.
> Is is suitable to use this:
> 
> child_stdin,child_stdout,child_stderr =  os.popen2('ssh -l my_username  
> my_host')

This is what I was about to reply as well. But I did a short test 
program and encountered a problem:

import os
fi, foe = os.popen4 ('ssh dopey')
print >>fi, 'ls'
fi.close ()  # <-- this is annoying
for line in foe:
    print line,
foe.close ()

The above connects to a server, passes the command 'ls', which is 
executed there, and prints the returned result.

However, reading from foe succeeds only if fin has been closed before. 
An fi.flush() seems to be not sufficient. But if one wants Python to 
interactivly communicate with some shell on a remote machine, it is 
inconvenient to have to close and reopen the connection all the time.

There should be a better way.

    Simon



More information about the Python-list mailing list