Our first Pexpect program

Our first program, chapter2_1.py, extends what we did in the last section with some additional code:

     #!/usr/bin/python3     import pexpect     devices = {'iosv-1': {'prompt': 'iosv-1#', 'ip': '172.16.1.20'}, 'iosv-2': {'prompt': 'iosv-2#', 'ip': '172.16.1.21'}}     username = 'cisco'     password = 'cisco'     for device in devices.keys():         device_prompt = devices[device]['prompt']         child = pexpect.spawn('telnet ' + devices[device]['ip'])         child.expect('Username:')         child.sendline(username)         child.expect('Password:')         child.sendline(password)         child.expect(device_prompt)         child.sendline('show version | i V')         child.expect(device_prompt)         print(child.before)         child.sendline('exit')

We use a nested dictionary in line 5:

 devices = {'iosv-1': {'prompt': ...

Get Mastering Python Networking - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.