Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def check_ping(data = None):
- ping_result = re.findall(r'!!!!!', data)
- return ping_result
- Traceback (most recent call last):
- File "IPtracetest.py", line 97, in <module>
- ping_results = check_ping(data)
- File "IPtracetest.py", line 25, in check_ping
- ping_results = re.findall( r'!!!!!', data )
- File "C:Python27libre.py", line 181, in findall
- return _compile(pattern, flags).findall(string)
- TypeError: expected string or buffer
- P:PythonScripts>
- # get the user credentials
- def get_credentials():
- username = raw_input('Enter your secondary LANID: ')
- password = getpass.getpass('Enter password for user %s: ' % username)
- return username, password
- def get_ip():
- return raw_input('nEnter IP address of device you which to trace: ')
- def get_ping():
- ping = "ping " + ip
- return ping
- def check_ping(data = None):
- ping_result = re.findall(r'!!!!!', data)
- return ping_result
- def get_ping_results(ping_results, ip ):
- if ping_results is '!!!!!' :
- trace = 'PING OK. Device is accessible'
- else:
- trace = 'Device is currently off the network!'
- return trace
- def get_gateways(data):
- gateways = re.findall(r'[0-9]+(?:.[0-9]+){3}', data )
- return gateways
- def get_gateway(gateway_results):
- gateway = gateway_results[-2]
- return gateway
- def get_mac(data):
- mac = re.findall(r'[0-9a-f]+(?:.[0-9a-f]+){2}', data )
- mac = mac[-1]
- return mac
- def get_access_switch(data):
- pattern = r'csd+-d+w'
- access_switch = re.findall(pattern, data)
- return access_switch[0] #--- could return the index value, that should give you a string to pass off to the host
- #def get_end_switch(data):
- # print data
- # pattern = r'csd+-d+w*'
- # end_switch = re.findall(pattern, data)
- # return end_switch
- def get_end_switch(data):
- #print data
- #pattern = r'csd+-d+w*'
- end_switch = re.findall(r'[c][s][0-9][0-9][0-9][?:-][w][w]', data)
- return end_switch[-2]
- def get_port(data):
- pattern = r'[GF][ai]d[d|/]*'
- port = re.findall(pattern, data)
- return port[0]
- def connect_to_device(username, password, host, command):
- try:
- print '.... connecting'
- ssh = paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- ssh.connect(host, username=username, password=password)
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- chan = ssh.invoke_shell()
- chan.send(command + ('r'))
- time.sleep(.5)
- data = chan.recv(9999)
- return data
- except:
- print "nn **** Unable to connect. Check password, device may be down, or IP not in ARP table ****nn"
- # ______________________________________MAIN______________________________________#
- if __name__ == "__main__":
- data = 0
- #print 'nEnter an IP address at a location and you will get the end point switch and port: n'
- username, password = get_credentials()
- ip = get_ip()
- ping = get_ping()
- data = connect_to_device(username, password, 'mls319-2c', ping)
- ping_results = check_ping(data)
- trace = get_ping_results(ping_results, ip)
- if trace is 'PING OK. Device is accessible':
- data = connect_to_device(username, password, 'mls319-2c', 'traceroute ' + ip)
- gateway_results = get_gateways(data)
- gateway = get_gateway(gateway_results)
- data = connect_to_device(username, password, gateway, 'sh ip arp | i ' + ip)
- mac = get_mac(data)
- data = connect_to_device(username, password, gateway, 'sh cdp nei' )
- access_switch = get_access_switch(data)
- #end_host, end_port = trace_mac(username, password, access_switch, mac)
- data = connect_to_device(username, password, access_switch, 'trace mac ' + mac + ' ' + mac)
- end_switch = get_end_switch(data)
- port = get_port(data)
- print 'nRouter IP is ', gateway
- print 'nMAC address is ', mac
- print 'nThe end switch is ', end_switch
- print 'nThe port is ', port
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement