My python pexpect script is finally working for me, except for the most important part updating the date! I can use SSH in the field, but my second command is not executing properly. I bang my head against the wall, trying to figure out why. I checked the output of the sting and it should work based on the encoded ones. I'm not an expert when it comes to python or pexpect, so I need a little help figuring out why my time is not updating.
my source code :
list = ["089"]
sn = 0
ssh_new_conn = 'Are you sure you want to continue connecting'
class ThreadClass(threading.Thread):
def __init__(self, index):
super(ThreadClass, self).__init__()
self.index = index
def run(self):
sn = storelist[self.index]
est = timezone('US/Eastern')
cst = timezone('US/Central')
currenttime = cst.localize(datetime.now())
easterndate = currenttime.astimezone(est).strftime("%a %b %d %H:%M:%S %Z %Y")
command1 = "/usr/bin/ssh %(username)s@%(hostname)s" % locals()
command2 = " sudo date -s\"%(easterndate)s\"" % locals()
command3 = " sudo date -s\"%(currenttime)s\"" % locals()
now = datetime.now()
if sn == "073" or sn == "066" or sn == "016":
p = pexpect.spawn((command1 + command3), timeout=360)
else:
print(command1 + command2)
p = pexpect.spawn((command1 + command2), timeout=360)
i = p.expect([ssh_new_conn,'[pP]assword:',pexpect.EOF])
print ' Initial pexpect command output: ', i
if i == 0:
p.sendline('yes')
i = p.expect(['[pP]assword:',pexpect.EOF])
print 'sent yes. pexpect command output', i
if i == 0:
print "logging into box %(sn)s" % locals()
p.sendline(password)
print "login successful"
print "Setting the time..."
elif i == 1:
print "logging into box %(sn)s" % locals()
p.sendline(password)
print "login successful"
print "Setting the time..."
p.close()
elif i == 2:
print "pexpect faced key or connection timeout"
pass
print p.before
for i in range(len(list)):
t = ThreadClass(i)
t.start()
New code :
class ThreadClass(threading.Thread):
def __init__(self, index):
super(ThreadClass, self).__init__()
self.index = index
def run(self):
try:
sn = storelist[self.index]
username = raw_input('username: ')
password = raw_input('password: ')
hostname = "[hostname]"
est = timezone('US/Eastern')
cst = timezone('US/Central')
currenttime = cst.localize(datetime.now())
easterndate = currenttime.astimezone(est).strftime("%a %b %d %H:%M:%S %Z %Y")
ssh = pxssh.pxssh()
print(hostname + " " + username + " " + password)
ssh.login(hostname, username, password)
if sn == "073" or sn == "066" or sn == "016":
ssh.sendline ('date')
ssh.prompt()
print(s.before)
ssh.sendline ('sudo date -s\"%(currenttime)s\"' % locals())
ssh.expect('(?i)password.*:')
ssh.sendline(password)
ssh.prompt()
print(s.before)
ssh.logout()
else:
ssh.sendline ('date')
ssh.prompt()
print(s.before)
ssh.sendline ('sudo date -s\"%(easterndate)s\"' % locals())
ssh.expect('(?i)password.*:')
ssh.sendline(password)
ssh.prompt()
print(s.before)
ssh.logout()
except pxssh.ExceptionPxssh as e:
print(e)
for i in range(len(storelist)):
t = ThreadClass(i)
t.start()
New mistake. I get :
Traceback (most recent call last):
File "./sshtest.py", line 8, in <module>
s.login (hostname, username, password)
File "/usr/lib/python2.6/dist-packages/pxssh.py", line 243, in login
if not self.synch_original_prompt():
File "/usr/lib/python2.6/dist-packages/pxssh.py", line 134, in synch_original_prompt
self.read_nonblocking(size=10000,timeout=1)
File "/usr/lib/python2.6/dist-packages/pexpect.py", line 824, in read_nonblocking
raise TIMEOUT ('Timeout exceeded in read_nonblocking().')
pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().
ERROR SOLUTION
I figured out the solution to the error I was getting. Due to a known error, I had to add the following lines to usr / lib / python.2.6 / dist-packages / pxssh.py:
self.sendline()
time.sleep(0.5)
self.read_nonblocking(size=10000,timeout=1)