Created
July 1, 2010 22:01
-
-
Save Daenyth/460632 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# | |
# Network configuration tool for NetworkManager | |
# Copyright (C) 2010 TUBITAK/UEKAE | |
# | |
# This program is free software; you can redistribute it and/or modify it | |
# under the terms of the GNU General Public License as published by the | |
# Free Software Foundation; either version 2 of the License, or (at your | |
# option) any later version. Please read the COPYING file. | |
# | |
import sys | |
import dbus | |
import uuid | |
from networkmanager import * | |
from networkmanager.ipaddr import * | |
use_color = True | |
# Color characters | |
colors = {'red' : '\x1b[31;01m', | |
'blue' : '\x1b[34;01m', | |
'cyan' : '\x1b[36;01m', | |
'gray' : '\x1b[30;01m', | |
'green' : '\x1b[32;01m', | |
'light' : '\x1b[37;01m', | |
'yellow' : '\x1b[33;01m', | |
'magenta' : '\x1b[35;01m', | |
'reddark' : '\x1b[31;0m', | |
'bluedark' : '\x1b[34;0m', | |
'cyandark' : '\x1b[36;0m', | |
'graydark' : '\x1b[30;0m', | |
'greendark' : '\x1b[32;0m', | |
'magentadark': '\x1b[35;0m', | |
'normal' : '\x1b[0m'} | |
def colorize(msg, color): | |
global use_color | |
if not use_color: | |
return msg | |
else: | |
return "%s%s%s" % (colors[color], msg, colors['normal']) | |
def print_usage(): | |
print "Usage: %s <command> <option>" % sys.argv[0] | |
print "Commands:" | |
print " connections Show connections" | |
print " devices Show devices" | |
print " create Create profile" | |
print " edit Edit profile" | |
print " delete Delete profile" | |
print " up <profile> Bring profile up" | |
print " down <profile> Bring profile down" | |
print "Options:" | |
print " --no-color Don't colorize output" | |
def get_input(label): | |
try: | |
return raw_input(colorize(("%s > " % label), 'light')) | |
except (KeyboardInterrupt, EOFError): | |
sys.exit(1) | |
def get_number(label, min_, max_): | |
index_ = min_ - 1 | |
while index_ < min_ or index_ > max_: | |
try: | |
index_ = int(raw_input(colorize(("%s > " % label), 'light'))) | |
except ValueError: | |
pass | |
except (KeyboardInterrupt, EOFError): | |
sys.exit(1) | |
return index_ | |
def print_connections(nm_handle): | |
connections = { | |
'802-11-wireless' : [], | |
'802-3-ethernet' : [], | |
'cdma' : [], | |
'gsm' : [], | |
'unknown' : [], | |
} | |
for connection in nm_handle.connections: | |
connections[connection.settings.type] += [connection] | |
for connection_key in connections.keys(): | |
if len(connections[connection_key]) > 0: | |
conn_interface = connection_key.split("-").pop().capitalize() | |
print colorize("%s profiles" % conn_interface, 'green') | |
for profile in connections[connection_key]: | |
for active in nm_handle.active_connections: | |
if active.connection.settings.id == profile.settings.id: | |
state_mark = "X" | |
else: | |
state_mark = " " | |
print "[%s] %s%s %s" % (colorize(state_mark, 'green'), colorize(profile.settings.id, 'cyan'), "","") | |
def print_devices(nm_handle): | |
devices = { | |
DeviceType.UNKNOWN : [], | |
DeviceType.WIFI : [], | |
DeviceType.GSM : [], | |
DeviceType.CDMA : [], | |
DeviceType.ETHERNET : [], | |
} | |
for device in nm_handle.devices: | |
devices[device.type] += [device] | |
for device_key in devices.keys(): | |
if len(devices[device_key]) > 0: | |
print colorize("%s devices" % str(device_key).capitalize(), 'green') | |
for device in devices[device_key]: | |
print " %s\t(%s)" % (device.driver,device.interface) | |
def delete_profile(nm): | |
connections = { | |
'802-11-wireless' : [], | |
'802-3-ethernet' : [], | |
'cdma' : [], | |
'gsm' : [], | |
'unknown' : [], | |
} | |
for connection in nm.connections: | |
connections[connection.settings.type] += [connection] | |
connection_list = [] | |
index_ = 1 | |
#Aşağıda yazdırdıktan sonra hangi interface in seçildiğini bulmak için | |
for connection_type in connections.keys(): | |
if len(connections[connection_type]) > 0: | |
conn_interface = connection_type.split("-").pop().capitalize() | |
print colorize("%s profiles" % conn_interface, 'green') | |
for connection in connections[connection_type] : | |
print " [%s] %s" % (index_, str(connection.settings.id).capitalize()) | |
connection_list += [connection] | |
index_ += 1 | |
connection = connection_list[(get_number("Delete", 1, index_ - 1) - 1)] | |
connection.delete() | |
def create_profile_ethernet(nm, _device) : | |
settings = None | |
settings = WiredSettings() | |
settings.uuid = str(uuid.uuid4()) | |
settings.device = _device | |
settings.mac_address = _device.hwaddress | |
print colorize("Select IP assignment method:", "yellow") | |
print " [1] Enter an IP address manually" | |
print " [2] Automatically obtain an IP address" | |
if get_number("Type", 1, 2) == 2 : | |
settings.set_auto() | |
else : | |
settings.address = getInput("Address") | |
settings.netmask = getInput("Mask") | |
settings.gateway = getInput("Gateway") | |
print colorize("Select Name server (DNS) assignment method:", "yellow") | |
print " [1] Use default name servers" | |
print " [2] Enter an name server address manually" | |
print " [3] Automatically obtain from DHCP" | |
dns = get_number("Type", 1, 3) | |
dns_address = "" | |
if dns == 1 : | |
dns_mode = "default" | |
dns_address = "" | |
elif dns == 2 : | |
dns_mode = "manual" | |
dns_address = get_input("Address") | |
settings.dns = dns_address | |
elif dns == 3 : | |
dns_mode = "auto" | |
dns_address = "" | |
profile = None | |
profiles = [] | |
for i in nm.connections : | |
profiles += [i.settings.id] | |
while not profile: | |
profile = get_input("Profile name").strip() | |
if profile in profiles: | |
print "There is already a profile named '%s'" % profile | |
profile = None | |
settings.id = profile | |
nm.add_connection(settings) | |
#possible_settings = ('dns','num','user','pass') | |
#for s in possible_settings: | |
# if s in params: | |
# settings.s = params[s] | |
def create_profile_wifi(nm_handle, _device): | |
pass | |
def create_profile(nm_handle) : | |
if len(nm_handle.devices) == 0 : | |
# No network devices | |
return | |
devices = { | |
DeviceType.UNKNOWN : [], | |
DeviceType.WIFI : [], | |
DeviceType.GSM : [], | |
DeviceType.CDMA : [], | |
DeviceType.ETHERNET : [], | |
} | |
functions = { | |
DeviceType.ETHERNET : create_profile_ethernet, | |
DeviceType.WIFI : create_profile_wifi, | |
} | |
for device in nm_handle.devices: | |
devices[device.type] += [device] | |
index_ = 1 | |
print colorize("Select interface:", "yellow") | |
device_list = [] | |
#Aşağıda yazdırdıktan sonra hangi interface in seçildiğini bulmak için | |
for interface in devices.keys(): | |
if len(devices[interface]) > 0: | |
print " [%s] %s" % (index_, str(interface).capitalize()) | |
device_list += [interface] | |
index_ += 1 | |
_max = 0 | |
for interface in devices.keys(): | |
_max += len(devices[interface]) | |
package_no = get_number("Interface", 1, _max) - 1 | |
index_ = 1 | |
_devices = devices[device_list[package_no]] | |
if len(_devices) > 0: | |
print colorize("Select device:", "yellow") | |
for device in _devices: | |
print " [%s] %s" % (index_, device) | |
index_ += 1 | |
device_no = get_number("Device", 1, _max) - 1 | |
functions[_devices[device_no].type](nm_handle, _devices[device_no]) | |
def state_profile(nm_handle, command) : | |
try : | |
profile = sys.argv[2] | |
except : | |
printUsage() | |
return 1 | |
def state_profile_down(nm_handle, profile) : | |
for active_conn in nm_handle.active_connections : | |
if active_conn.connection.settings.id == profile : | |
active_conn.devices[0].disconnect() | |
def state_profile_up(nm_handle, profile) : | |
for conn in nm_handle.connections : | |
if conn.settings.id == profile : | |
for device in nm_handle.devices : | |
if device.hwaddress == conn.settings.mac_address : | |
nm_handle.activate_connection(conn, device) | |
if command == "down" : | |
state_profile_down(nm_handle, profile) | |
elif command == "up" : | |
state_profile_up(nm_handle, profile) | |
##################### | |
### Main function ### | |
##################### | |
def main(): | |
try: | |
command = sys.argv[1] | |
except: | |
print_usage() | |
return 1 | |
if "--no-color" in sys.argv: | |
global use_color | |
use_color = False | |
sys.argv.remove("--no-color") | |
# Create NetworkManager handle | |
nm_handle = NetworkManager() | |
if command == "connections": | |
return print_connections(nm_handle) | |
elif command == "devices": | |
return print_devices(nm_handle) | |
elif command == "create": | |
return create_profile(nm_handle) | |
elif command == "delete": | |
return delete_profile(nm_handle) | |
elif command in ("up", "down"): | |
return state_profile(nm_handle, command) | |
else: | |
print_usage() | |
return 1 | |
return 0 | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment