********************************************************* Lab 1 - Displaying Output of Show Commands - Interactive ********************************************************** from telnetlib import Telnet usercmd = input('Enter the Command : ') usercmd = usercmd + '\n' telsession = Telnet('172.25.1.1') telsession.write(b'khawar\n') telsession.write(b'cisco\n') telsession.write(b'terminal length 0\n') telsession.write(usercmd.encode('ascii')) telsession.write(b'exit\n') print(telsession.read_all().decode('ascii')) Notes: - Invoke the Telnet Function from the built-in telnetlib - usercmd is a variable that will store the input from the input function. - telsession is a variable that points to the Telnet Function to the IP address of 172.25.1.1. You need to use it everytime you want to read or write from this session. - Send commands to the session using the Telnet function uses 2 methods: - telsession.write(b'xxxxxx\n') Send the command within the ' ' in the ASCII format (b' ') - telsession.write(usercmd.encode('ascii') If a variable needs to be sent, use the encode('ascii') method - telsession.read_all().decode('ascii') reads the terminal output for the entire session. - print function prints the output on the screen ****************************************************************** Lab 2 - Displaying Output of Show Commands - Interactive Login ****************************************************************** from telnetlib import Telnet HOST = input ('IP Address of device: ') USER = input ('Enter your Username: ') USER = USER + '\n' PASS = input ('Enter the password: ') PASS = PASS + '\n' usercmd = input('Enter the Command : ') usercmd = usercmd + '\n' telsession = Telnet(HOST) telsession.write(USER.encode('ascii')) telsession.write(PASS.encode('ascii')) telsession.write(b'terminal length 0\n') telsession.write(usercmd.encode('ascii')) telsession.write(b'exit\n') print(telsession.read_all().decode('ascii')) ********************************************************** Lab 3 - Configuring Interface using Telnet - Interactive ********************************************************** from telnetlib import Telnet HOST = input ('IP Address of device: ') USER = input ('Enter your Username: ') USER = USER + '\n' PASS = input ('Enter the password: ') PASS = PASS + '\n' intid = input('Enter the Interface to be configured "Loopback99,E0/0 etc" : ') intcmd = 'interface ' + intid + '\n' ipaddr = input('Enter the IP Address for the Interface: ') smask = input('Enter the Subnet Mask: ') ipcmd = 'ip address ' + ipaddr + ' ' + smask + '\n' telsession = Telnet(HOST) telsession.write(USER.encode('ascii')) telsession.write(PASS.encode('ascii')) telsession.write(b'terminal length 0\n') telsession.write(b'config t\n') telsession.write(intcmd.encode('ascii')) telsession.write(ipcmd.encode('ascii')) telsession.write(b'end\n') telsession.write(b'sh ip int brief\n') telsession.write(b'exit\n') telsession.read_until(b'#end') print (telsession.read_all().decode('ascii'))