I am having problems using cron to run a pexpect script on Solaris 10 that will ssh to other devices. The devices I am connecting to are networking elements which will accept ssh connections using user/pass (but I cannot reconfigure them).
I have this script that works fine when I run from a login shell but it fails when run from cron. I have read posts on setting the env to simulate an interactive shell but to no avail. Any help much appreciated. I am limited to using python 2.6, pexpect, Solaris 10.
Script is: /path_to/try.py
#!/usr/bin/env python
import pexpect
import traceback
import os, sys, time, re
from time import sleep
host='10.0.0.58'
user='myuser'
password='password'
child=pexpect.spawn("/usr/bin/ssh -l %s %s" % (user, host))
i = child.expect([pexpect.EOF, pexpect.TIMEOUT, "password: "])
if i == 0:
print "EOF ERROR SSH could not login %s . Here is what SSH said:"% (host)
print child.before, child.after
if i == 1: # Timeout
print "TIMEOUT ERROR SSH could not login. Here is what SSH said:" % (host)
print child.before, child.after
if i == 2:
child.sendline(password)
print "Got device password prompt"
print "finally"
exit;
crontab is:
42 * * * * PATH=$PATH:/usr/local/bin && bash -lc "/path_to/try.py"
Result from script via cron i.e.:
EOF ERROR SSH could not login 10.0.0.58 . Here is what SSH said:
<class 'pexpect.EOF'>
<pexpect.spawn object at 0x8ed30>
version: 2.3 ($Revision: 399 $)
command: /usr/bin/ssh
args: ['/usr/bin/ssh', 'myuser', '10.0.0.58']
searcher: searcher_re:
0: EOF
1: TIMEOUT
2: re.compile("password: ")
buffer (last 100 chars):
before (last 100 chars):
after: <class 'pexpect.EOF'>
match: <class 'pexpect.EOF'>
match_index: 0
exitstatus: None
flag_eof: True
pid: 18189
child_fd: 3
closed: False
timeout: 300
delimiter: <class 'pexpect.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
finally