1 from subprocess
import *
5 if platform
.system() == 'Windows':
6 import _winreg
as winreg
9 os_type
= platform
.system()
10 if os_type
== "Linux":
11 interface
= get_linux_interface()
12 elif os_type
== 'Darwin':
13 interface
= get_darwin_interface()
14 elif os_type
== 'Windows':
15 interface
= get_windows_interface()
16 elif os_type
[-3:] == "BSD":
17 interface
= get_bsd_interface()
19 raise OSError, "Don't know how to identify the network interface used to reach the Internet. Try specifying one with -i <interface>"
21 print 'sorry, couldn''t detect your network interface (is it up?)\n'
22 print 'please use -i to specify an interface\n'
27 def get_local_ip(interface
=None):
29 Figure out the local IP address, by hook or by crook. Optional
30 command line hints about the local IP should be passed in here; sometimes
34 os_type
= platform
.system()
35 if os_type
== "Linux":
36 if not interface
: interface
= get_linux_interface()
38 ip
=get_linux_local_ip(interface
)
40 elif os_type
== 'Darwin':
41 if not interface
: interface
= get_darwin_interface()
42 interface
= get_darwin_interface()
44 ip
=get_darwin_local_ip(interface
)
46 elif os_type
== 'Windows':
47 return get_windows_local_ip()
48 elif os_type
[-3:] == "BSD":
49 if not interface
: interface
= get_bsd_interface()
51 ip
=get_darwin_local_ip(interface
)
54 print "Alice cannot id your local IP address. Check that you have \
55 network access; if you do, try adding your LAN IP manually with -l"
58 print "Alice could not id your network interface. Check that you have \
59 network access; if you do, try adding the local interface manually with -i"
63 # XXX Audit for local privilege escalation here
65 def get_linux_interface():
66 "Guess which interface we are using, on linux"
68 cmd
= Popen(["/sbin/route", "-n"], stdout
=PIPE
, stderr
=PIPE
)
72 cmd
= Popen(["route", "-n"], stdout
=PIPE
, stderr
=PIPE
)
78 for line
in cmd
.stdout
.readlines():
81 # This is the default route
82 if words
[0] == "0.0.0.0":
89 def get_darwin_interface():
90 "Guess which interface we are using, on darwin"
92 cmd
= Popen(["/sbin/route", "-n", "get", "default"], stdout
=PIPE
, stderr
=PIPE
)
96 cmd
= Popen(["route", "-n", "get", "default"], stdout
=PIPE
, stderr
=PIPE
)
102 for line
in cmd
.stdout
.readlines():
105 # This is the default route
106 if words
[0] == "interface:":
113 def get_windows_connection_names():
114 """ return a map from pretty connection name -> interface device guid """
115 magic
= 'SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}'
116 k
= winreg
.OpenKey(winreg
.HKEY_LOCAL_MACHINE
, magic
)
117 (nsk
, blah
, blah
) = winreg
.QueryInfoKey(k
)
119 for i
in xrange(0,nsk
):
120 dev
= winreg
.EnumKey(k
,i
)
121 if len(dev
) == 38 and dev
[0]=='{': # guid
123 sk
= winreg
.OpenKey(k
, dev
+'\Connection')
124 name
= winreg
.QueryValueEx(sk
, 'Name')
125 connections
[name
[0]] = dev
131 def get_windows_interface():
132 """ get device name of default interface """
133 # 1. get ip address of default route
134 ip
= get_windows_local_ip()
138 # 2. get network device keys from registry
139 devices
= get_windows_connection_names().values()
141 # 3. scan registry to figure out which has ip
144 kn
= "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\"+dev
145 k
= winreg
.OpenKey(winreg
.HKEY_LOCAL_MACHINE
, kn
)
146 enable_dhcp
= winreg
.QueryValueEx(k
, 'EnableDHCP')[0]
148 dhcp_ip
= winreg
.QueryValueEx(k
, 'DhcpIPAddress')[0]
150 return "\\Device\\NPF_"+dev
152 static_ip
= winreg
.QueryValueEx(k
, 'IPAddress')[0][0]
154 return "\\Device\\NPF_"+dev
160 def get_darwin_local_ip(interface
):
161 cmd
= Popen(["/sbin/ifconfig", interface
], stdout
=PIPE
, stderr
=PIPE
)
164 print cmd
.stderr
.read()
166 words
= cmd
.stdout
.read().split()
167 num_words
= len(words
)
168 for i
in range(num_words
):
169 if words
[i
] == "inet" and i
+1 < num_words
:
173 # what do you know, Darwin gets its code from BSD :)
175 get_bsd_interface
= get_darwin_interface
176 get_bsd_local_ip
= get_darwin_local_ip
178 def get_linux_local_ip(interface
):
179 cmd
= Popen(["/sbin/ifconfig", interface
], stdout
=PIPE
, stderr
=PIPE
)
182 print cmd
.stderr
.read()
184 words
= cmd
.stdout
.read().split()
186 if word
[:5] == "addr:":
189 def get_windows_local_ip():
190 cmd
= Popen(['route', 'print', '0.0.0.0'], stdout
=PIPE
, stderr
=PIPE
)
193 print cmd
.stderr
.read()
195 lines
= cmd
.stdout
.read().split("\r\n")
200 if len(words
) > 3 and words
[0] == '0.0.0.0':
207 if __name__
== "__main__":
208 print "Local IP is", get_local_ip()