Parent Directory
|
Revision Log
Revision 1.7 - (view) (download) (as text)
1 : | olson | 1.1 | import urlparse |
2 : | import socket | ||
3 : | import os | ||
4 : | import sys | ||
5 : | import re | ||
6 : | import FIG_Config | ||
7 : | |||
8 : | def get_local_hostname(): | ||
9 : | # | ||
10 : | olson | 1.7 | # See if there is a FIGdisk/config/hostname file. If there |
11 : | # is, force the hostname to be that. | ||
12 : | # | ||
13 : | |||
14 : | try: | ||
15 : | fh = open(os.path.join(FIG_Config.fig_disk, "config", "hostname")) | ||
16 : | host = fh.readline() | ||
17 : | return host.strip() | ||
18 : | except: | ||
19 : | pass | ||
20 : | |||
21 : | # | ||
22 : | olson | 1.1 | # First check to see if we our hostname is correct. |
23 : | # | ||
24 : | # Map it to an IP address, and try to bind to that ip. | ||
25 : | # | ||
26 : | |||
27 : | hostname = socket.getfqdn() | ||
28 : | olson | 1.3 | |
29 : | olson | 1.1 | # |
30 : | olson | 1.3 | # See if hostname is something.local., which is what |
31 : | # a Mac will return if it didn't get a name via some | ||
32 : | # other mechanism (DHCP or static config). We have to | ||
33 : | # check here because otherwise it will pass the fqdn and | ||
34 : | # local binding test. | ||
35 : | olson | 1.1 | # |
36 : | |||
37 : | olson | 1.4 | if not re.search(r"\.local\.?$", hostname): |
38 : | olson | 1.3 | |
39 : | # | ||
40 : | # First check that hostname is a fqdn, and that we can bind to it. | ||
41 : | # | ||
42 : | |||
43 : | if hostname.find('.') >= 0: | ||
44 : | if try_bind(hostname): | ||
45 : | return hostname | ||
46 : | |||
47 : | olson | 1.1 | # |
48 : | # Otherwise, do a hostname lookup and try to bind to the IP address. | ||
49 : | # | ||
50 : | |||
51 : | try: | ||
52 : | ip = socket.gethostbyname(hostname) | ||
53 : | |||
54 : | except socket.error: | ||
55 : | return get_hostname_by_adapter() | ||
56 : | |||
57 : | if not try_bind(ip): | ||
58 : | |||
59 : | return get_hostname_by_adapter() | ||
60 : | |||
61 : | # | ||
62 : | # It worked. Reverse-map back to a hopefully fqdn. | ||
63 : | # | ||
64 : | |||
65 : | try: | ||
66 : | rev = socket.gethostbyaddr(ip) | ||
67 : | |||
68 : | except socket.error: | ||
69 : | |||
70 : | # | ||
71 : | # Failed, return bare IP address. | ||
72 : | # | ||
73 : | |||
74 : | return ip | ||
75 : | |||
76 : | host = rev[0] | ||
77 : | # | ||
78 : | # Check to see if we have a FQDN. | ||
79 : | # | ||
80 : | if host.find(".") >= 0: | ||
81 : | return host | ||
82 : | else: | ||
83 : | return ip | ||
84 : | |||
85 : | def try_bind(host): | ||
86 : | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
87 : | try: | ||
88 : | sock.bind((host, 0)) | ||
89 : | return 1 | ||
90 : | except socket.error: | ||
91 : | return 0 | ||
92 : | |||
93 : | def get_hostname_by_adapter(): | ||
94 : | # | ||
95 : | # Attempt to determine our local hostname based on the | ||
96 : | # network environment. | ||
97 : | # | ||
98 : | # This implementation reads the routing table for the default route. | ||
99 : | # We then look at the interface config for the interface that holds the default. | ||
100 : | # | ||
101 : | # | ||
102 : | # Linux routing table: | ||
103 : | # [olson@yips 0.0.0]$ netstat -rn | ||
104 : | # Kernel IP routing table | ||
105 : | # Destination Gateway Genmask Flags MSS Window irtt Iface | ||
106 : | # 140.221.34.32 0.0.0.0 255.255.255.224 U 0 0 0 eth0 | ||
107 : | # 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 | ||
108 : | # 127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo | ||
109 : | # 0.0.0.0 140.221.34.61 0.0.0.0 UG 0 0 0 eth0 | ||
110 : | # | ||
111 : | # Mac routing table: | ||
112 : | # | ||
113 : | # bash-2.05a$ netstat -rn | ||
114 : | # Routing tables | ||
115 : | # | ||
116 : | # Internet: | ||
117 : | # Destination Gateway Flags Refs Use Netif Expire | ||
118 : | # default 140.221.11.253 UGSc 12 120 en0 | ||
119 : | # 127.0.0.1 127.0.0.1 UH 16 8415486 lo0 | ||
120 : | # 140.221.8/22 link#4 UCS 12 0 en0 | ||
121 : | # 140.221.8.78 0:6:5b:f:51:c4 UHLW 0 183 en0 408 | ||
122 : | # 140.221.8.191 0:3:93:84:ab:e8 UHLW 0 92 en0 622 | ||
123 : | # 140.221.8.198 0:e0:98:8e:36:e2 UHLW 0 5 en0 691 | ||
124 : | # 140.221.9.6 0:6:5b:f:51:d6 UHLW 1 63 en0 1197 | ||
125 : | # 140.221.10.135 0:d0:59:34:26:34 UHLW 2 2134 en0 1199 | ||
126 : | # 140.221.10.152 0:30:1b:b0:ec:dd UHLW 1 137 en0 1122 | ||
127 : | # 140.221.10.153 127.0.0.1 UHS 0 0 lo0 | ||
128 : | # 140.221.11.37 0:9:6b:53:4e:4b UHLW 1 624 en0 1136 | ||
129 : | # 140.221.11.103 0:30:48:22:59:e6 UHLW 3 973 en0 1016 | ||
130 : | # 140.221.11.224 0:a:95:6f:7:10 UHLW 1 1 en0 605 | ||
131 : | # 140.221.11.237 0:1:30:b8:80:c0 UHLW 0 0 en0 1158 | ||
132 : | # 140.221.11.250 0:1:30:3:1:0 UHLW 0 0 en0 1141 | ||
133 : | # 140.221.11.253 0:d0:3:e:70:a UHLW 13 0 en0 1199 | ||
134 : | # 169.254 link#4 UCS 0 0 en0 | ||
135 : | # | ||
136 : | # Internet6: | ||
137 : | # Destination Gateway Flags Netif Expire | ||
138 : | # UH lo0 | ||
139 : | # fe80::%lo0/64 Uc lo0 | ||
140 : | # link#1 UHL lo0 | ||
141 : | # fe80::%en0/64 link#4 UC en0 | ||
142 : | # 0:a:95:a8:26:68 UHL lo0 | ||
143 : | # ff01::/32 U lo0 | ||
144 : | # ff02::%lo0/32 UC lo0 | ||
145 : | # ff02::%en0/32 link#4 UC en0 | ||
146 : | |||
147 : | try: | ||
148 : | fh = os.popen("netstat -rn", "r") | ||
149 : | except: | ||
150 : | return "localhost" | ||
151 : | |||
152 : | interface_name = None | ||
153 : | for l in fh: | ||
154 : | cols = l.strip().split() | ||
155 : | |||
156 : | olson | 1.2 | if len(cols) > 0 and (cols[0] == "default" or cols[0] == "0.0.0.0"): |
157 : | olson | 1.1 | interface_name = cols[-1] |
158 : | break | ||
159 : | |||
160 : | fh.close() | ||
161 : | |||
162 : | olson | 1.2 | # print "Default route on ", interface_name |
163 : | olson | 1.1 | |
164 : | # | ||
165 : | # Find ifconfig. | ||
166 : | # | ||
167 : | |||
168 : | ifconfig = None | ||
169 : | |||
170 : | path = os.environ["PATH"].split(":") | ||
171 : | path.extend(["/sbin", "/usr/sbin"]) | ||
172 : | for p in path: | ||
173 : | i = os.path.join(p, "ifconfig") | ||
174 : | if os.access(i, os.X_OK): | ||
175 : | ifconfig = i | ||
176 : | break | ||
177 : | |||
178 : | if ifconfig is None: | ||
179 : | print >> sys.stderr, "Ifconfig not found" | ||
180 : | return "localhost" | ||
181 : | |||
182 : | olson | 1.2 | # print >> sys.stderr, "found ifconfig ", ifconfig |
183 : | olson | 1.1 | |
184 : | try: | ||
185 : | fh = os.popen(ifconfig+ " " + interface_name, "r") | ||
186 : | except: | ||
187 : | print >> sys.stderr, "Could not run ", ifconfig | ||
188 : | return "localhost" | ||
189 : | |||
190 : | ip = None | ||
191 : | |||
192 : | linux_re = re.compile("inet\s+addr:(\d+\.\d+\.\d+\.\d+)\s+") | ||
193 : | mac_re = re.compile("inet\s+(\d+\.\d+\.\d+\.\d+)\s+") | ||
194 : | |||
195 : | for l in fh: | ||
196 : | # | ||
197 : | # Mac: | ||
198 : | # inet 140.221.10.153 netmask 0xfffffc00 broadcast 140.221.11.255 | ||
199 : | # Linux: | ||
200 : | # inet addr:140.221.34.37 Bcast:140.221.34.63 Mask:255.255.255.224 | ||
201 : | # | ||
202 : | |||
203 : | l = l.strip() | ||
204 : | |||
205 : | m = linux_re.search(l) | ||
206 : | if m: | ||
207 : | # | ||
208 : | # Linux hit. | ||
209 : | # | ||
210 : | ip = m.group(1) | ||
211 : | break | ||
212 : | |||
213 : | m = mac_re.search(l) | ||
214 : | |||
215 : | if m: | ||
216 : | # | ||
217 : | # Mac hit. | ||
218 : | # | ||
219 : | ip = m.group(1) | ||
220 : | break | ||
221 : | fh.close() | ||
222 : | |||
223 : | if ip is None: | ||
224 : | print >> sys.stderr, "Didn't find an IP" | ||
225 : | return "localhost" | ||
226 : | |||
227 : | return ip | ||
228 : | |||
229 : | def cgi_url(): | ||
230 : | return plug_url(FIG_Config.cgi_url) | ||
231 : | |||
232 : | def temp_url(): | ||
233 : | return plug_url(FIG_Config.temp_url) | ||
234 : | |||
235 : | def plug_url(url): | ||
236 : | |||
237 : | name = get_local_hostname() | ||
238 : | |||
239 : | if not name: | ||
240 : | return url | ||
241 : | |||
242 : | p = urlparse.urlparse(url) | ||
243 : | |||
244 : | p = list(p) | ||
245 : | |||
246 : | p[1] = name | ||
247 : | |||
248 : | new_url = urlparse.urlunparse(p) | ||
249 : | return new_url | ||
250 : | |||
251 : | olson | 1.5 | def get_seed_id(): |
252 : | # | ||
253 : | # Retrieve the seed identifer from FIGdisk/config/seed_id. | ||
254 : | # | ||
255 : | # If it's not there, create one, and make it readonly. | ||
256 : | # | ||
257 : | |||
258 : | id_file = os.path.join(FIG_Config.fig_disk, "config", "seed_id") | ||
259 : | if not os.path.isfile(id_file): | ||
260 : | |||
261 : | fh = os.popen("uuidgen", "r") | ||
262 : | |||
263 : | newid = fh.readline() | ||
264 : | olson | 1.6 | newid = newid.strip() |
265 : | olson | 1.5 | |
266 : | fh.close() | ||
267 : | |||
268 : | fh = open(id_file, "w") | ||
269 : | print >>fh, newid | ||
270 : | fh.close() | ||
271 : | |||
272 : | os.chmod(id_file, 0444) | ||
273 : | |||
274 : | fh = open(id_file) | ||
275 : | id = fh.readline() | ||
276 : | fh.close() | ||
277 : | id = id.strip() | ||
278 : | return id | ||
279 : | |||
280 : | olson | 1.1 | if __name__ == "__main__": |
281 : | |||
282 : | print get_local_hostname() | ||
283 : | print cgi_url() | ||
284 : | print temp_url() | ||
285 : | olson | 1.5 | |
286 : | print get_seed_id() |
MCS Webmaster | ViewVC Help |
Powered by ViewVC 1.0.3 |