Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. import paramiko #Used for SSH
  4. import time
  5. import re
  6. import getpass #Used for masking passwords
  7.  
  8. def disable_paging(_conn):
  9.     '''Disable paging on a Cisco router'''
  10.  
  11.     remote_conn.send("terminal length 0\n")
  12.     time.sleep(1)
  13.  
  14.     # Clear the buffer on the screen
  15.     output = remote_conn.recv(9000)
  16.  
  17.     return output
  18.  
  19. def stp_changes(ip, username, password):
  20.     # Create instance of SSHClient object
  21.     remote_conn_pre = paramiko.SSHClient()
  22.  
  23.     # Automatically add untrusted hosts
  24.     remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  25.  
  26.     # Initiate SSH connection
  27.     remote_conn_pre.connect(ip, username=username, password=password)
  28.  
  29.     # Use invoke_shell to establish an 'interactive session'
  30.     remote_conn = remote_conn_pre.invoke_shell()
  31.  
  32.     # Strip the initial router prompt
  33.     output = remote_conn.recv(9000)
  34.  
  35.     # Turn off paging
  36.     disable_paging(remote_conn)
  37.  
  38.     # send a command
  39.     remote_conn.send("\n")
  40.     remote_conn.send("show spanning-tree detail | i ieee|from|last\n")
  41.  
  42.     # Wait for the command to complete
  43.     time.sleep(2)
  44.    
  45.     #Gat output from previous command
  46.     stp_changes = remote_conn.recv(9000)
  47.  
  48.     #Change from bytes to readable code
  49.     stp_changes = stp_changes.decode("utf-8")
  50.     print(stp_changes)
  51.     print('\n')
  52.     remote_conn.close()
  53.  
  54.  
  55.  
  56. if __name__ == '__main__':
  57.     #Prompt user for credentials to be used for authentication
  58.     filename = 'switches.txt'
  59.     username = input("Username:")
  60.     #Get password with masking
  61.     password = getpass.getpass()
  62.     #Open file
  63.     with open(filename, 'r') as f:
  64.         for ip in f:
  65.             stp_changes(ip.rstrip('\n'), username, password)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement