SSH using Paramiko

hanjar hanjar at phenix.rootshell.be
Thu Mar 25 13:47:46 EST 2004


Hello,

I'm using paramiko 0.9 to access one of my home machines and then execute
a command. But with the script included below, the command gets executed
on the 2nd to 6th go, never on the first. And the commands aren't that
commplicated, they are usualy 'touch some-file' or 
'cp some_file to /tmp/a'. 

Am I misunderstanding the paramiko's API or ... ?

This is the script:

import sys
import socket
import traceback
import threading
import paramiko


hostname = "192.168.0.10"
username = "alex"
port = 22
password = 'asdfg6'

# now connect
try:
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((hostname, port))
except Exception, e:
    print 'Connect failed: ' + str(e)
    traceback.print_exc()
    sys.exit(1)

t = paramiko.Transport(sock)
event = threading.Event()
t.start_client(event)
print "started client"
event.wait(15)

if not t.is_active():
    print 'SSH negotiation failed.'
    sys.exit(1)
else:
    print "SSH negotiation sucessful"

print "doing authentication"
t.auth_password(username, password, event)


event.clear()
event.wait(20)

if not t.is_authenticated():
    print 'Authentication failed. :('
    t.close()
    sys.exit(1)

if not t.is_active():
    print "Channel is not active"
else:
    print "Channel is active"
    chan = t.open_session()
    print "session opened"
    print "Channel name is " + chan.get_name()
    chan.exec_command("cp /etc/ftpusers /tmp/")
    print "executing the command"
    chan.close()
    t.close()
    print "exiting"



Thanks

P.S. I'm aware that this is not one of the cleanest scripts out there. 
Or even very security conscious. All I'm looking for is to get it to work,
then I'll worry about it after.




More information about the Python-list mailing list