Access Cisco Firewall through Telnet using Python

In my company we have a couple of VPN’s configured and one of the VPN’s is essential to the core business of the company.

That particular VPN from time to time freezes and i need to disconnect it, but that isn’t always a simple task as when i am in vacations i don’t have access to internet so i needed a way for users to be able to disconnect the VPN themselves. The solution was a python script that connects to the Cisco equipment through¬†telnet using the telnetlib.

This is the script:

import telnetlib

pwd1 = “password
pwd2 = “enable_password
cmdVpn = “vpn-sessiondb logoff ipaddress 1.0.0.0”
cmdEn = “enable”
host = “10.60.2.254”

tn = telnetlib.Telnet(host)
##tn.set_debuglevel(5)

tn.read_until(“:”)
tn.write(pwd1 + “\n”)
tn.read_until(“ciscoasa>”)
tn.write(cmdEn + “\n”)
tn.read_until(“:”)
tn.write(pwd2 + “\n”)
tn.read_until(“ciscoasa#”)
tn.write(cmdVpn + “\n”)
tn.read_until(“[confirm]”)
tn.write(“\n”)
tn.read_until(“ciscoasa#”)
tn.write(“exit”+”\n”)
print tn.read_all()
tn.close()
print “Operation completed”