0

I am trying to use pexpect to telnet to a device and then subsequently telnet from that device to another device and then output the result of a command on the second device but i am unable to. Here is the script i wrote and the error i am getting

I will appreciate help very much. Thanks

import pexpect

child = pexpect.spawn('telnet 192.168.2.45');

child.expect('assword:');
child.sendline('getin');

child.expect('AS-C5350-02>');
child.sendline('telnet 192.168.1.2'); 
child.expect('\nTrying 192.168.1.2 ... Open\n\n\nUser Access Verification\n nPassword:');
child.sendline('cisco'); 
child.expect('ipipgw01>'); 
child.sendline('sh clock');
child.sendline('exit');

child.expect(pexpect.EOF);

print child.before;

Here is the error i keep getting

Traceback (most recent call last):
  File "expect_telnetback.py", line 14, in <module>
  child.expect('\nTrying 192.168.1.2 ... Open\n\n\nUser Access Verification\n    \nPassword:');
  File "/usr/lib/python2.6/dist-packages/pexpect.py", line 1311, in expect
  return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
  File "/usr/lib/python2.6/dist-packages/pexpect.py", line 1325, in expect_list
  return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
  File "/usr/lib/python2.6/dist-packages/pexpect.py", line 1409, in expect_loop
  raise TIMEOUT (str(e) + '\n' + str(self))
  pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().
 <pexpect.spawn object at 0xb7752a2c>
version: 2.3 ($Revision: 399 $)
command: /usr/bin/telnet
args: ['/usr/bin/telnet', '192.168.2.45']
searcher: searcher_re:
0: re.compile("
Trying 196.192.7.14 ... Open


User Access Verification

Password:")
buffer (last 100 chars): telnet 192.168.1.2
Trying 192.168.1.2 ... Open


User Access Verification

Password: 
before (last 100 chars): telnet 192.168.1.2
Trying 192.168.1.2 ... Open


User Access Verification

Password: 
after: <class 'pexpect.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 26275
child_fd: 3
closed: False
timeout: 30
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
user2041020
  • 11
  • 1
  • 2
  • Well, the error says you're getting a timeout in `/usr/bin/telnet 192.168.2.45` waiting for the response to `'telnet 192.168.1.2'`. Have you made sure you can `telnet 192.168.2.45`, and from there `telnet 192.168.1.2`, and see the string you're expecting? – abarnert Feb 04 '13 at 23:07
  • 1
    Looks like a problem in this line: `child.expect('\nTrying 192.168.1.2 ... Open\n\n\nUser Access Verification\n nPassword:');` Looks like you're missing a backslash before Password. That's failing to match the prompt, so timing out. – John Hazen Feb 05 '13 at 00:46
  • Yes i checked it and the string i am expecting is correctly represented. I modified the script a bit by changing the portion that had that line `code` child.expect('Password:'); child.sendline('cisco'); child.expect('ipipgw01>'); child.sendline('sh clock'); child.sendline('exit'); child.expect('AS-C5350-02>'); child.sendline('exit'); child.expect(pexpect.EOF); print child.before; `code` This only gives this output exit Connection closed by foreign host. – user2041020 Feb 05 '13 at 01:01
  • Why do you have semicolons? – technazi May 19 '16 at 08:25

0 Answers0