1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """
18 Functions to retrieve hostname and IP address of the machine running Elisa
19 """
20
21 __maintainer__ = 'Philippe Normand <philippe@fluendo.com>'
22
23 import socket
24 import struct
25 import platform
26
28 """
29 Retrieve host address for Win32 platform
30
31 @rtype: string
32 """
33 s = socket.gethostname()
34 return socket.gethostbyname(s)
35
37 """
38 Retrieve host address for Linux platform
39
40 @rtype: string
41 """
42 route_file = '/proc/net/route'
43 route = open(route_file)
44 host_address = '127.0.0.1'
45 if route:
46 tmp = route.readline()
47 while tmp != '':
48 tmp = route.readline()
49 l = tmp.split('\t')
50 if len(l) > 2:
51 if l[2] != '00000000':
52 host_address = get_ip_address(l[0])
53 break
54
55 route.close()
56 return host_address
57
59 """
60 Retrieve current host address for Win32 and Linux platforms
61
62 @raises ValueError: if current system's platform is not windows or linux
63 @rtype: string
64 """
65 platform_type = platform.system().lower()
66 if platform_type == "windows":
67 return _get_host_address_win()
68 elif platform_type == "linux":
69 return _get_host_address_linux()
70 else:
71 raise ValueError("Unsupported platform")
72
74 """
75 Retrieve IP address of the given network interface, on a Linux platform
76
77 @param ifname: network interface name
78 @type ifname: string
79 @rtype: string
80 """
81 import fcntl
82
83 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
84 ip_address = socket.inet_ntoa(fcntl.ioctl(
85 s.fileno(),
86 0x8915,
87 struct.pack('256s', ifname[:15])
88 )[20:24])
89 return ip_address
90
92 """
93 Retrieve IP address of the given network interface, on a Windows platform
94
95 @param ifname: network interface name
96 @type ifname: string
97 @rtype: string
98 """
99
100 return ""
101
103 """
104 Retrieve name of the network interface connected to the default
105 gateway, on a Windows platform.
106
107 @rtype: string
108 """
109
110 return "eth0"
111
113 """
114 Retrieve name of the network interface connected to the default
115 gateway, on a Linux platform.
116
117 @rtype: string
118 """
119 route_file = '/proc/net/route'
120 route = open(route_file)
121 iface = 'eth0'
122 if route:
123 tmp = route.readline()
124 while tmp != '':
125 tmp = route.readline()
126 l = tmp.split('\t')
127 if len(l) > 2:
128 if l[2] == '00000000':
129 iface = l[0]
130 break
131 route.close()
132 return iface
133
134
136 """
137 Retrieve IP address of the given network interface, on a Windows platform
138
139 @raises ValueError: if current system's platform is not windows or linux
140 @param ifname: network interface name
141 @type ifname: string
142 @rtype: string
143 """
144 platform_type = platform.system().lower()
145 if platform_type == "windows":
146 if ifname is None:
147 ifname = _get_win32_default_iface()
148 return _get_win32_ip_address(ifname)
149 elif platform_type == "linux":
150 if ifname is None:
151 ifname = _get_linux_default_iface()
152 return _get_linux_ip_address(ifname)
153 else:
154 raise ValueError("Unsupported platform")
155