-2

I am trying to connect a switch Juniper using pexpect library to get some interfaces informations, here is my code:

import pexpect

child = pexpect.spawn('ssh root@10.171.23.246')
child.expect('login as: ')
child.sendline('root')
child.expect('password:')
child.sendline(mypassword)
child.expect('% ')
child.sendline('cli')
child.expect('> ')
child.sendline('show interface')

Here is the result :

Traceback (most recent call last):

  File "first_test.py", line 4, in <module>

    child.expect('login as: ')

  File "/usr/lib/python2.7/site-packages/pexpect.py", line 1311, in 
expect

    return self.expect_list(compiled_pattern_list, timeout, 
searchwindowsize)

  File "/usr/lib/python2.7/site-packages/pexpect.py", line 1325, in 
expect_list

    return self.expect_loop(searcher_re(pattern_list), timeout, 
searchwindowsize)

  File "/usr/lib/python2.7/site-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 0x7fb21cfd5a50>

version: 2.3 ($Revision: 399 $)

command: /usr/bin/ssh

args: ['/usr/bin/ssh', 'root@10.171.23.246']

searcher: searcher_re:

    0: re.compile("login as: ")

buffer (last 100 chars): root@10.171.23.246's password:

before (last 100 chars): root@10.171.23.246's password:

after: <class 'pexpect.TIMEOUT'>

match: None

match_index: None

exitstatus: None

flag_eof: False

pid: 20230

child_fd: 3

closed: False

It seems like, the password is not send to the switch. I'm a new python user sorry if it's not clear. How can i connect properly to the switch ?

manoman
  • 1
  • 1
  • 3

2 Answers2

0

Try fabric - it is higher-level than pexpect and definitely easier to use.

Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.

Simple example based on your code would be:

from fabric.api import env, run
env.host_string = '10.171.23.246'
env.user = 'root'
env.password = 'put_your_password_here'

run('cli')
kchomski
  • 2,872
  • 20
  • 31
  • Thank you for your reply but as i need my script to be very portable i need to use pexpect. I will try fabric as my last option thank you. – manoman Aug 10 '17 at 12:37
  • run('cli') puts you in interactive shell and the python code is no further interpreted. – user3603644 Feb 03 '21 at 10:19
0

I find the answer after severals tests, the problem was the first expect "child.expect('login as: ')" who was not expect.

Then I put an expect a the end to wait the result of my command (show interface), and a print child.before to show the output of my last sendline()

import pexpect

child = pexpect.spawn('ssh root@10.171.23.246')
child.expect('login as: ')
child.sendline('root')
child.expect('password:')
child.sendline(mypassword)
child.expect('% ')
child.sendline('cli')
child.expect('> ')
child.sendline('show interface')
child.expect(' > ')
print child.before

It works !

manoman
  • 1
  • 1
  • 3