Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: akkana/netutils
base: 412186c573
...
head fork: akkana/netutils
compare: 0c5a8c5f7c
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Commits on Mar 09, 2013
@akkana Chown the "current" file to the logged-in user. 3c40023
Commits on Mar 10, 2013
@akkana Only delete the current scheme file when changing
to a different scheme.
Add debugging messages and comments to try to solve the problem
of why switching from eth0 to wlan0 doesn't work.
0c5a8c5
Showing with 66 additions and 12 deletions.
  1. +48 −11 netscheme
  2. +18 −1 netutils.py
View
59 netscheme
@@ -6,6 +6,7 @@
#
import sys, os
+import pwd
import subprocess
import signal
import re
@@ -165,10 +166,10 @@ auto %s""" % (iface.name)
if not add :
netutils.ifdown_all()
- print "Calling ifconfig up"
iface.ifconfig_up()
if not iface.wireless :
+ print "It's not wireless"
pass
elif self.encryption == "wep" or self.encryption == "open" :
# iwconfig doesn't work reliably on some systems unless you
@@ -286,7 +287,8 @@ psk="%s"
# Figure out what interface we'll be using:
iface = None
all = netutils.get_interfaces()
- # find the first wireless interface
+ # Find the first wireless interface if the scheme is wireless,
+ # else the first wired.
for i in all :
print "Trying interface", i.name
if i.wireless: print i.name, "is wireless"
@@ -304,6 +306,7 @@ psk="%s"
if not iface :
print "Couldn't find an interface to set the scheme"
return
+ print "Using interface", iface
# Are we on debian?
is_deb = is_debian()
@@ -360,9 +363,10 @@ psk="%s"
# in particular, if more than one interface is up, make sure
# there's no wifi default route blocking on on a wired interface.
up_ifaces = netutils.get_interfaces(True)
+ print "Up interfaces now:", up_ifaces
if len(up_ifaces) > 0 :
rt = netutils.Route.read_route_table()
- # print "Route table:", rt
+ print "Route table:", rt
defaults = []
for r in rt :
if r.dest == 'default' or r.dest == '0.0.0.0' :
@@ -496,20 +500,53 @@ def find_and_set_scheme(newscheme, add=False, make_persistent=False) :
# Found it; scheme is a NetScheme object.
scheme.set_scheme(add)
- config_dir = os.path.expanduser("~/.config/netscheme")
- current_file = os.path.join(config_dir, "current")
+ config_dir = os.path.expanduser("~/.config")
+ netscheme_dir = os.path.join(config_dir, "netscheme")
+ current_file = os.path.join(netscheme_dir, "current")
# If specified, make this the default scheme, used for netscheme -r
if make_persistent :
- if not os.access(config_dir, os.W_OK) :
- os.makedirs(config_dir)
- fp = open(current_file, "w")
+ if os.path.exists(current_file) :
+ # If the file is already there, assume it has the right
+ # ownership, and just rewrite the contents:
+ fp = open(current_file, "w")
+ else :
+ # If the file, or its two parent directories, don't exist,
+ # we have to create them, then chown them to the user
+ # (since we're running as root).
+ # Amazingly, there's no way under sudo python to get the
+ # real UID. But we can get the logged-in username,
+ # then get the UID from that:
+ sudo_user = os.getenv("SUDO_USER")
+ if sudo_user :
+ pw = pwd.getpwnam(sudo_user)
+ uid = pw.pw_uid
+ print "Pre-sudo UID is", uid
+ else :
+ uid = os.getuid()
+
+ if not os.path.exists(config_dir) :
+ print "Making", config_dir
+ os.mkdir(config_dir)
+ os.chown(config_dir, uid, -1)
+ if not os.path.exists(netscheme_dir) :
+ print "Making", netscheme_dir
+ os.mkdir(netscheme_dir)
+ os.chown(netscheme_dir, uid, -1)
+ fp = open(current_file, "w")
+ os.chown(current_file, uid, -1)
+
print >>fp, scheme.name
fp.close()
- # Otherwise make sure there is no default scheme.
- elif os.access(current_file, os.W_OK) :
- os.remove(current_file)
+ # Otherwise, if there's a default scheme and it's different
+ # from the one we're switching to, clear it.
+ elif os.access(current_file, os.R_OK & os.W_OK) :
+ fp = open(current_file)
+ filescheme = fp.readline().strip()
+ fp.close()
+ if filescheme != newscheme :
+ os.remove(current_file)
return
View
19 netutils.py
@@ -89,13 +89,29 @@ def reload(self) :
def ifconfig_up(self) :
"""Mark the interface UP with ifconfig"""
+
+ # Okay, I have no idea what's going on here.
+ # If I set an eth0 scheme (which works correctly),
+ # then later try to set a wlan0 WPA scheme,
+ # when we get here, all interfaces are properly down.
+ # ifconfig -a shows that.
+ # Then we call ifconfig wlan0 up,
+ # and immediately after that, another ifconfig -a
+ # shows that both wlan0 and eth0 have been marked up.
+ # How do we mark wlan0 up without bringing eth0 with it?
+ # Running ifconfig wlan0 up by hand doesn't do that.
+
+ print "Before calling ifconfig_up", self.name, ", ifconfig -a looks like:"
+ subprocess.call(["ifconfig", "-a"])
+ print "Calling ifconfig", self.name, "up"
subprocess.call(["ifconfig", self.name, "up"])
+ print "After calling ifconfig up, ifconfig -a looks like:"
+ subprocess.call(["ifconfig", "-a"])
def ifconfig_down(self) :
"""Mark the interface DOWN with ifconfig"""
subprocess.call(["ifconfig", self.name, "down"])
self.reload()
- subprocess.call(["ifconfig", "-a"])
class Connection :
def __init__(self, iface=None) :
@@ -587,6 +603,7 @@ def ifdown_all() :
"""
print "ifdown_all"
up_ifaces = get_interfaces(True)
+ print "Taking up interfaces down:", up_ifaces
# Kill DHCP and wpa_supplicant.
# In theory apparently it's better to stop wpa_supplicant with

No commit comments for this range

Something went wrong with that request. Please try again.