Advertisement
Guest User

sadfas

a guest
Apr 20th, 2019
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import sys
  3. import time
  4. import paramiko
  5. import os
  6. import cmd
  7. import datetime
  8. import getpass
  9. from netmiko import ConnectHandler
  10.  
  11. now = datetime.datetime.now()
  12.  
  13. # File below needs to have IP addresses with no line breaks between them
  14. f = open ('ciscoswitchssh')
  15.  
  16. def choose():
  17. while True:
  18. print('Please choose one of the options below: ')
  19. print('1: Backup Configuration 2: Create new admin user: ')
  20. option = input('Enter your option: ')
  21. if option == '3'
  22. :
  23. break
  24. else:
  25. select(option)
  26. print('Exiting now ')
  27. choose()
  28. select()
  29. def select(option):
  30. if option == '1':
  31. option1()
  32. elif option == '2':
  33. option2
  34. else:
  35. print('Does not compute...Process crash ')
  36.  
  37. def option1():
  38. for line in f:
  39. #Credentials use to connect to switch/router
  40. ip_address = line
  41. ssh_client = paramiko.SSHClient()
  42. ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  43. ssh_client.connect(hostname=ip_address,username=username,password=password)
  44.  
  45. print ("Successful connection to backup the configuration " + ip_address)
  46.  
  47. remote_connection = ssh_client.invoke_shell()
  48. remote_connection.send("terminal length 0\n")
  49. remote_connection.send("show run\n")
  50. time.sleep(10)
  51. remote_connection.send("terminal no length\n")
  52. output = remote_connection.recv(65535)
  53. #print ("This is before decoding: + str(output)")
  54. print (output.decode())
  55. remote_connection.send("exit\n")
  56. filename = "%s_%.2i-%.2i-%i_%.2i-%.2i-%.2i" % (ip_address,now.day,now.month,now.year,now.hour,now.minute,now.second)
  57. ff = open(str(filename), "a")
  58. ff.write(output.decode())
  59. ff.close()
  60. #close ssh session
  61. ssh_client.close()
  62.  
  63. print (line)
  64. f.close()
  65.  
  66. def option2():
  67. for line in f:
  68. #Credentials use to connect to switch/router
  69. ip_address = line
  70. ssh_client = paramiko.SSHClient()
  71. ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  72. ssh_client.connect(hostname=ip_address,username=username,password=password)
  73.  
  74. #Prints successful connection to console followed by IP address of end device
  75. print ("Successful connection " + ip_address)
  76. time.sleep(3)
  77. remote_connection = ssh_client.invoke_shell()
  78. remote_connection.send("configure terminal\n")
  79. #No space between password and \n, it will add a space to the actual password
  80. remote_connection.send("username <USERNAME> privilege 15 password <PASSWORD>\n")
  81. remote_connection.send("line console 0\n")
  82. remote_connection.send("line vty 0 15\n")
  83. remote_connection.send("end\n")
  84. remote_connection.send("wr mem\n")
  85.  
  86. remote_connection.send("end\n")
  87. time.sleep(2)
  88. output = remote_connection.recv(65535)
  89. print (output.decode())
  90. print ("Successfully created admin account and saved config of " + ip_address)
  91. ssh_client.close
  92. print('Great choice, exiting now... ')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement