Our API can now provide static information about the network; anything that we can store in the database can be returned to the requester. It would be great if we can interact with our network directly, such as a query for the device information or to push configuration changes to the device.
We will start this process by leveraging the script we have already seen in Chapter 2, Low-Level Network Device Interactions, for interacting with a device via Pexpect. We will modify the script slightly into a function we can repeatedly use in chapter9_pexpect_1.py:
# We need to install pexpect for our virtual env
$ pip install pexpect
$ cat chapter9_pexpect_1.py
import pexpect
def show_version(device, prompt, ip, username, password):
device_prompt = prompt
child = pexpect.spawn('telnet ' + ip)
child.expect('Username:')
child.sendline...