Basically I am creating a script to build SSH connection from my Windows 7 network management computer to Cisco devices and have interactive commands with those Cisco devices. This has been done by many other network engineer. With Google’s help, I am able to get their experiences to quickly implement it in my home lab environment.
It will be part of network management automation task. If
I can get this working it will prevent me from having to sign on to all of my Cisco devices and issue the command manually. Previously I used network automation tool InfoBlox NetMRI to achieve that. Now with Python script, it can be achieved almost with a very low cost.
Step 1. Install Python and necessary components on my Windows 7 Machine.
I had a post before to describe the steps how to install Python and related module into windows system.
- Install Python 2.7.15
- Download and install pycrypto win-amd64-py2.7.exe
- Open a command prompt window. Change the path to c:\Python27 then and go to Scripts directory.
- pip install paramiko
- Test with the following command:
- import paramiko
Step 2. Create a Script
C:\Python27>type cisco.py
import paramiko
from getpass import getpass
import time
import datetime
import sys
ip = "192.168.2.5"
username = "Cisco"
password = "Cisco"
f = open('1Reboot-APlog.txt', 'a')
old_stdout = sys.stdout
sys.stdout = f
remote_conn_pre=paramiko.SSHClient()
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
remote_conn_pre.connect(ip, port=22, username=username,
password=password,
look_for_keys=False, allow_agent=False)
remote_conn = remote_conn_pre.invoke_shell()
output = remote_conn.recv(65535)
print('\n\n\n##############################################################\n')
print (datetime.datetime.now())
print('\n##############################################################\n')
print (output)
remote_conn.send("enable\n")
time.sleep(.5)
output = remote_conn.recv(65535)
print (output)
remote_conn.send("Cisco\n")
time.sleep(.5)
output = remote_conn.recv(65535)
print (output)
remote_conn.send("reload\n")
time.sleep(.5)
output = remote_conn.recv(65535)
print (output)
remote_conn.send("y\n")
time.sleep(.5)
output = remote_conn.recv(65535)
print (output)
C:\Python27>python cisco.py
C:\Python27>type Newdaytest.txt
##############################################################
2018-08-24 12:38:42.178000
##############################################################****************************************************************
* This is a private computing facility. *
* Unauthorized use of this device is strictly prohibited. *
* Violators will be prosecuted to the maximum extent possible. *
* *
* TACACS+ Authentication and Accounting are in place. *
* All actions/commands are monitored and recorded. *
* By using the network you expressly consent to such *
* monitoring and recording. *
****************************************************************SW-FW-MGMT1#config t
Enter configuration commands, one per line. End with CNTL/Z.
SW-FW-MGMT1(config)#
file prompt quiet
SW-FW-MGMT1(config)#
end
SW-FW-MGMT1#
copy running-config tftp://192.168.154.5/
.
.
.
3. Schedule Daily Reboot Task in WindowsCreate a new daily task to run following command:c:\Python27\python.exe c:\Python27\1reboot-ap.py
4. Check Logs
Check log file 1Reboot-APlog.txt created under c:\Python27
##############################################################
2018-08-24 15:06:29.151000
##############################################################
1142>
enable
Password:
1142#
reload
Proceed with reload? [confirm]