Advertisement
idaresiwins

intopmode.py

May 6th, 2018
2,158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.65 KB | None | 0 0
  1. import netmiko
  2. import commands
  3. import smtplib
  4. import os
  5. from netmiko import ConnectHandler
  6. from email.mime.text import MIMEText
  7. from email.MIMEMultipart import MIMEMultipart
  8.  
  9.  
  10. exception = (netmiko.ssh_exception.NetMikoTimeoutException,
  11.              netmiko.ssh_exception.NetMikoAuthenticationException,
  12.              netmiko.ssh_exception.NetMikoTimeoutException,
  13.              netmiko.NetMikoTimeoutException,
  14.              netmiko.NetMikoAuthenticationException,
  15.              netmiko.NetmikoTimeoutError,
  16.              netmiko.NetmikoAuthError,
  17.              netmiko.ssh_exception.SSHException,
  18.              netmiko.ssh_exception.AuthenticationException)
  19.  
  20. #declairing variables for the networking devices
  21. switch_1 = {'device_type': 'cisco_ios', 'ip': '192.168.0.11', 'username': 'username', 'password': 'password'}
  22. switch_2 = {'device_type': 'cisco_ios', 'ip': '192.168.0.22', 'username': 'username', 'password': 'password'}
  23. switch_3 = {'device_type': 'cisco_ios', 'ip': '192.168.0.33', 'username': 'username', 'password': 'password'}
  24. switch_4 = {'device_type': 'cisco_ios', 'ip': '192.168.0.44', 'username': 'username', 'password': 'password'}
  25. switch_5 = {'device_type': 'cisco_ios', 'ip': '192.168.0.55', 'username': 'username', 'password': 'password'}
  26. switch_6 = {'device_type': 'cisco_ios', 'ip': '192.168.0.66', 'username': 'username', 'password': 'password'}
  27. switch_all = [switch_1, switch_2, switch_3, switch_4, switch_5, switch_6]
  28.  
  29. for a_device in switch_all: #Loop through all devices in switch_all
  30.  
  31.   try:
  32.     net_connect = ConnectHandler(**a_device)
  33.     currentDev = ("{}").format(a_device['ip'])
  34.  
  35.     sFileName = ("/root/python.sc/intopmode/.Status_{}.txt").format(a_device['ip'])
  36.     oldFileName = ("/root/python.sc/intopmode/Status_{}.old").format(a_device['ip'])
  37.     cSFileName = ("/root/python.sc/intopmode/Cut_Status_{}.txt").format(a_device['ip'])
  38.     diffFileName = ("/root/python.sc/intopmode/Last_Change{}.txt").format(a_device['ip'])
  39.  
  40.     output = net_connect.send_command("sho int status | inc trunk | routed") #get current status of interfaces
  41.     StatusW = open(sFileName, "w")
  42.     StatusW.write(output)
  43.     StatusW.close()
  44.     cut = commands.getoutput("cut -b 1-10 '" + sFileName + "' ") #return only the first ten columns
  45.     cutStatusW = open(cSFileName, "w")
  46.     cutStatusW.write(cut)
  47.     cutStatusW.close()
  48.     diff = commands.getoutput("diff '" + cSFileName + "' '" + oldFileName + "' ")
  49.     diffStatus = open(diffFileName, "w")
  50.     diffStatus.write(diff)
  51.     diffStatus.close()
  52.  
  53.     iface = commands.getoutput(" cat '" + diffFileName + "' | grep '<' ")
  54.     iface2 = commands.getoutput(" cat '" + diffFileName + "' | grep '>' ")
  55.  
  56.  
  57.     print("\n\n\n\n\n\n\n>>>>>>>>>>>>>>>> Status {0} <<<<<<<<<<<<<<<").format(a_device['ip'])
  58.     print(diff)
  59.     print(">>>>>>>>>>>>>>>>         End         <<<<<<<<<<<<<<<<\n\n\n\n\n\n\n")
  60.     commands.getoutput(" mv -f '" + cSFileName + "' '" + oldFileName + "' ")
  61.  
  62.  
  63.     if os.stat(diffFileName).st_size == 0:
  64.         pass
  65.     else:
  66.         body = iface
  67.         body = MIMEText(body)
  68.         fromaddr = "****@gmail.com"
  69.         toaddr = "****@gmail.com"
  70.         msg = MIMEMultipart()
  71.         msg['From'] = fromaddr
  72.         msg['To'] = toaddr
  73.         msg['Subject'] = "WARNING: Port operating mode has changed! " + str(currentDev) + str(iface) + str(iface2)
  74.         msg.attach(body)
  75.         server = smtplib.SMTP('smtp.gmail.com', 587)
  76.         server.starttls()
  77.         server.login(fromaddr, "****")
  78.         text = msg.as_string()
  79.         server.sendmail(fromaddr, toaddr, text)
  80.         server.quit()
  81.  
  82.   except exception:
  83.     print("Error: {} could not be contacted!").format(a_device['ip'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement