ssh
gert
gert.cuykens at gmail.com
Wed Apr 30 07:04:45 EDT 2008
Eric Wertman wrote:
>
>from popen2 import Popen3
>
>def ssh(host,command) :
> ''' Wraps ssh commands '''
> ssh_exec = ['/usr/bin/ssh -qnx -F ssh_config', host, command]
> cmd = ' '.join(ssh_exec)
> output,errors,status = process(cmd)
> return output,errors,status
>
>def process(cmd) :
> proc = Popen3(cmd,-1)
> output = proc.fromchild.readlines()
> errors = proc.childerr.readlines()
> status = proc.poll()
> return output,errors,status
thanks, what happens with the ssh connection after def process(cmd) is
done
Do i not need to remove the 'else' for it to work ?
Also can i execute multiple commands at once ?
import os, time
def ssh(user, rhost, pw, cmd):
pid, fd = os.forkpty()
if pid == 0:
os.execv("/bin/ssh", ["/bin/ssh", "-l", user, rhost] +
cmd)
time.sleep(0.2)
os.read(fd, 1000)
time.sleep(0.2)
os.write(fd, pw + "\n")
time.sleep(0.2)
res = ''
s = os.read(fd, 1)
while s:
res += s
s = os.read(fd, 1)
return res
print ssh('username', 'serverdomain.com', 'Password', ['ls -l'])
More information about the Python-list
mailing list