Guest User

Untitled

a guest
Mar 3rd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. '''
  2.  
  3. Description: Takes a file of IP addresses as first argument, command as second argument
  4. and runs the command on all the devices
  5.  
  6. Example: python3 paramiko-test.py devices "show ver | inc uptime"
  7.  
  8. '''
  9.  
  10. from paramiko import client
  11. import argparse
  12.  
  13. parser = argparse.ArgumentParser(description='Send command to cisco device.')
  14. parser.add_argument('infile', metavar='input file', type=str, nargs=1, help='list of device ip addresses')
  15. parser.add_argument('command', metavar='command', type=str, nargs=1, help='send command to Cisco device')
  16.  
  17. args = parser.parse_args()
  18. infile = args.infile[0]
  19. command = args.command[0]
  20.  
  21.  
  22. class ssh:
  23. client = None
  24.  
  25. def __init__(self, address, username, password):
  26. print("Connecting to server.")
  27. self.client = client.SSHClient()
  28. self.client.set_missing_host_key_policy(client.AutoAddPolicy())
  29. self.client.connect(address, username=username, password=password, look_for_keys = False)
  30.  
  31. def sendCommand(self, command):
  32. if(self.client):
  33. stdin, stdout, stderr = self.client.exec_command(command)
  34. while not stdout.channel.exit_status_ready():
  35. if stdout.channel.recv_ready():
  36. alldata = stdout.channel.recv(1024)
  37. while stdout.channel.recv_ready():
  38. alldata += stdout.channel.recv(1024)
  39.  
  40. print(str(alldata, "utf8"))
  41.  
  42. else:
  43. print("Connection not opened")
  44.  
  45. with open(infile) as f:
  46. address = f.readlines()
  47. address = [x.strip() for x in address]
  48. for node in address:
  49. if (node != ""):
  50. connection = ssh(node, "cisco", "cisco")
  51. connection.sendCommand(command)
Add Comment
Please, Sign In to add comment