PageRenderTime 62ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/cli/wicd-cli.py

https://github.com/cbxbiker61/wicd
Python | 242 lines | 184 code | 31 blank | 27 comment | 59 complexity | c3899f4eb576ee700475de5b0594aa5d MD5 | raw file
  1. #!/usr/bin/python
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2 of the License, or
  5. # (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program; if not, write to the Free Software
  14. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  15. # MA 02110-1301, USA.
  16. import optparse
  17. import dbus
  18. import dbus.service
  19. import sys
  20. from wicd import misc
  21. misc.RenameProcess('wicd-cli')
  22. if getattr(dbus, 'version', (0, 0, 0)) < (0, 80, 0):
  23. import dbus.glib
  24. else:
  25. from dbus.mainloop.glib import DBusGMainLoop
  26. DBusGMainLoop(set_as_default=True)
  27. bus = dbus.SystemBus()
  28. try:
  29. daemon = dbus.Interface(bus.get_object('org.wicd.daemon', '/org/wicd/daemon'),
  30. 'org.wicd.daemon')
  31. wireless = dbus.Interface(bus.get_object('org.wicd.daemon', '/org/wicd/daemon/wireless'),
  32. 'org.wicd.daemon.wireless')
  33. wired = dbus.Interface(bus.get_object('org.wicd.daemon', '/org/wicd/daemon/wired'),
  34. 'org.wicd.daemon.wired')
  35. config = dbus.Interface(bus.get_object('org.wicd.daemon', '/org/wicd/daemon/config'),
  36. 'org.wicd.daemon.config')
  37. except dbus.DBusException:
  38. print 'Error: Could not connect to the daemon. Please make sure it is running.'
  39. sys.exit(3)
  40. parser = optparse.OptionParser()
  41. parser.add_option('--network', '-n', type='int', default=-1)
  42. parser.add_option('--network-property', '-p')
  43. parser.add_option('--set-to', '-s')
  44. parser.add_option('--name', '-m')
  45. parser.add_option('--scan', '-S', default=False, action='store_true')
  46. parser.add_option('--save', '-w', default=False, action='store_true')
  47. parser.add_option('--list-networks', '-l', default=False, action='store_true')
  48. parser.add_option('--network-details', '-d', default=False, action='store_true')
  49. parser.add_option('--disconnect', '-x', default=False, action='store_true')
  50. parser.add_option('--connect', '-c', default=False, action='store_true')
  51. parser.add_option('--list-encryption-types', '-e', default=False, action='store_true')
  52. # short options for these two aren't great.
  53. parser.add_option('--wireless', '-y', default=False, action='store_true')
  54. parser.add_option('--wired', '-z', default=False, action='store_true')
  55. parser.add_option('--load-profile', '-o', default=False, action='store_true')
  56. options, arguments = parser.parse_args()
  57. op_performed = False
  58. if not (options.wireless or options.wired):
  59. print "Please use --wireless or --wired to specify " + \
  60. "the type of connection to operate on."
  61. # functions
  62. def is_valid_wireless_network_id(network_id):
  63. if not (network_id >= 0 \
  64. and network_id < wireless.GetNumberOfNetworks()):
  65. print 'Invalid wireless network identifier.'
  66. sys.exit(1)
  67. def is_valid_wired_network_id(network_id):
  68. num = len(wired.GetWiredProfileList())
  69. if not (network_id < num and \
  70. network_id >= 0):
  71. print 'Invalid wired network identifier.'
  72. sys.exit(4)
  73. def is_valid_wired_network_profile(profile_name):
  74. if not profile_name in wired.GetWiredProfileList():
  75. print 'Profile of that name does not exist.'
  76. sys.exit(5)
  77. if options.scan and options.wireless:
  78. # synchronized scan
  79. wireless.Scan(True)
  80. op_performed = True
  81. if options.load_profile and options.wired:
  82. is_valid_wired_network_profile(options.name)
  83. config.ReadWiredNetworkProfile(options.name)
  84. op_performed = True
  85. if options.list_networks:
  86. if options.wireless:
  87. print '#\tBSSID\t\t\tChannel\tESSID'
  88. for network_id in range(0, wireless.GetNumberOfNetworks()):
  89. print '%s\t%s\t%s\t%s' % (network_id,
  90. wireless.GetWirelessProperty(network_id, 'bssid'),
  91. wireless.GetWirelessProperty(network_id, 'channel'),
  92. wireless.GetWirelessProperty(network_id, 'essid'))
  93. elif options.wired:
  94. print '#\tProfile name'
  95. id = 0
  96. for profile in wired.GetWiredProfileList():
  97. print '%s\t%s' % (id, profile)
  98. id += 1
  99. op_performed = True
  100. if options.network_details:
  101. if options.wireless:
  102. if options.network >= 0:
  103. is_valid_wireless_network_id(options.network)
  104. network_id = options.network
  105. else:
  106. network_id = wireless.GetCurrentNetworkID(0)
  107. is_valid_wireless_network_id(network_id)
  108. # we're connected to a network, print IP
  109. print "IP: %s" % wireless.GetWirelessIP(0)
  110. print "Essid: %s" % wireless.GetWirelessProperty(network_id, "essid")
  111. print "Bssid: %s" % wireless.GetWirelessProperty(network_id, "bssid")
  112. if wireless.GetWirelessProperty(network_id, "encryption"):
  113. print "Encryption: On"
  114. print "Encryption Method: %s" % \
  115. wireless.GetWirelessProperty(network_id, "encryption_method")
  116. else:
  117. print "Encryption: Off"
  118. print "Quality: %s" % wireless.GetWirelessProperty(network_id, "quality")
  119. print "Mode: %s" % wireless.GetWirelessProperty(network_id, "mode")
  120. print "Channel: %s" % wireless.GetWirelessProperty(network_id, "channel")
  121. print "Bit Rates: %s" % wireless.GetWirelessProperty(network_id, "bitrates")
  122. op_performed = True
  123. # network properties
  124. if options.network_property:
  125. options.network_property = options.network_property.lower()
  126. if options.wireless:
  127. if options.network >= 0:
  128. is_valid_wireless_network_id(options.network)
  129. network_id = options.network
  130. else:
  131. network_id = wireless.GetCurrentNetworkID(0)
  132. is_valid_wireless_network_id(network_id)
  133. if not options.set_to:
  134. print wireless.GetWirelessProperty(network_id, options.network_property)
  135. else:
  136. wireless.SetWirelessProperty(network_id, \
  137. options.network_property, options.set_to)
  138. elif options.wired:
  139. if not options.set_to:
  140. print wired.GetWiredProperty(options.network_property)
  141. else:
  142. wired.SetWiredProperty(options.network_property, options.set_to)
  143. op_performed = True
  144. if options.disconnect:
  145. daemon.Disconnect()
  146. if options.wireless:
  147. if wireless.GetCurrentNetworkID(0) > -1:
  148. print "Disconnecting from %s on %s" % (wireless.GetCurrentNetwork(0),
  149. wireless.DetectWirelessInterface())
  150. elif options.wired:
  151. if wired.CheckPluggedIn():
  152. print "Disconnecting from wired connection on %s" % wired.DetectWiredInterface()
  153. op_performed = True
  154. if options.connect:
  155. check = None
  156. if options.wireless and options.network > -1:
  157. is_valid_wireless_network_id(options.network)
  158. name = wireless.GetWirelessProperty(options.network, 'essid')
  159. encryption = wireless.GetWirelessProperty(options.network, 'enctype')
  160. print "Connecting to %s with %s on %s" % (name, encryption,
  161. wireless.DetectWirelessInterface())
  162. wireless.ConnectWireless(options.network)
  163. check = lambda: wireless.CheckIfWirelessConnecting()
  164. message = lambda: wireless.CheckWirelessConnectingMessage()[1]
  165. elif options.wired:
  166. print "Connecting to wired connection on %s" % wired.DetectWiredInterface()
  167. wired.ConnectWired()
  168. check = lambda: wired.CheckIfWiredConnecting()
  169. message = lambda: wired.CheckWiredConnectingMessage()
  170. # update user on what the daemon is doing
  171. last = None
  172. if check:
  173. while check():
  174. next = message()
  175. if next != last:
  176. # avoid a race condition where status is updated to "done" after the
  177. # loop check
  178. if next == "done":
  179. break
  180. print "%s..." % next.replace("_", " ")
  181. last = next
  182. print "done!"
  183. op_performed = True
  184. # pretty print optional and required properties
  185. def str_properties(prop):
  186. if len(prop) == 0:
  187. return "None"
  188. else:
  189. return ', '.join("%s (%s)" % (x[0], x[1].replace("_", " ")) for x in type['required'])
  190. if options.wireless and options.list_encryption_types:
  191. et = misc.LoadEncryptionMethods()
  192. # print 'Installed encryption templates:'
  193. print '%s\t%-20s\t%s' % ('#', 'Name', 'Description')
  194. id = 0
  195. for type in et:
  196. print '%s\t%-20s\t%s' % (id, type['type'], type['name'])
  197. print ' Req: %s' % str_properties(type['required'])
  198. print '---'
  199. # don't print optionals (yet)
  200. #print ' Opt: %s' % str_properties(type['optional'])
  201. id += 1
  202. op_performed = True
  203. if options.save and options.network > -1:
  204. if options.wireless:
  205. is_valid_wireless_network_id(options.network)
  206. config.SaveWirelessNetworkProfile(options.network)
  207. elif options.wired:
  208. config.SaveWiredNetworkProfile(options.name)
  209. op_performed = True
  210. if not op_performed:
  211. print "No operations performed."