From ad1fdb0201c057a51db20b08470e806926f7edb2 Mon Sep 17 00:00:00 2001 From: Robby Workman Date: Thu, 5 Nov 2009 23:20:36 -0600 Subject: [PATCH] Initial commit of Ronuk Raval's wicd_cli branch into the main 1.6 codebase. This is still untested, so depending on any problems found, and assuming it eventually makes into the main branch, we might rebase it before the merge. --- cli/README | 50 +++++++++ cli/wicd-cli.py | 234 +++++++++++++++++++++++++++++++++++++++++ in/man=wicd-cli.8.in | 7 ++ in/scripts=wicd-cli.in | 2 + setup.py | 6 ++ 5 files changed, 299 insertions(+) create mode 100644 cli/README create mode 100644 cli/wicd-cli.py create mode 100644 in/man=wicd-cli.8.in create mode 100755 in/scripts=wicd-cli.in diff --git a/cli/README b/cli/README new file mode 100644 index 0000000..f7e9aaa --- /dev/null +++ b/cli/README @@ -0,0 +1,50 @@ +wicd-cli is a scriptable command-line only "client" for wicd written +by Ronuk Raval. + +Some usage examples for wicd-cli.py + +Scan for new wireless networks and display them: + python wicd-cli.py --wireless --scan --list-networks +or (same thing, short version) + python wicd-cli.py -y -S -l + +Or, you can view the networks Wicd has in its cache: + python wicd-cli.py --wireless --list-networks + +If you want to do anything with these networks, you''ll need to note the +network ID listed on the left side of the above command. + +Get the channel number from network 0: + python wicd-cli.py --wireless --network 0 --network-property channel + +Or get all available information for a certain network, do: + python wicd-cli.py --wireless --network 0 --network-details + +Or do the same for the currently connected network, do: + python wicd-cli.py --wireless --network-details + +View the available encryption templates Wicd can use: + python wicd-cli.py --wireless --list-encryption-types + +Look under your chosen encryption scheme for a list of required properties +(marked with "Req:"). These are additional properties that must be set before +the encryption can be used. + +For example, the listing for WPA encryption looks as follows: + 0 wpa WPA 1/2 (Passphrase) + Req: key (Key) +The list indicates that to use WPA encryption, the following properties must be +set: + 'enctype' => 'wpa' (from the name field) + 'key' => YOUR_WPA_PASSKEY (from the required field) + +Use the WPA encryption scheme on network 0 by issuing the following two +commands: + python wicd-cli.py --wireless --network 0 \ + --network-property enctype --set-to wpa + python wicd-cli.py --wireless --network 0 \ + --network-property key --set-to YOUR_WPA_PASSKEY + +Finally, we need to connect to the network: + python wicd-cli.py --wireless --network 0 --connect + diff --git a/cli/wicd-cli.py b/cli/wicd-cli.py new file mode 100644 index 0000000..69afd5d --- /dev/null +++ b/cli/wicd-cli.py @@ -0,0 +1,234 @@ +#!/usr/bin/python + +# 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. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. + +import optparse +import dbus +import dbus.service +import sys +from wicd import misc + +misc.RenameProcess('wicd-cli') + +if getattr(dbus, 'version', (0, 0, 0)) < (0, 80, 0): + import dbus.glib +else: + from dbus.mainloop.glib import DBusGMainLoop + DBusGMainLoop(set_as_default=True) + +bus = dbus.SystemBus() +try: + daemon = dbus.Interface(bus.get_object('org.wicd.daemon', '/org/wicd/daemon'), + 'org.wicd.daemon') + wireless = dbus.Interface(bus.get_object('org.wicd.daemon', '/org/wicd/daemon/wireless'), + 'org.wicd.daemon.wireless') + wired = dbus.Interface(bus.get_object('org.wicd.daemon', '/org/wicd/daemon/wired'), + 'org.wicd.daemon.wired') + config = dbus.Interface(bus.get_object('org.wicd.daemon', '/org/wicd/daemon/config'), + 'org.wicd.daemon.config') +except dbus.DBusException: + print 'Error: Could not connect to the daemon. Please make sure it is running.' + sys.exit(3) + +parser = optparse.OptionParser() + +parser.add_option('--network', '-n', type='int', default=-1) +parser.add_option('--network-property', '-p') +parser.add_option('--set-to', '-s') +parser.add_option('--name', '-m') + +parser.add_option('--scan', '-S', default=False, action='store_true') +parser.add_option('--save', '-w', default=False, action='store_true') +parser.add_option('--list-networks', '-l', default=False, action='store_true') +parser.add_option('--network-details', '-d', default=False, action='store_true') +parser.add_option('--disconnect', '-x', default=False, action='store_true') +parser.add_option('--connect', '-c', default=False, action='store_true') +parser.add_option('--list-encryption-types', '-e', default=False, action='store_true') +# short options for these two aren't great. +parser.add_option('--wireless', '-y', default=False, action='store_true') +parser.add_option('--wired', '-z', default=False, action='store_true') +parser.add_option('--load-profile', '-o', default=False, action='store_true') + +options, arguments = parser.parse_args() + +op_performed = False + +if not (options.wireless or options.wired): + print "Please use --wireless or --wired to specify " + \ + "the type of connection to operate on." + +# functions +def is_valid_wireless_network_id(network_id): + if not (network_id >= 0 \ + and network_id < wireless.GetNumberOfNetworks()): + print 'Invalid wireless network identifier.' + sys.exit(1) + +def is_valid_wired_network_id(network_id): + num = len(config.GetWiredProfileList()) + if not (network_id < num and \ + network_id >= 0): + print 'Invalid wired network identifier.' + sys.exit(4) + +def is_valid_wired_network_profile(profile_name): + if not profile_name in config.GetWiredProfileList(): + print 'Profile of that name does not exist.' + sys.exit(5) + +if options.scan and options.wireless: + # synchronized scan + wireless.Scan(True) + op_performed = True + +if options.load_profile and options.wired: + is_valid_wired_network_profile(options.name) + config.ReadWiredNetworkProfile(options.name) + op_performed = True + +if options.list_networks: + if options.wireless: + print '#\tBSSID\t\t\tChannel\tESSID' + for network_id in range(0, wireless.GetNumberOfNetworks()): + print '%s\t%s\t%s\t%s' % (network_id, + wireless.GetWirelessProperty(network_id, 'bssid'), + wireless.GetWirelessProperty(network_id, 'channel'), + wireless.GetWirelessProperty(network_id, 'essid')) + elif options.wired: + print '#\tProfile name' + id = 0 + for profile in config.GetWiredProfileList(): + print '%s\t%s' % (id, profile) + id += 1 + op_performed = True + +if options.network_details: + if options.wireless: + if options.network >= 0: + is_valid_wireless_network_id(options.network) + network_id = options.network + else: + network_id = wireless.GetCurrentNetworkID(0) + is_valid_wireless_network_id(network_id) + # we're connected to a network, print IP + print "IP: %s" % wireless.GetWirelessIP(0) + + print "Essid: %s" % wireless.GetWirelessProperty(network_id, "essid") + print "Bssid: %s" % wireless.GetWirelessProperty(network_id, "bssid") + if wireless.GetWirelessProperty(network_id, "encryption"): + print "Encryption: On" + print "Encryption Method: %s" % \ + wireless.GetWirelessProperty(network_id, "encryption_method") + else: + print "Encryption: Off" + print "Quality: %s" % wireless.GetWirelessProperty(network_id, "quality") + print "Mode: %s" % wireless.GetWirelessProperty(network_id, "mode") + print "Channel: %s" % wireless.GetWirelessProperty(network_id, "channel") + print "Bit Rates: %s" % wireless.GetWirelessProperty(network_id, "bitrates") + op_performed = True + +# network properties + +if options.network_property: + if options.wireless: + is_valid_wireless_network_id(options.network) + if not options.set_to: + print wireless.GetWirelessProperty(options.network, options.network_property) + else: + wireless.SetWirelessProperty(options.network, \ + options.network_property, options.set_to) + elif options.wired: + if not options.set_to: + print wired.GetWiredProperty(options.network_property) + else: + wired.SetWiredProperty(options.network_property, options.set_to) + op_performed = True + +if options.disconnect: + daemon.Disconnect() + if options.wireless: + if wireless.GetCurrentNetworkID(0) > -1: + print "Disconnecting from %s on %s" % (wireless.GetCurrentNetwork(0), + wireless.DetectWirelessInterface()) + elif options.wired: + if wired.CheckPluggedIn(): + print "Disconnecting from wired connection on %s" % wired.DetectWiredInterface() + op_performed = True + +if options.connect: + if options.wireless and options.network > -1: + is_valid_wireless_network_id(options.network) + name = wireless.GetWirelessProperty(options.network, 'essid') + encryption = wireless.GetWirelessProperty(options.network, 'enctype') + print "Connecting to %s with %s on %s" % (name, encryption, + wireless.DetectWirelessInterface()) + wireless.ConnectWireless(options.network) + + check = lambda: wireless.CheckIfWirelessConnecting() + message = lambda: wireless.CheckWirelessConnectingMessage() + elif options.wired: + print "Connecting to wired connection on %s" % wired.DetectWiredInterface() + wired.ConnectWired() + + check = lambda: wired.CheckIfWiredConnecting() + message = lambda: wired.CheckWiredConnectingMessage() + + # update user on what the daemon is doing + last = None + while check(): + next = message() + if next != last: + # avoid a race condition where status is updated to "done" after the + # loop check + if next == "done": + break + print "%s..." % next.replace("_", " ") + last = next + print "done!" + op_performed = True + +# pretty print optional and required properties +def str_properties(prop): + if len(prop) == 0: + return "None" + else: + return ', '.join("%s (%s)" % (x[0], x[1].replace("_", " ")) for x in type['required']) + +if options.wireless and options.list_encryption_types: + et = misc.LoadEncryptionMethods() + # print 'Installed encryption templates:' + print '%s\t%-20s\t%s' % ('#', 'Name', 'Description') + id = 0 + for type in et: + print '%s\t%-20s\t%s' % (id, type['type'], type['name']) + print ' Req: %s' % str_properties(type['required']) + print '---' + # don't print optionals (yet) + #print ' Opt: %s' % str_properties(type['optional']) + id += 1 + op_performed = True + +if options.save and options.network > -1: + if options.wireless: + is_valid_wireless_network_id(options.network) + config.SaveWirelessNetworkProfile(options.network) + elif options.wired: + config.SaveWiredNetworkProfile(options.name) + op_performed = True + +if not op_performed: + print "No operations performed." + diff --git a/in/man=wicd-cli.8.in b/in/man=wicd-cli.8.in new file mode 100644 index 0000000..100ed5e --- /dev/null +++ b/in/man=wicd-cli.8.in @@ -0,0 +1,7 @@ +.\" First revision was r??? +.TH WICD-CCLI "6" "November 2009" +.SH NAME +.B wicd-cli +\- commandline- only wicd(8) controller +.SH DESCRIPTION +TODO :-) diff --git a/in/scripts=wicd-cli.in b/in/scripts=wicd-cli.in new file mode 100755 index 0000000..584cf45 --- /dev/null +++ b/in/scripts=wicd-cli.in @@ -0,0 +1,2 @@ +#!/bin/bash +exec python -O %LIB%wicd-cli.py $@ diff --git a/setup.py b/setup.py index fbdd0c6..060d97a 100755 --- a/setup.py +++ b/setup.py @@ -98,6 +98,7 @@ class configure(Command): ('no-install-pmutils', None, 'do not install the pm-utils hooks'), ('no-install-docs', None, 'do not install the auxiliary documentation'), ('no-install-ncurses', None, 'do not install the ncurses client'), + ('no-install-cli', None, 'do not install the command line executable'), ('no-use-notifications', None, 'do not ever allow the use of libnotify notifications') ] @@ -134,6 +135,7 @@ class configure(Command): self.no_install_pmutils = False self.no_install_docs = False self.no_install_ncurses = False + self.no_install_cli = False self.no_use_notifications = False # Determine the default init file location on several different distros @@ -505,6 +507,10 @@ try: data.append((wpath.bin, ['scripts/wicd-curses'])) if not wpath.no_install_man: data.append(( wpath.mandir + 'man8/', ['man/wicd-curses.8'])) + if not wpath.no_install_cli: + data.append((wpath.bin, ['scripts/wicd-cli'])) + if not wpath.no_install_man: + data.append(( wpath.mandir + 'man8/', ['man/wicd-cli.8'])) piddir = os.path.dirname(wpath.pidfile) if not piddir.endswith('/'): piddir += '/'