-1

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
weldja
  • 1
  • `pexpect.spawn("/usr/bin/ssh -l %s %s" % (user, host))` vs `args: ['/usr/bin/ssh', 'myuser', '10.0.0.58']` The command that the script said to execute includes "-l", but the command actually executed does not, and the resulting list of arguments isn't a valid form for an ssh command. Are you sure the script in your question is the actual script that's producing this output? – Kenster May 13 '16 at 21:54
  • right - I have tried several combinations including args: ['/usr/bin/ssh', '-l', 'myuser', '10.0.0.58']' 'args: ['/usr/bin/ssh', '-tt', '-l', 'myuser', '10.0.0.58']' 'args: ['/usr/bin/ssh', '-t', '-l', 'myuser', '10.0.0.58]' 'args: ['/usr/bin/ssh', '-t', '-t', '-l', 'myuser', '10.0.0.58']' also tried crontab - 'PATH=$PATH:/usr/bin && python "/path_to/try.py"' All fail with similar pexpect.EOF. – weldja May 14 '16 at 21:10
  • Hello - has anyone got an idea how to resolve this. Thanks – weldja Jun 06 '16 at 18:56

1 Answers1

0

I found a solution which was to upgrade pexpect from 2.3 to 4.1 (and to install it's dependency ptyprocess-0.5.1).

weldja
  • 1