Advertisement
Guest User

Untitled

a guest
Sep 27th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. import sys
  2. import telnetlib
  3. import getpass
  4. import time
  5. import os
  6. import webbrowser
  7. #function credential file
  8. #read only the file (credfile) line by line and return 2 values (user, password)
  9. def credent(credfile):
  10. cred = open(credfile, 'r')
  11. user = cred.readline().rstrip('\n')
  12. password = cred.readline().rstrip('\n')
  13. cred.close
  14. print(user)
  15. print(password)
  16. return (user, password)
  17. #function device file
  18. #reads only the file (devicefile) line by line and returns all the values
  19. def device(devicefile):
  20. with open(devicefile, 'r') as devicelist:
  21. hostlist=devicelist.readlines()
  22. return hostlist
  23. #function device ping
  24. def pingdevice():
  25. #ask the operator to input devicefile name (without the extension txt)
  26. devicefile = input('enter filewith devices : ')
  27. hostlist = device(devicefile + ".txt")
  28. #create a list (hostel) where it will put the ips that can be pinged
  29. hostel = []
  30. #each ip tested 1 by 1 if it can be pinged
  31. for host in hostlist:
  32. print(host)
  33. print(hostlist)
  34. print('ping -n 3 -w 500 ' + host)
  35.  
  36. response = os.system('ping -n 2 -w 200 ' + host)
  37. if response != 0:
  38. pingstatus = "Network Error"
  39. print(pingstatus)
  40. continue
  41. else:
  42. pingstatus = "Network Active"
  43. print(pingstatus)
  44. print(host)
  45. print(hostlist)
  46. #append every ip that can be pinged
  47. hostel.append(host);
  48. return hostel
  49.  
  50. #execute device ping
  51. #pingdevice()
  52. #function telnet
  53. def telnetdevice():
  54. #asks the operator to give the credential file name
  55. credentfile = input('enter filewith credential : ')
  56. #call the funcion pingdevice in order to take the list of ips that can be pinged
  57. hostlist = pingdevice()
  58. print(hostlist)
  59. #call the credent function to take the user and password
  60. (user, password) = credent(credentfile + ".txt")
  61. # devicefile = input('enter filewith devices : ')
  62. # hostlist = device(devicefile + ".txt")
  63. for host in hostlist:
  64. print(host)
  65. print(hostlist)
  66. try:
  67. tn = telnetlib.Telnet(host.rstrip('\n'),23,20)
  68. except:
  69. print("host unreachable" + host)
  70. continue
  71.  
  72. # if timer exceeds jump to the next step
  73. # do the telnet with the ip from
  74. tn.read_until(b'sername: ')
  75. #tn.write(user.encode('ascii'))
  76. tn.write(user.encode('ascii') + b"\n")
  77. tn.read_until(b'assword: ')
  78. #tn.write(password.encode('ascii'))
  79. tn.write(password.encode('ascii') + b"\n")
  80. tn.read_until(b'#')
  81. tn.write(b'term length 0 \n')
  82. tn.read_until(b'#')
  83. tn.write(b'show interface description | e BE\n')
  84. tn.read_until(b'#')
  85. tn.write(b'show run\n')
  86. tn.write(b"exit \n")
  87. output = tn.read_all().decode('ascii')
  88. op=open ("output.txt", "a")
  89. op.write(output)
  90. print("writing to file")
  91. op.close()
  92. print(output)
  93. tn.close
  94. # while not _is_eof(host):
  95. # host1 = host.readline()
  96. # tn = telnetlib.Telnet(host)
  97.  
  98. #cred = open('credentialstel.txt', 'r')
  99. #host1 = input('enter ip : ')
  100. #user = cred.readline().rstrip('\n')
  101. #password = cred.readline().rstrip('\n')
  102. #tn = telnetlib.Telnet(host1)
  103. telnetdevice()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement