import shlex import subprocess def traceips(interface, dest): """ Using specified interface and dest as the destination, uses traceroute command to return the resulting traceroute. """ args = ["traceroute", "-4","-I", "-i", interface, "-n", "-N", "1", "-w", "1", "-q", "1", dest] #use -i when in larger interface p = subprocess.Popen(args, stdout=subprocess.PIPE) r = p.communicate()[0] p.wait() a = [] for line in r.split('\n')[1:]: piece = line.split(); if len(piece) > 0: a.append(piece[1]) return a def main(): ips = traceips("wlan0", "www.google.com") print ips if __name__ == "__main__": main()