3 # Author: Martin Matusiak <numerodix@gmail.com>
4 # Licensed under the GNU Public License, version 3.
6 # <desc> Dumb network connection manager </desc>
11 from conf
import config
14 from output
import logger
24 # make sure we are the only instance
25 procs
.kill_previous_instances()
27 # read command line options
31 if config
.opts
.checktools
:
34 # kill switch was triggered
38 # run in foreground if we want debug mode
39 if not config
.opts
.debug
and not config
.opts
.wardrive
:
43 if config
.opts
.dropip
or config
.opts
.wardrive
:
44 [network
.iface_down(i
.interface
) for i
in config
.interfaces
]
46 # write our pid to the pidfile
51 p
= opts
.create_parser()
52 p
.add_option("-i", "--interface", dest
="interface", action
="append",
53 metavar
="iface", help="Prefer this interface")
54 p
.add_option("-n", "--network", dest
="network", action
="append",
55 metavar
="name[,key]", help="Prefer this wireless network")
56 p
.add_option("-w", "--wardrive", action
="store_true",
57 help="Attempt to connect to any open network in range")
58 p
.add_option("-d", "--debug", action
="store_true",
59 help="Run in foreground (debug mode)")
60 p
.add_option("-D", "--dropip", action
="store_true",
61 help="Release existing ip")
62 p
.add_option("-C", "--checktools", action
="store_true",
63 help="Do sanity check (check for missing tools)")
64 p
.add_option("-k", "--kill", action
="store_true",
65 help="Stop %s" % config
.program_name
)
69 def obtain_ip(wardrive
=None):
71 # make current state be previous state
72 config
.had_ip
= config
.have_ip
75 if config
.have_ip
: logger
.mute()
81 else: config
.have_ip
= False
83 # if we had an ip on the previous run, but we just discovered it's gone,
84 # we need to toggle screen output and re-run ip check
85 if config
.had_ip
and not config
.have_ip
: return
89 prefer_wired
= ui
.choose_medium()
91 if prefer_wired
and ui
.check_wired_link():
92 # find top prority wired interface
93 iface
= config
.interfaces
.get_top(pred
=lambda x
: x
.medium
== "wired")
94 if ui
.request_ip(iface
): return
96 # find top prority wireless interface
97 iface
= config
.interfaces
.get_top(pred
=lambda x
: x
.medium
== "wireless")
98 wifi_nets
= ui
.scan_wifi_networks(iface
, wardrive
=wardrive
)
101 for net
in wifi_nets
:
102 if ui
.request_ip(iface
, net
=net
): return
108 obtain_ip(wardrive
=config
.opts
.wardrive
)
113 if __name__
== "__main__":