a script for managing locations -- improve it?

Matt Price matt.price-H217xnMUJC0sA/PxXw9srA at public.gmane.org
Tue Jan 9 21:53:13 UTC 2007


Hi,

I just wrote a silly little script to help me manage my laptop
locaitons.  Nothing else I'd tried was really working for me.  

Anyway, it works well for me, but I'd like to get it to a stage where it
can be used by other people... except that I really have to stop fooling
around and do some work.  So I thought I'd just post it here and see if
anyone finds it useful/has ideas to make it better.

THis is it -- I call it set_locations.py and run it as root...  It
basically assumes you're using postfix and cupsys, but I hope it could
be modified to be more ecumenical

#!/usr/bin/python

import sys, os

# define the relevant system calls
NMINIT="/etc/dbus-1/event.d/25NetworkManager "
SENDMAIL="postfix"
PRINTCONF="lpoptions -d "
# haven't tested vpn
VPN="/etc/init.d/openvpn "

class Location (object):
    """ THe class is the main object type in our little script
    """        
    def __init__ (self, attributes, name):
        #print attributes
        self.controlled_ifaces=attributes['controlled_ifaces']
        self.nmstatus=attributes['nmstatus']
        self.smtphost=attributes['smtphost']
        self.defaultprinter=attributes['defaultprinter']
        self.vpn=attributes['vpn']
##        print x,v
##            self[x.__str__()]=v
        self.name=name
        #print dir(self)
        


# locations have a bunch of attributes.  You should define your own 
# based on the examples below    
home=Location({
                'controlled_ifaces'    : {'eth0'  : "",
                                        'eth1'  : ""
                                        },
                'defaultprinter'       :  "HL-1440",
                'nmstatus'             :  1,
                'vpn'                  :  0,
                'smtphost'              :  "smtp.broadband.rogers.com",
                'name'              : 'home'
                },
                'home')

linuxcaffe=Location({
                'controlled_ifaces'    : {'eth0'  : "",
                                        'eth1'  : ""
                                        },
                'defaultprinter'       :  "HL-1440",
                'nmstatus'             :  1,
                'vpn'                  :  0,
                'smtphost'              :  "",
                'name'              : 'linuxcaffe'
                },
                'linuxcaffe')
utor=Location({
                'controlled_ifaces'    : {'eth0'  : "utor",
                                        'eth1'  : ""
                                        },
                'defaultprinter'       :  "SAMBA",
                'nmstatus'             :  0,
                'vpn'                  :  0,
                'smtphost'              :
"postoffice126.utcc.utoronto.ca",
                'name'              : 'utor'
                },
                'utor')
utorwireless=Location({
                'controlled_ifaces'    : {'eth0 ' : "",
                                        'eth1'  : ""
                                        },
                'defaultprinter'       :  "SAMBA",
                'nmstatus'             :  1,
                'vpn'                  :  0,
                'smtphost'              :
"postoffice126.utcc.utoronto.ca",
                'name'              : 'utorwireless'
                },
                'utorwireless')

roaming=Location({
                'controlled_ifaces'    : {'eth0 ' : "",
                                        'eth1'  : ""
                                        },
                'defaultprinter'       :  "SAMBA",
                'nmstatus'             :  1,
                'vpn'                  :  0,
                'smtphost'              :
"postoffice126.utcc.utoronto.ca",
                'name'              : 'roaming'
                },
                'roaming')   

DefaultLocation=home

def SetLocation (location):
    print "Using Location " + location.name
    # bring down all ifaces
    print "bringing down old interfaces"
    os.system(NMINIT + " stop")
    os.system("ifdown eth1")
    os.system("ifdown eth0")
    
    # bring network services back up
    print "bringing up network interfaces"
    for iface,name in location.controlled_ifaces.items():
        #print iface, name
        if name:
            print "making the system call"
            cmd="ifup "+ iface + "=" + name
            print "cmd: ", cmd
            # problem here:  if the value is wrong 
            # the process stalls
            # needs a timeout control
            os.system("ifup "+ iface + "=" + name)
    if location.nmstatus:
        print "bringing up network-manager"
        os.system(NMINIT + " start")
    # reset postfix and printer
    if location.smtphost:
        print "Changing SMTP host..."
        if SENDMAIL=="postfix":
            os.system("postconf -e relayhost=" + location.smtphost)
            os.system("/etc/init.d/postfix restart")
    # reset default printer
    if location.defaultprinter:
        print "Resetting default printer"
        if PRINTCONF=="lpoptions -d ":
            os.system(PRINTCONF + location.defaultprinter)
        if location.vpn:
            os.system(VPN + " start")
        else:
            os.system(VPN + " stop")


if __name__ == "__main__":
    if len(sys.argv)>1:
        # stupid brute-force method, I hate that
        import gc
        for obj in gc.get_objects():
            if isinstance(obj, Location):
                #print obj.name
                if obj.name==sys.argv[1]:
                    SetLocation(obj)    
            #SetLocation(sys.argv[1])

    else: SetLocation(DefaultLocation)
    
--------------------
have fun,

matt

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://gtalug.org/pipermail/legacy/attachments/20070109/f187221d/attachment.sig>


More information about the Legacy mailing list