Guest User

Untitled

a guest
Mar 12th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. # Creator : Uriel Yochpaz
  2.  
  3. # Imports
  4. import re
  5. import time
  6. import os
  7. import sys
  8. import netmiko
  9. import telnetlib
  10.  
  11. # Constants
  12. USER = 'user' # Change it to your User
  13. PASS = 'password' # Change it to your Password
  14. l_telnet = []
  15.  
  16.  
  17. def connect_telnet(ip):
  18.  
  19. """
  20. connect_telnet(ip) >>> bool
  21. This function gets device ip, run commands on the device via TELNET protocol
  22. and creates report on devices that supports Telnet
  23. """
  24.  
  25. # Try to connect with Telnet
  26. try:
  27. device = telnetlib.Telnet(ip)
  28. # The device doesnt support either SSH or Telnet
  29. except:
  30. return False
  31.  
  32. device.read_until("Username: ")
  33. device.write(USER + "\n")
  34. device.read_until("Password: ")
  35. device.write(PASS + "\n")
  36.  
  37. # Telnet is sucks
  38. time.sleep(1.5)
  39.  
  40. try:
  41. msg = device.read_until("Login invalid", timeout=1)
  42. if "Login invalid" in msg:
  43. print "Wrong credentials with Telnet"
  44. return False
  45. except:
  46. pass
  47.  
  48. print "Connected with Telnet"
  49. device.write("config t" + "\n")
  50. device.read_until("#")
  51. device.write("ENTER COMMAND HERE" + "\n")
  52. device.read_until("#")
  53. device.write("ENTER COMMAND HERE" + "\n")
  54. device.read_until("#")
  55. device.write("ENTER COMMAND HERE" + "\n")
  56. device.read_until("#")
  57.  
  58. print "Configuration committed successfully with Telnet at IP: {}\n".format(ip)
  59.  
  60. # Report on Telnet devices (Unsecured)
  61. with open(r"Telnet_devices.txt", 'a') as log:
  62. log.write(ip+'\n')
  63. return True
  64.  
  65.  
  66. def connect_ssh(ip):
  67.  
  68. """
  69. check_ssh(ip) >>> bool
  70. This function gets device ip and run commands on this device via SSH protocol
  71. """
  72.  
  73. print "Trying to connect with SSH to {}...".format(ip)
  74.  
  75. # Trying to connect with SSH
  76. try:
  77. device = netmiko.ConnectHandler(device_type='cisco_ios', ip = ip, username = USER, password = PASS)
  78.  
  79. # Wrong Credential's Error
  80. except netmiko.NetMikoAuthenticationException:
  81. print "Wrong Credentials (Username or Password) for SSH!\n"
  82. sys.exit(0)
  83.  
  84. # Here, sys.exit(0) means continue to next device
  85.  
  86. # SSH is disable Error
  87. except netmiko.NetMikoTimeoutException:
  88. print "SSH is disabled on {}".format(ip)
  89. if connect_telnet(ip) is False:
  90. print "Failed to connect to {} with SSH and Telnet".format(ip)
  91. sys.exit(0)
  92. else:
  93. return True
  94. except:
  95. sys.exit(0)
  96.  
  97. try:
  98. # Entering config mode
  99. device.config_mode() # If you run only Show Commands you do not need this
  100.  
  101. # Something wrong with netmiko module
  102. except:
  103. sys.exit(0)
  104.  
  105. device.send_command("ENTER COMMAND HERE")
  106. device.send_command("ENTER COMMAND HERE")
  107. device.send_command("ENTER COMMAND HERE")
  108.  
  109. # Exit config mode
  110. device.exit_config_mode()
  111.  
  112. # Saves configuration
  113. device.send_command("wr")
  114.  
  115. # Disconnect device
  116. device.disconnect()
  117.  
  118. print "Done with {} !\n".format(ip)
  119.  
  120.  
  121. def main():
  122.  
  123. # Read all ip's
  124. with open(r"Devices ip's.txt", 'r') as f:
  125. data = f.read()
  126.  
  127. # Ip regex pattern
  128. pat = r'(?:\d{1,3}\.){3}\d{1,3}'
  129.  
  130. # Get all ip's
  131. l_ip = re.findall(pat, data)
  132.  
  133. for ip in l_ip:
  134. try:
  135. connect_ssh(ip)
  136.  
  137. # Something wrong with the device
  138. except SystemExit:
  139. continue
  140.  
  141.  
  142. if __name__ == '__main__':
  143. main()
Add Comment
Please, Sign In to add comment