invik

fakehtcnat.py 2.1

Apr 10th, 2012
221
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /usr/bin/python2
  2.  
  3. # fakehtcnat.py, version 2.1 by invik
  4.  
  5.  
  6. import io,socket,subprocess,sys, time
  7.  
  8. dhcpfile = "/var/state/dhclient/dhclient.leases"
  9. srvrstr = "option dhcp-server-identifier"
  10.  
  11. print "Waiting for the phone to come up...\n"
  12.  
  13. # To send things to trash
  14. o = file("/dev/null","w")
  15.  
  16. # Wait up to 10 seconds waiting for usb0 to show up
  17. i=0
  18. while (i<10):
  19.     rtn = subprocess.call(["/sbin/ifconfig","usb0"], stdout = o, stderr=o)
  20.     if (not rtn): break
  21.     time.sleep(1)
  22.     i += 1
  23.  
  24. if (rtn):
  25.     print "ERROR: Timeout waiting for usb0 to come up\n"
  26.     sys.exit(1)
  27.  
  28. # Get a dhcp address
  29. rtn = subprocess.call(["sudo","/usr/sbin/dhclient","usb0"], stdout = o, stderr=o)
  30. if (rtn):
  31.     print "ERROR: not able to get IP from DHCP\n"
  32.     sys.exit(2)
  33.  
  34. # Check that usb0 is up and has IP
  35. pipeout = subprocess.Popen(["/sbin/ifconfig","usb0"],stdout = subprocess.PIPE,stderr=subprocess.STDOUT)
  36. rtn = pipeout.communicate()[0].find("192.168.99")
  37. if (rtn<0):
  38.     print "ERROR: usb0 is not up or has not IP\n"
  39.     sys.exit(3)
  40.  
  41. # Read dhcp lease file and get phone's address
  42. f = open(dhcpfile,'r')
  43. d = f.read()
  44. u = d.rfind("interface \"usb0\"")
  45. l = d.find(srvrstr,u)
  46. e = d.find(';',l)
  47. host = d[l+len(srvrstr)+1:e]
  48.  
  49. print "Phone is up at IP = " + host + "\n"
  50.  
  51. # Send activation command to phone
  52. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  53. s.setsockopt(socket.SOL_IP, socket.IP_TTL, 128 )
  54. s.connect((host, 6000))
  55. s.sendall(chr(0) + chr(2) + chr(0) + chr(0))
  56. s.close()
  57.  
  58. print "Connection signal sent.\n"
RAW Paste Data