commit
c39002103a
2 changed files with 92 additions and 0 deletions
@ -0,0 +1,17 @@ |
|||||
|
netconnect is a simple helper for configuring networks on OpenBSD. |
||||
|
|
||||
|
While the existing hostname.if(5) configuration mechanism works |
||||
|
great for relatively static networks (as in always the same set |
||||
|
of networks, obviously it works fine on DHCP), it doesn't handle |
||||
|
the use case of connecting to different wireless networks very well. |
||||
|
|
||||
|
netconnect intentionally avoids direct hardware interfaces. All |
||||
|
manipulation of network state is performed through ifconfig(8). |
||||
|
This allows netconnect to work seamlessly on a wide range of |
||||
|
hardware. |
||||
|
|
||||
|
TODO: |
||||
|
- Stored network configs |
||||
|
- Handle other interface configs |
||||
|
- WPA Enterprise? |
||||
|
- Hidden SSIDs? |
||||
@ -0,0 +1,75 @@ |
|||||
|
#!/usr/local/bin/python2.7 |
||||
|
from subprocess import check_output |
||||
|
from subprocess import call |
||||
|
from re import search |
||||
|
from re import split |
||||
|
import sys |
||||
|
|
||||
|
wifi_if="iwm0" |
||||
|
eth_if="em0" |
||||
|
trunk_if="trunk0" |
||||
|
|
||||
|
nwlist = check_output(["ifconfig", wifi_if, "scan"]).split("\n") |
||||
|
networks = [] |
||||
|
nw_num = 0 |
||||
|
for line in nwlist: |
||||
|
if search("%",line): |
||||
|
cleaned = line.lstrip().split(" ") |
||||
|
name = [] |
||||
|
chan_pos = 0 |
||||
|
for term in cleaned: |
||||
|
if term == "nwid": |
||||
|
chan_pos+=1 |
||||
|
continue |
||||
|
elif term == "chan": |
||||
|
break |
||||
|
else: |
||||
|
chan_pos+=1 |
||||
|
name.append(term) |
||||
|
security = "Open" |
||||
|
for feature in cleaned[chan_pos+6].split(","): |
||||
|
if feature == "wep" or search("wpa",feature): |
||||
|
security = feature |
||||
|
nw_num+=1 |
||||
|
nwconfig = { "number": nw_num, "name": " ".join(name), "chan": cleaned[chan_pos+1], "signal": cleaned[chan_pos+4], "speed": cleaned[chan_pos+5], "security": security } |
||||
|
networks.append(nwconfig) |
||||
|
print "Found the following networks on interface %s:" % wifi_if |
||||
|
for network in networks: |
||||
|
print "%s) %s - %s (%s, %s, %s)" % (network["number"], network["name"], network["security"], network["signal"], network["speed"], network["chan"]) |
||||
|
print "%s) Just try bringing up the wired connection." % str(int(nw_num)+1) |
||||
|
selection = raw_input("Please select a network: ") |
||||
|
if selection.isdigit(): |
||||
|
print "Selection: %s" % selection |
||||
|
print "Max Network Number: %s" % str(int(nw_num)+1) |
||||
|
selection = int(selection) |
||||
|
if selection <= nw_num: |
||||
|
for network in networks: |
||||
|
if network["number"] == selection: |
||||
|
arguments = ["ifconfig", wifi_if, "up", "nwid", network["name"], "chan", network["chan"]] |
||||
|
if network["security"] != "Open": |
||||
|
key = raw_input("Please input the security key for this network: ") |
||||
|
if network["security"] == "wep": |
||||
|
arguments.extend(["nwkey", key]) |
||||
|
else: |
||||
|
arguments.extend(["wpa", "wpaakms", "psk", "wpakey", key]) |
||||
|
result = call(arguments) |
||||
|
if result != 0: |
||||
|
print "It appears we have failed to connect to the network - ifconfig exit code: %s" % result |
||||
|
sys.exit(result) |
||||
|
if selection <= int(nw_num)+1: |
||||
|
result = call(["ifconfig", eth_if, "up"]) |
||||
|
if result != 0: |
||||
|
print "It appears we have failed to bring up the wired interface - ifconfig exit code: %s" % result |
||||
|
sys.exit(result) |
||||
|
result = call(["ifconfig", trunk_if, "up", "trunkproto", "failover", "trunkport", eth_if, "trunkport", wifi_if]) |
||||
|
if result != 0: |
||||
|
print "It appears we have failed to bring up the trunk interface - ifconfig exit code: %s" % result |
||||
|
sys.exit(result) |
||||
|
result = call(["dhclient", trunk_if]) |
||||
|
if result != 0: |
||||
|
print "It appears we have failed to acquire an IP address on any interfaces - dhclient exit code: %s" % result |
||||
|
sys.exit(result) |
||||
|
if selection > int(nw_num)+1: |
||||
|
print "C'mon, man. Get your shit together." |
||||
|
else: |
||||
|
print "C'mon, man. Get your shit together." |
||||
Loading…
Reference in new issue