Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import netmiko
- import commands
- import smtplib
- import os
- from netmiko import ConnectHandler
- from email.mime.text import MIMEText
- from email.MIMEMultipart import MIMEMultipart
- exception = (netmiko.ssh_exception.NetMikoTimeoutException,
- netmiko.ssh_exception.NetMikoAuthenticationException,
- netmiko.ssh_exception.NetMikoTimeoutException,
- netmiko.NetMikoTimeoutException,
- netmiko.NetMikoAuthenticationException,
- netmiko.NetmikoTimeoutError,
- netmiko.NetmikoAuthError,
- netmiko.ssh_exception.SSHException,
- netmiko.ssh_exception.AuthenticationException)
- #declairing variables for the networking devices
- switch_1 = {'device_type': 'cisco_ios', 'ip': '192.168.0.11', 'username': 'username', 'password': 'password'}
- switch_2 = {'device_type': 'cisco_ios', 'ip': '192.168.0.22', 'username': 'username', 'password': 'password'}
- switch_3 = {'device_type': 'cisco_ios', 'ip': '192.168.0.33', 'username': 'username', 'password': 'password'}
- switch_4 = {'device_type': 'cisco_ios', 'ip': '192.168.0.44', 'username': 'username', 'password': 'password'}
- switch_5 = {'device_type': 'cisco_ios', 'ip': '192.168.0.55', 'username': 'username', 'password': 'password'}
- switch_6 = {'device_type': 'cisco_ios', 'ip': '192.168.0.66', 'username': 'username', 'password': 'password'}
- switch_all = [switch_1, switch_2, switch_3, switch_4, switch_5, switch_6]
- for a_device in switch_all: #Loop through all devices in switch_all
- try:
- net_connect = ConnectHandler(**a_device)
- currentDev = ("{}").format(a_device['ip'])
- sFileName = ("/root/python.sc/intopmode/.Status_{}.txt").format(a_device['ip'])
- oldFileName = ("/root/python.sc/intopmode/Status_{}.old").format(a_device['ip'])
- cSFileName = ("/root/python.sc/intopmode/Cut_Status_{}.txt").format(a_device['ip'])
- diffFileName = ("/root/python.sc/intopmode/Last_Change{}.txt").format(a_device['ip'])
- output = net_connect.send_command("sho int status | inc trunk | routed") #get current status of interfaces
- StatusW = open(sFileName, "w")
- StatusW.write(output)
- StatusW.close()
- cut = commands.getoutput("cut -b 1-10 '" + sFileName + "' ") #return only the first ten columns
- cutStatusW = open(cSFileName, "w")
- cutStatusW.write(cut)
- cutStatusW.close()
- diff = commands.getoutput("diff '" + cSFileName + "' '" + oldFileName + "' ")
- diffStatus = open(diffFileName, "w")
- diffStatus.write(diff)
- diffStatus.close()
- iface = commands.getoutput(" cat '" + diffFileName + "' | grep '<' ")
- iface2 = commands.getoutput(" cat '" + diffFileName + "' | grep '>' ")
- print("\n\n\n\n\n\n\n>>>>>>>>>>>>>>>> Status {0} <<<<<<<<<<<<<<<").format(a_device['ip'])
- print(diff)
- print(">>>>>>>>>>>>>>>> End <<<<<<<<<<<<<<<<\n\n\n\n\n\n\n")
- commands.getoutput(" mv -f '" + cSFileName + "' '" + oldFileName + "' ")
- if os.stat(diffFileName).st_size == 0:
- pass
- else:
- body = iface
- body = MIMEText(body)
- fromaddr = "****@gmail.com"
- toaddr = "****@gmail.com"
- msg = MIMEMultipart()
- msg['From'] = fromaddr
- msg['To'] = toaddr
- msg['Subject'] = "WARNING: Port operating mode has changed! " + str(currentDev) + str(iface) + str(iface2)
- msg.attach(body)
- server = smtplib.SMTP('smtp.gmail.com', 587)
- server.starttls()
- server.login(fromaddr, "****")
- text = msg.as_string()
- server.sendmail(fromaddr, toaddr, text)
- server.quit()
- except exception:
- print("Error: {} could not be contacted!").format(a_device['ip'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement