ssh

gert gert.cuykens at gmail.com
Wed Apr 30 03:29:26 CEST 2008


Is this the best way to use ssh ?
How can i use ssh keys instead of passwords ?
I dont understand what happens when pid does not equal 0 , where does
the cmd get executed when pid is not 0 ?
How do you close the connection ?

# http://mail.python.org/pipermail/python-list/2002-July/155390.html
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)
	else:
		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