Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python
- import paramiko #Used for SSH
- import time
- import re
- import getpass #Used for masking passwords
- def disable_paging(_conn):
- '''Disable paging on a Cisco router'''
- remote_conn.send("terminal length 0\n")
- time.sleep(1)
- # Clear the buffer on the screen
- output = remote_conn.recv(9000)
- return output
- def stp_changes(ip, username, password):
- # Create instance of SSHClient object
- remote_conn_pre = paramiko.SSHClient()
- # Automatically add untrusted hosts
- remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- # Initiate SSH connection
- remote_conn_pre.connect(ip, username=username, password=password)
- # Use invoke_shell to establish an 'interactive session'
- remote_conn = remote_conn_pre.invoke_shell()
- # Strip the initial router prompt
- output = remote_conn.recv(9000)
- # Turn off paging
- disable_paging(remote_conn)
- # send a command
- remote_conn.send("\n")
- remote_conn.send("show spanning-tree detail | i ieee|from|last\n")
- # Wait for the command to complete
- time.sleep(2)
- #Gat output from previous command
- stp_changes = remote_conn.recv(9000)
- #Change from bytes to readable code
- stp_changes = stp_changes.decode("utf-8")
- print(stp_changes)
- print('\n')
- remote_conn.close()
- if __name__ == '__main__':
- #Prompt user for credentials to be used for authentication
- filename = 'switches.txt'
- username = input("Username:")
- #Get password with masking
- password = getpass.getpass()
- #Open file
- with open(filename, 'r') as f:
- for ip in f:
- stp_changes(ip.rstrip('\n'), username, password)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement