QuestionQ63

VerifiedDevice-Level Network Automation

An engineer must apply an initial CLI configuration to a router connected to a terminal server for Day-0 setup. The engineer must automate Telnet login and configuration to the console port by using pexpect, then save the changes. Drag the code snippets from the bottom into the boxes in the code to create the artifact. Not every option is used.

Drag & Drop
import pexpect

child = pexpect.("telnet 192.0.2.5 2001", =20)
child.("Username:")
child.("netops")
child.expect("Password:")
child.sendline("Cisco12345")
child.expect(">")
child.sendline("enable")
child.expect("Password:")
child.sendline("CiscoEnable")
child.expect("#")
child.sendline("configure terminal")
child.expect("(config)#")
child.sendline("hostname BR1-EDGE")
child.sendline("interface GigabitEthernet1")
child.sendline("ip address dhcp")
child.sendline("no shutdown")
child.sendline("end")
child.sendline("write memory")
child.expect("#")
child.close()
Explanation

Pexpect uses spawn() to create and control a child application, accepts timeout as a spawn parameter, uses expect() to wait for a matching prompt, and uses sendline() to send text with a line ending. This provides the required prompt-driven Telnet automation before the configuration is saved.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!