By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,544 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,544 IT Pros & Developers. It's quick & easy.

SSH using Paramiko

P: n/a
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.

Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.