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