Python Network Programming
上QQ阅读APP看书,第一时间看更新

Configuring Cisco switchport for IP Phone

Similar to the earlier scenario, where we want a switchport as a trunk port for AP, we can configure the switchport to work with IP Phones. An additional task for configuring a port to be used as IP Phone is that another end machine or data machine can be connected to the IP Phone for data transfer. In other words, a single switchport of a Cisco router can act as both a voice and data port when used with IP Phone.

Let's see an example of configuring a switchport to act as an IP Phone port:

from netmiko import ConnectHandler
import time

def ipphoneconfig(routerip,switchport):
uname="cisco"
passwd="cisco"
device = ConnectHandler(device_type='cisco_ios', ip=routerip, username=uname, password=passwd)
cmds="interface "+switchport
cmds=cmds+"\nswitchport mode access\nswitchport access vlan 100\n"
cmds=cmds+ "switchport voice vlan 200\nspanning-tree portfast\nno shut\n"
xcheck=device.send_config_set(cmds)
print (xcheck)
device.disconnect()

def validateswitchport(routerip,switchport):
print ("\nValidating switchport...."+switchport)
uname="cisco"
passwd="cisco"
device = ConnectHandler(device_type='cisco_ios', ip=routerip, username=uname, password=passwd)
cmds="show interface "+switchport+" switchport "
outputx=device.send_command(cmds)
print (outputx)
outputx=outputx.split("\n")
for line in outputx:
if ("Trunking Native Mode VLAN: 10" in line):
changenativevlan(routerip,switchport,"20")
device.disconnect()

ipphoneconfig("192.168.255.245","FastEthernet2/5")
time.sleep(5) # 5 seconds
validateswitchport("192.168.255.245","FastEthernet2/5")

The output is as follows:

As we see now, the port configured (FastEthernet 2/5) has been assigned a Voice VLAN of 200 and a data/access VLAN of 100 (from the preceding output, notice the line Access Mode VLAN: 100 (VLAN0100). Any IP Phone connecting to this port will have access to both the VLANs for its voice and data usage. Again, going by previous examples, we can perform additional validations and checks on the ports and trigger some actions in case of any incorrect or missing configs.