add some doc to sprunge
[xo.git] / lan
blob7fa88c3ede05729cc7aa46a125767996b5522964
1 #!/usr/bin/python
3 import optparse
4 import os
5 import re
6 import subprocess
7 import sys
8 import urllib
10 def buildparser():
11 usage = "Usage: %prog [-e] [-i] [-l]"
12 parser = optparse.OptionParser(usage=usage)
14 parser.add_option("-e", "--ext", action="store_true", dest="ext",
15 help="show external IP",
16 default=False)
18 parser.add_option("-i", "--int", action="store_true", dest="int",
19 help="show internal IP",
20 default=False)
22 parser.add_option("-l", "--lan", action="store_true", dest="lan",
23 help="show wlan ESSID. Default if no args",
24 default=False)
26 return parser
29 def getasfile(name, pth=""):
30 if sys.platform.startswith('linux'):
31 if os.path.exists('/sbin/' + name):
32 pth = '/sbin/' + name
33 elif os.path.exists('/usr/sbin/' + name):
34 pth = '/usr/sbin/' + name
35 if pth:
36 proc = subprocess.Popen(pth, stdout=subprocess.PIPE,
37 stderr=subprocess.PIPE)
38 return proc.communicate()[0].split('\n')
39 else:
40 return ""
42 def intip():
43 for line in getasfile('ifconfig'):
44 if re.search('Bcast:', line):
45 line = line.split(':')
46 return line[1].split(' ')[0]
48 def extip():
49 x = urllib.urlopen('http://ip.cutup.org/')
50 return x.readline()
52 def essid():
53 for line in getasfile('iwconfig'):
54 if (line.startswith('eth') and re.search('ESSID', line)
55 and not re.search('NOT READY', line)):
56 line = line.split(r'"')
57 return line[1]
59 if "__main__" == __name__:
60 parser = buildparser()
61 opts, args = parser.parse_args()
63 if len(sys.argv) < 2:
64 opts.lan = True
65 if opts.lan:
66 print essid()
67 if opts.int:
68 print intip()
69 if opts.ext:
70 print extip()