def arg_parser(): parser = argparse.ArgumentParser() parser.add_argument('-i', '--interface', default='wlan0', help="Specifies which interface to use (wlan0, eth0, etc.)") parser.add_argument('-f', '--file', default='/etc/network/interfaces', help="Specifies which file for scheme storage.") subparsers = parser.add_subparsers(title='commands') parser_scan = subparsers.add_parser('scan', help="Shows a list of available networks.") parser_scan.set_defaults(func=scan_command) parser_list = subparsers.add_parser('list', help="Shows a list of networks already configured.") parser_list.set_defaults(func=list_command) scheme_help = ("A memorable nickname for a wireless network." " If SSID is not provided, the network will be guessed using SCHEME.") ssid_help = ("The SSID for the network to which you wish to connect." " This is fuzzy matched, so you don't have to be precise.") parser_show = subparsers.add_parser('config', help="Prints the configuration to connect to a new network.") parser_show.add_argument('scheme', help=scheme_help, metavar='SCHEME') parser_show.add_argument('ssid', nargs='?', help=ssid_help, metavar='SSID') parser_show.set_defaults(func=show_command) parser_add = subparsers.add_parser('add', help="Adds the configuration to connect to a new network.") parser_add.add_argument('scheme', help=scheme_help, metavar='SCHEME') parser_add.add_argument('ssid', nargs='?', help=ssid_help, metavar='SSID') parser_add.set_defaults(func=add_command) parser_connect = subparsers.add_parser('connect', help="Connects to the network corresponding to SCHEME") parser_connect.add_argument('scheme', help="The nickname of the network to which you wish to connect.", metavar='SCHEME') parser_connect.add_argument('-a', '--ad-hoc', dest='adhoc', action="store_true", help="Connect to a network without storing it in the config file") parser_connect.set_defaults(func=connect_command) # TODO: how to specify the correct interfaces file to work off of. parser_connect.get_options = lambda: [scheme.name for scheme in Scheme.all()] parser_autoconnect = subparsers.add_parser( 'autoconnect', help="Searches for saved schemes that are currently" " available and connects to the first one it finds." ) parser_autoconnect.set_defaults(func=autoconnect_command) return parser, subparsers
def delete(self, name): s = [s for s in Scheme.all() if s.name == name] if len(s) > 0: s[0].delete() return jsonify({'response': "ok"}) else: return jsonify({'response': "non found"})
def get(self, name): parser = reqparse.RequestParser() parser.add_argument('action') args = parser.parse_args() s = [s for s in Scheme.all() if s.name == name] if len(s) == 0: return jsonify({'response': "non found"}) scheme = s[0] return jsonify({'scheme': scheme.__dict__})
def put(self, name): parser = reqparse.RequestParser() parser.add_argument('action') parser.add_argument('ssid') parser.add_argument('password') args = parser.parse_args() s = [s for s in Scheme.all() if s.name == name] if len(s) == 0: return jsonify({'response': "non found"}) scheme = s[0] if args["action"] == 'connect': try: scheme.activate() except ConnectionError: return jsonify( {"error": "Failed to connect to %s." % scheme.name}) return jsonify({'scheme': scheme.__dict__, "connected": True}) elif args["action"] == "configure": cells = [ cell for cell in Cell.all("wlan0") if cell.ssid == args['ssid'] ] if len(cells) == 0: return jsonify({'error': 'wifi not found'}) sname = scheme.name for s in Scheme.all(): s.delete() if s.name == sname: s = Scheme.for_cell('wlan0', sname, cells[0], args['password']) s.save() return jsonify({'scheme': scheme.__dict__}) elif args["action"] == "clean": sname = scheme.name for s in Scheme.all(): s.delete() if s.name == sname: s = Scheme('wlan0', sname) s.save() else: return jsonify({'scheme': scheme.__dict__})
def scheme_all(): """ return all schemes stored in /etc/network/interfaces :return: list of schemes as json string """ schemes = Scheme.all() res = [] for s in schemes: res.append(_scheme_to_dict(s)) return res
def autoconnect_command(args): ssids = [cell.ssid for cell in Cell.all(args.interface)] for scheme in Scheme.all(): # TODO: make it easier to get the SSID off of a scheme. ssid = scheme.options.get('wpa-ssid', scheme.options.get('wireless-essid')) if ssid in ssids: sys.stderr.write('Connecting to "%s".\n' % ssid) try: scheme.activate() except ConnectionError: assert False, "Failed to connect to %s." % scheme.name break else: assert False, "Couldn't find any schemes that are currently available."
def select_network(self): # Wifi select element self.selectwifi = builder.get_object("selectwifi") self.selectwifi.show() # List Interface self.list_interface = builder.get_object("list_interface") # List wifi networks ssids = [cell.ssid for cell in Cell.all("wlan0")] schemes = list(Scheme.all()) # Fills the wifi select with available networks for ssid in ssids: self.treeiter = self.list_interface.append([ssid])
def delete_all(db, db_only=False): """ delete all connection schemes :param db: sqlite3 database handle :param db_only: boolean flag to decide whether a deletion concerns only the database :return: tuple with the total number of schemes and the number of deleted schemes """ schemes = Scheme.all() total = 0 deleted = 0 for s in schemes: total += 1 delete(s.interface, s.name, db, db_only) deleted += 1 return total, deleted
def Search(self): cells = Cell.all('wlan0') self.endpoints = list(cells) serializableEndpoints = [] for i in self.endpoints: wifiEndpoint = WifiEndpoint() wifiEndpoint.ssid = i.ssid wifiEndpoint.signal = i.signal wifiEndpoint.quality = i.quality wifiEndpoint.frequency = i.frequency wifiEndpoint.bitrates = i.bitrates wifiEndpoint.encrypted = i.encrypted wifiEndpoint.channel = i.channel wifiEndpoint.address = i.address wifiEndpoint.mode = i.mode serializableEndpoints.append(wifiEndpoint) #print (i.ssid, i.signal, i.quality, i.frequency) schemes = Scheme.all() #print(jsonpickle.encode(schemes)) return jsonpickle.encode(serializableEndpoints)
def post(self): parser = reqparse.RequestParser() parser.add_argument('name') parser.add_argument('password') args = parser.parse_args() schemes = [s for s in Scheme.all()] cells = Cell.all('wlan0') newscheme = None for cell in cells: if cell.ssid == args['name']: newscheme = Scheme.for_cell('wlan0', 'scheme-' + str(len(schemes)), cell, args['password']) break if newscheme is None: return jsonify({'response': "network non found"}) else: newscheme.save() newscheme.activate() return jsonify({'response': "ok"})
from wifi import Cell, Scheme wifi_list = Cell.all('wlp1s0') con_to = 0 for item in wifi_list: if item.ssid == 'SSID_Name': con_to = item print(con_to) schemes = Scheme.all() for scheme in schemes: scheme.delete() passkey = 'Password123' scheme = Scheme.for_cell('wlp1s0', 'test', con_to, passkey) scheme.activate()
from __future__ import print_function from wifi import Cell, Scheme # get all cells from the air ssids = [cell.ssid for cell in Cell.all('wlan0')] schemes = list(Scheme.all()) for scheme in schemes: ssid = scheme.options.get('wpa-ssid', scheme.options.get('wireless-essid')) if ssid in ssids: print('Connecting to %s' % ssid) scheme.activate() break
def get_saved_networks(): saved_networks = [] for scheme in Scheme.all(): saved_networks.append(scheme) return saved_networks
def get(self): schemes = Scheme.all() return jsonify({'schemes': [s.__dict__ for s in schemes]})
#!/usr/bin/python from __future__ import print_function from wifi import Cell, Scheme # get all cells from the air ssids = [cell.ssid for cell in Cell.all('wlan0')] schemes = list(Scheme.all()) for scheme in schemes: ssid = scheme.options.get('wpa-ssid', scheme.options.get('wireless-essid')) if ssid in ssids: print('Connecting to %s' % ssid) scheme.activate() break
def get(self): schemes = Scheme.all() pattern = re.compile("^scheme-\d*$") sc = [s.__dict__ for s in schemes if pattern.match(s.name)] sc = sorted(sc, key=lambda s: s['name']) return jsonify({'schemes': sc})