Advertisement
Guest User

Untitled

a guest
Apr 10th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. def check_ping(data = None):
  2. ping_result = re.findall(r'!!!!!', data)
  3. return ping_result
  4.  
  5. Traceback (most recent call last):
  6. File "IPtracetest.py", line 97, in <module>
  7. ping_results = check_ping(data)
  8. File "IPtracetest.py", line 25, in check_ping
  9. ping_results = re.findall( r'!!!!!', data )
  10. File "C:Python27libre.py", line 181, in findall
  11. return _compile(pattern, flags).findall(string)
  12. TypeError: expected string or buffer
  13. P:PythonScripts>
  14.  
  15. # get the user credentials
  16. def get_credentials():
  17. username = raw_input('Enter your secondary LANID: ')
  18. password = getpass.getpass('Enter password for user %s: ' % username)
  19. return username, password
  20.  
  21. def get_ip():
  22. return raw_input('nEnter IP address of device you which to trace: ')
  23.  
  24. def get_ping():
  25. ping = "ping " + ip
  26. return ping
  27.  
  28. def check_ping(data = None):
  29. ping_result = re.findall(r'!!!!!', data)
  30. return ping_result
  31.  
  32. def get_ping_results(ping_results, ip ):
  33. if ping_results is '!!!!!' :
  34. trace = 'PING OK. Device is accessible'
  35. else:
  36. trace = 'Device is currently off the network!'
  37. return trace
  38.  
  39.  
  40. def get_gateways(data):
  41. gateways = re.findall(r'[0-9]+(?:.[0-9]+){3}', data )
  42. return gateways
  43.  
  44. def get_gateway(gateway_results):
  45. gateway = gateway_results[-2]
  46. return gateway
  47.  
  48. def get_mac(data):
  49. mac = re.findall(r'[0-9a-f]+(?:.[0-9a-f]+){2}', data )
  50. mac = mac[-1]
  51. return mac
  52.  
  53. def get_access_switch(data):
  54. pattern = r'csd+-d+w'
  55. access_switch = re.findall(pattern, data)
  56. return access_switch[0] #--- could return the index value, that should give you a string to pass off to the host
  57.  
  58. #def get_end_switch(data):
  59. # print data
  60. # pattern = r'csd+-d+w*'
  61. # end_switch = re.findall(pattern, data)
  62. # return end_switch
  63.  
  64.  
  65. def get_end_switch(data):
  66. #print data
  67. #pattern = r'csd+-d+w*'
  68. end_switch = re.findall(r'[c][s][0-9][0-9][0-9][?:-][w][w]', data)
  69. return end_switch[-2]
  70.  
  71.  
  72. def get_port(data):
  73. pattern = r'[GF][ai]d[d|/]*'
  74. port = re.findall(pattern, data)
  75. return port[0]
  76.  
  77. def connect_to_device(username, password, host, command):
  78.  
  79. try:
  80. print '.... connecting'
  81. ssh = paramiko.SSHClient()
  82. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  83. ssh.connect(host, username=username, password=password)
  84. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  85. chan = ssh.invoke_shell()
  86. chan.send(command + ('r'))
  87. time.sleep(.5)
  88. data = chan.recv(9999)
  89. return data
  90. except:
  91. print "nn **** Unable to connect. Check password, device may be down, or IP not in ARP table ****nn"
  92.  
  93. # ______________________________________MAIN______________________________________#
  94. if __name__ == "__main__":
  95. data = 0
  96. #print 'nEnter an IP address at a location and you will get the end point switch and port: n'
  97. username, password = get_credentials()
  98. ip = get_ip()
  99. ping = get_ping()
  100. data = connect_to_device(username, password, 'mls319-2c', ping)
  101. ping_results = check_ping(data)
  102. trace = get_ping_results(ping_results, ip)
  103. if trace is 'PING OK. Device is accessible':
  104. data = connect_to_device(username, password, 'mls319-2c', 'traceroute ' + ip)
  105. gateway_results = get_gateways(data)
  106. gateway = get_gateway(gateway_results)
  107. data = connect_to_device(username, password, gateway, 'sh ip arp | i ' + ip)
  108. mac = get_mac(data)
  109. data = connect_to_device(username, password, gateway, 'sh cdp nei' )
  110. access_switch = get_access_switch(data)
  111. #end_host, end_port = trace_mac(username, password, access_switch, mac)
  112. data = connect_to_device(username, password, access_switch, 'trace mac ' + mac + ' ' + mac)
  113. end_switch = get_end_switch(data)
  114. port = get_port(data)
  115. print 'nRouter IP is ', gateway
  116. print 'nMAC address is ', mac
  117. print 'nThe end switch is ', end_switch
  118. print 'nThe port is ', port
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement