def waitUntilPortsAppear(resource_id=1, base_url="http://localhost:8080", port_list=()): print("waitUntilPortsAppear") found_stations = [] sleep(2) while len(found_stations) < len(port_list): found_stations = [] for port_name in port_list: sleep(1) url = base_url + "/port/1/%s/%s" % (resource_id, port_name) lf_r = LFRequest.LFRequest(url) json_response = lf_r.getAsJson(show_error=False) if (json_response != None): found_stations.append(port_name) else: lf_r = LFRequest.LFRequest(base_url + "/cli-form/nc_show_ports") lf_r.addPostData({ "shelf": 1, "resource": resource_id, "port": port_name, "flags": 1 }) lr_r.formPost() sleep(2) print("These stations appeared: " + ", ".join(found_stations)) return None
def main(): base_url = "http://localhost:8080" json_post = "" json_response = "" # see if there are old wanlinks to remove json_post = LFRequest.LFRequest(base_url + "/layer4/list") try: json_response = json_post.getAsJson() LFUtils.debug_printer.pprint(json_response) except urllib.error.HTTPError as error: j_printer.pprint(error) add_l4_endp_url = base_url + "/cli-json/add_l4_endp" json_post = LFRequest.LFRequest(add_l4_endp_url) json_post.addPostData({ "shelf": 1, "resource": 1, "port": "sta00500", "type": "l4_generic", "timeout": 2000, "url_rate": 600, # this produces an error that should be listed in headers "URL": "dl http://10.40.0.1/ /dev/null" }) json_response = json_post.jsonPost(True) j_printer.pprint(json_response)
def wait_until_ports_appear(base_url="http://localhost:8080", port_list=(), debug=False): """ Use this method to pause until the LANforge system has caught up and implemented the ports you have requested to create. This determines the presence of interfaces, it does not inspect their state. It is appropriate to use when creating stations in the admin-down state. Remember physical port changes, mac-vlans, and 1Qvlans might not appear if they are created admin-down. :param base_url: :param port_list: :param debug: :return: """ if debug: print("Waiting until ports appear...") found_stations = [] port_url = "/port/1" ncshow_url = "/cli-json/nc_show_ports" if base_url.endswith('/'): port_url = port_url[1:] ncshow_url = ncshow_url[1:] while len(found_stations) < len(port_list): found_stations = [] for port_eid in port_list: eid = name_to_eid(port_eid) shelf = eid[0] resource_id = eid[1] port_name = eid[2] # print("waiting for sta sta "+port_eid) uri = "%s/%s/%s" % (port_url, resource_id, port_name) lf_r = LFRequest.LFRequest(base_url, uri) json_response = lf_r.getAsJson(debug_=False) if (json_response != None): found_stations.append(port_name) else: lf_r = LFRequest.LFRequest(base_url, ncshow_url) lf_r.addPostData({ "shelf": shelf, "resource": resource_id, "port": port_name, "probe_flags": 5 }) lf_r.jsonPost() if (len(found_stations) < len(port_list)): sleep(2) if debug: print("These stations appeared: " + ", ".join(found_stations)) return
def wait_until_ports_disappear(base_url="http://localhost:8080", port_list=[], debug=False): print("Waiting until ports disappear...") url = "/port/1" found_stations = port_list.copy() while len(found_stations) > 0: found_stations = [] for port_eid in port_list: eid = name_to_eid(port_eid) shelf = eid[0] resource_id = eid[1] port_name = eid[2] check_url = "%s/%s/%s" % (url, resource_id, port_name) if debug: print("checking:" + check_url) lf_r = LFRequest.LFRequest(base_url, check_url) json_response = lf_r.getAsJson(debug_=debug) if (json_response != None): found_stations.append(port_name) if len(found_stations) > 0: sleep(1) sleep(1) # safety return
def wait_until_ports_admin_up(resource_id=1, base_url="http://localhost:8080", port_list=(), debug_=False): print("Waiting until ports appear admin-up...") down_stations = port_list.copy() sleep(1) port_url = "/port/1" # url = /%s/%s?fields=device,down" % (resource_id, port_name) while len(down_stations) > 0: down_stations = [] for port_name in port_list: uri = "%s/%s/%s?fields=device,down" % (port_url, resource_id, port_name) lf_r = LFRequest.LFRequest(base_url, uri) json_response = lf_r.getAsJson(debug_=False) if json_response == None: if debug_: print("port %s appeared" % port_name) continue if "interface" in json_response: json_response = json_response['interface'] if json_response['down'] == "true": down_stations.append(port_name) sleep(1) return None
def json_put(self, _req_url, _data, debug_=False, response_json_list_=None): """ Send a PUT request. This is presently used for data sent to /status-msg for creating a new messaging session. It is not presently used for CLI scripting so lacks suppress_x features. :param _req_url: url to put :param _data: data to place at URL :param debug_: enable debug output :param response_json_list_: array for json results in the response object, (alternative return method) :return: http response object """ json_response = None try: lf_r = LFRequest.LFRequest(self.lfclient_url, _req_url, debug_=self.debug, die_on_error_=self.exit_on_error) lf_r.addPostData(_data) if debug_ or self.debug: LANforge.LFUtils.debug_printer.pprint(_data) json_response = lf_r.json_put(show_error=self.debug, debug=(self.debug or debug_), response_json_list_=response_json_list_, die_on_error_=self.exit_on_error) if debug_ and (response_json_list_ is not None): pprint.pprint(response_json_list_) except Exception as x: if self.debug or self.halt_on_error or self.exit_on_error: print("json_put submitted to %s" % _req_url) pprint.pprint(_data) print("Exception %s:" % x) traceback.print_exception(Exception, x, x.__traceback__, chain=True) if self.halt_on_error or self.exit_on_error: exit(1) return json_response
def wait_until_endps(base_url="http://localhost:8080", endp_list=(), debug=False): """ :param base_url: :param port_list: :param debug: :return: """ print("Waiting until endpoints appear...") found_endps = [] port_url = "/port/1" ncshow_url = "/cli-form/show_endp" if base_url.endswith('/'): port_url = port_url[1:] ncshow_url = ncshow_url[1:] while len(found_stations) < len(endp_list): found_stations = [] for port_eid in endp_list: eid = name_to_eid(port_eid) shelf = eid[0] resource_id = eid[1] port_name = eid[2] uri = "%s/%s/%s" % (port_url, resource_id, port_name) lf_r = LFRequest.LFRequest(base_url, uri) json_response = lf_r.getAsJson(debug_=False) if (json_response != None): found_stations.append(port_name) else: lf_r = LFRequest.LFRequest(base_url, ncshow_url) lf_r.addPostData({ "shelf": shelf, "resource": resource_id, "port": port_name, "flags": 1 }) lf_r.formPost() if (len(found_stations) < len(endp_list)): sleep(2) if debug: print("These stations appeared: " + ", ".join(found_stations)) return
def main(): url = "http://localhost:8080/port/1/1/list" timeout = 5 # seconds lf_r = LFRequest.LFRequest(url) json_response = lf_r.getAsJson(True) j_printer = pprint.PrettyPrinter(indent=2) j_printer.pprint(json_response)
def remove_cx(baseurl, cx_names, debug=False): if debug: print("Removing cx %s" % ", ".join(cx_names)) url = "/cli-json/rm_cx" for name in cx_names: data = {"test_mgr": "all", "cx_name": name} lf_r = LFRequest.LFRequest(baseurl, url) lf_r.addPostData(data) lf_r.jsonPost(debug)
def remove_endps(baseurl, endp_names, debug=False): if debug: print("Removing endp %s" % ", ".join(endp_names)) url = "/cli-json/rm_endp" lf_r = LFRequest.LFRequest(baseurl, url) for name in endp_names: data = {"endp_name": name} lf_r.addPostData(data) lf_r.jsonPost(debug)
def remove_port(resource, port_name, baseurl="http://localhost:8080/", debug=False): if debug: print("Removing port %d.%s" % (resource, port_name)) url = "/cli-json/rm_vlan" lf_r = LFRequest.LFRequest(baseurl, url) lf_r.addPostData({"shelf": 1, "resource": resource, "port": port_name}) lf_r.jsonPost(debug)
def json_post(self, _req_url, _data, debug_=False, suppress_related_commands_=None, response_json_list_=None): """ send json to the LANforge client :param _req_url: requested url :param _data: json data to send :param debug_: turn on debugging output :param suppress_related_commands_: when False, override self.preexec; when True use :param response_json_list_: array for json results in the response object, (alternative return method) :return: http response object """ json_response = None try: lf_r = LFRequest.LFRequest(self.lfclient_url, _req_url, debug_=self.debug, die_on_error_=self.exit_on_error) if suppress_related_commands_ is None: if 'suppress_preexec_cli' in _data: del _data['suppress_preexec_cli'] if 'suppress_preexec_method' in _data: del _data['suppress_preexec_method'] if 'suppress_postexec_cli' in _data: del _data['suppress_postexec_cli'] if 'suppress_postexec_method' in _data: del _data['suppress_postexec_method'] elif suppress_related_commands_ == False: _data['suppress_preexec_cli'] = False _data['suppress_preexec_method'] = False _data['suppress_postexec_cli'] = False _data['suppress_postexec_method'] = False elif self.suppress_related_commands or suppress_related_commands_: _data['suppress_preexec_cli'] = False _data['suppress_preexec_method'] = False _data['suppress_postexec_cli'] = True _data['suppress_postexec_method'] = True lf_r.addPostData(_data) if debug_ or self.debug: LANforge.LFUtils.debug_printer.pprint(_data) json_response = lf_r.jsonPost(show_error=self.debug, debug=(self.debug or debug_), response_json_list_=response_json_list_, die_on_error_=self.exit_on_error) if debug_ and (response_json_list_ is not None): pprint.pprint(response_json_list_) except Exception as x: if self.debug or self.halt_on_error or self.exit_on_error: print("json_post posted to %s" % _req_url) pprint.pprint(_data) print("Exception %s:" % x) traceback.print_exception(Exception, x, x.__traceback__, chain=True) if self.halt_on_error or self.exit_on_error: exit(1) return json_response
def find_port_eids(resource_id=1, base_url="http://localhost:8080", port_names=(), debug=False): port_eids = [] if len(port_names) < 0: return [] port_url = "/port/1" for port_name in port_names: uri = "%s/%s/%s" % (port_url, resource_id, port_name) lf_r = LFRequest.LFRequest(base_url, uri) try: response = lf_r.getAsJson(debug) if response is None: continue port_eids.append(PortEID(response)) except: print("Not found: " + port_name) return port_eids
def waitUntilPortsDisappear(resource_id=1, base_url="http://localhost:8080", port_list=()): print("waitUntilPortsDisappear") found_stations = port_list.copy() sleep(1) while len(found_stations) > 0: found_stations = [] for port_name in port_list: sleep(1) url = base_url + "/port/1/%s/%s" % (resource_id, port_name) lf_r = LFRequest.LFRequest(url) json_response = lf_r.getAsJson(show_error=False) if (json_response != None): found_stations.append(port_name) return None
def findPortEids(resource_id=1, base_url="http://localhost:8080", port_names=()): port_eids = [] if len(port_names) < 0: return [] for port_name in port_names: url = "/port/1/%s/%s" % (resource_id, port_name) lf_r = LFRequest.LFRequest(url) try: response = lf_r.getAsJson() if response == None: continue port_eids.append(PortEID(response)) except: print("Not found: " + port_name) return None
def json_get(self, _req_url, debug_=False): if self.debug or debug_: print("GET: "+_req_url) json_response = None # print("----- GET ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ") try: lf_r = LFRequest.LFRequest(self.lfclient_url, _req_url, debug_=(self.debug or debug_), die_on_error_=self.exit_on_error) json_response = lf_r.get_as_json(debug_=self.debug, die_on_error_=self.halt_on_error) #debug_printer.pprint(json_response) if (json_response is None) and (self.debug or debug_): print("LFCliBase.json_get: no entity/response, probabily status 404") return None except ValueError as ve: if self.debug or self.halt_on_error or self.exit_on_error: print("jsonGet asked for " + _req_url) print("Exception %s:" % ve) traceback.print_exception(ValueError, ve, ve.__traceback__, chain=True) if self.halt_on_error or self.exit_on_error: sys.exit(1) return json_response
def waitUntilPortsAdminUp(resource_id=1, base_url="http://localhost:8080", port_list=()): print("waitUntilPortsAdminUp") down_stations = port_list.copy() sleep(1) while len(down_stations) > 0: down_stations = [] for port_name in port_list: url = base_url + "/port/1/%s/%s?fields=device,down" % (resource_id, port_name) lf_r = LFRequest.LFRequest(url) json_response = lf_r.getAsJson(show_error=False) if json_response == None: print("port %s disappeared" % port_name) continue if "interface" in json_response: json_response = json_response['interface'] if json_response['down'] is "true": down_stations.append(port_name) sleep(1) return None
def main(): base_url = "http://localhost:8080" json_post = "" json_response = "" num_wanlinks = -1 # see if there are old wanlinks to remove lf_r = LFRequest.LFRequest(base_url+"/wl/list") try: json_response = lf_r.getAsJson() LFUtils.debug_printer.pprint(json_response) for key,value in json_response.items(): if (isinstance(value, dict) and "_links" in value): num_wanlinks = 1 except urllib.error.HTTPError as error: num_wanlinks = 0; # remove old wanlinks if (num_wanlinks > 0): lf_r = LFRequest.LFRequest(base_url+"/cli-json/rm_cx") lf_r.addPostData({ 'test_mgr': 'all', 'cx_name': 'wl_eg1' }) lf_r.jsonPost() sleep(0.05) lf_r = LFRequest.LFRequest(base_url+"/cli-json/rm_endp") lf_r.addPostData({ 'endp_name': 'wl_eg1-A' }) lf_r.jsonPost() sleep(0.05) lf_r = LFRequest.LFRequest(base_url+"/cli-json/rm_endp") lf_r.addPostData({ 'endp_name': 'wl_eg1-B' }) lf_r.jsonPost() sleep(0.05) # create wanlink 1a lf_r = LFRequest.LFRequest(base_url+"/cli-json/add_wl_endp") lf_r.addPostData({ 'alias': 'wl_eg1-A', 'shelf': 1, 'resource': '1', 'port': 'eth3', 'latency': '75', 'max_rate': '128000', 'description': 'cookbook-example' }) lf_r.jsonPost() sleep(0.05) # create wanlink 1b lf_r = LFRequest.LFRequest(base_url+"/cli-json/add_wl_endp") lf_r.addPostData({ 'alias': 'wl_eg1-B', 'shelf': 1, 'resource': '1', 'port': 'eth5', 'latency': '95', 'max_rate': '256000', 'description': 'cookbook-example' }) lf_r.jsonPost() sleep(0.05) # create cx lf_r = LFRequest.LFRequest(base_url+"/cli-json/add_cx") lf_r.addPostData({ 'alias': 'wl_eg1', 'test_mgr': 'default_tm', 'tx_endp': 'wl_eg1-A', 'rx_endp': 'wl_eg1-B', }) lf_r.jsonPost() sleep(0.05) # start wanlink once we see it seen = 0 while (seen < 1): sleep(1) lf_r = LFRequest.LFRequest(base_url+"/wl/wl_eg1?fields=name,state,_links") try: json_response = lf_r.getAsJson() if (json_response is None): continue LFUtils.debug_printer.pprint(json_response) for key,value in json_response.items(): if (isinstance(value, dict)): if ("_links" in value): if (value["name"] == "wl_eg1"): seen = 1 #else: # print(" name was not wl_eg1") #else: # print("value lacks _links") #else: # print("value not a dict") except urllib.error.HTTPError as error: print("Error code "+error.code) continue print("starting wanlink:") lf_r = LFRequest.LFRequest(base_url+"/cli-json/set_cx_state") lf_r.addPostData({ 'test_mgr': 'all', 'cx_name': 'wl_eg1', 'cx_state': 'RUNNING' }) lf_r.jsonPost() running = 0 while (running < 1): sleep(1) lf_r = LFRequest.LFRequest(base_url+"/wl/wl_eg1?fields=name,state,_links") try: json_response = lf_r.getAsJson() if (json_response is None): continue for key,value in json_response.items(): if (isinstance(value, dict)): if ("_links" in value): if (value["name"] == "wl_eg1"): if (value["state"].startswith("Run")): LFUtils.debug_printer.pprint(json_response) running = 1 except urllib.error.HTTPError as error: print("Error code "+error.code) continue print("Wanlink is running, wait one sec...") sleep(1) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Now we can alter the delay and speed of the wanlink by # updating its endpoints see https://www.candelatech.com/lfcli_ug.php#set_wanlink_info # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - print("Updating Wanlink...") lf_r = LFRequest.LFRequest(base_url+"/cli-json/set_wanlink_info") lf_r.addPostData({ 'name': 'wl_eg1-A', 'speed': 265333, 'latency': 30, 'reorder_freq': 3200, # thats 3200/1000000 'drop_freq': 2000, # 2000/1000000 'dup_freq': 1325, # 1325/1000000 'jitter_freq': 25125, # 25125/1000000 }) lf_r.jsonPost() sleep(1) # stop wanlink lf_r = LFRequest.LFRequest(base_url+"/cli-json/set_cx_state") lf_r.addPostData({ 'test_mgr': 'all', 'cx_name': 'wl_eg1', 'cx_state': 'STOPPED' }) lf_r.jsonPost() running = 1 while (running > 0): sleep(1) lf_r = LFRequest.LFRequest(base_url+"/wl/wl_eg1?fields=name,eid,state,_links") LFUtils.debug_printer.pprint(json_response) try: json_response = lf_r.getAsJson() if (json_response is None): continue for key,value in json_response.items(): if (isinstance(value, dict)): if ("_links" in value): if (value["name"] == "wl_eg1"): if (value["state"].startswith("Stop")): LFUtils.debug_printer.pprint(json_response) running = 0 except urllib.error.HTTPError as error: print("Error code "+error.code) continue print("Wanlink is stopped.") print("Wanlink info:") lf_r = LFRequest.LFRequest(base_url+"/wl/wl_eg1") json_response = lf_r.getAsJson() LFUtils.debug_printer.pprint(json_response) lf_r = LFRequest.LFRequest(base_url+"/wl_ep/wl_eg1-A") json_response = lf_r.getAsJson() LFUtils.debug_printer.pprint(json_response) lf_r = LFRequest.LFRequest(base_url+"/wl_ep/wl_eg1-B") json_response = lf_r.getAsJson() LFUtils.debug_printer.pprint(json_response)
def main(): host = "localhost" base_url = "http://%s:8080" % host resource_id = 1 # typically you're using resource 1 in stand alone realm radio = "wiphy0" start_id = 200 end_id = 202 padding_number = 10000 # the first digit of this will be deleted ssid = "jedway-wpa2-x2048-4-1" passphrase = "jedway-wpa2-x2048-4-1" parser = argparse.ArgumentParser(description="test creating a station") parser.add_argument("-m", "--host", type=str, help="json host to connect to") parser.add_argument("-r", "--radio", type=str, help="radio to create a station on") parser.add_argument("-a", "--start_id", type=int, help="starting station id") parser.add_argument("-b", "--end_id", type=int, help="ending station id") parser.add_argument("-s", "--ssid", type=str, help="station ssid") parser.add_argument("-p", "--passwd", type=str, help="password for ssid") args = None try: args = parser.parse_args() if (args.host is not None): host = args.host, baseurl = base_url = "http://%s:8080" % host if (args.radio is not None): radio = args.radio if (args.start_id is not None): start_id = args.start_id if (args.end_id is not None): end_id = args.end_id if (args.ssid is not None): ssid = args.ssid if (args.passwd is not None): passphrase = args.passwd except Exception as e: logging.exception(e) usage() exit(2) # station numbers are heavily manipulated strings, often using manual padding # sta200 is not sta0200 nor sta00200, and we can format these numbers by adding # a 1000 or 10000 to the station id, and trimming the first digit off j_printer = pprint.PrettyPrinter(indent=2) json_post = "" json_response = "" found_stations = [] lf_r = LFRequest.LFRequest(base_url + "/port/1/1/wiphy0") wiphy0_json = lf_r.getAsJson() if (wiphy0_json is None) or (wiphy0_json['interface'] is None): print("Unable to find radio. Are we connected?") exit(1) # If you need to inspect a radio.... #print("# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -") #print("# radio wiphy0 -") #print("# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -") #LFUtils.debug_printer.pprint(wiphy0_json['interface']['alias']) #parent_radio_mac = wiphy0_json['interface']['mac'] #print("# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -") # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Example 1 - # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # This section uses URLs /cli-form/rm_vlan, /cli-form/add_sta # The /cli-form URIs take URL-encoded form posts # # For each of the station names, delete them if they exist. It # takes a few milliseconds to delete them, so after deleting them # you need to poll until they don't appear. # # NOTE: the ID field of the EID is ephemeral, so best stick to # requesting the station name. The station name can be formatted # with leading zeros, sta00001 is legal # and != {sta0001, sta001, sta01, or sta1} desired_stations = LFUtils.portNameSeries("sta", start_id, end_id, padding_number) #LFUtils.debug_printer.pprint(desired_stations) print("Example 1: will create stations %s" % (",".join(desired_stations))) for sta_name in desired_stations: url = base_url + "/port/1/%s/%s" % (resource_id, sta_name) print("Ex 1: Checking for station : " + url) lf_r = LFRequest.LFRequest(url) json_response = lf_r.getAsJson(show_error=False) if (json_response != None): found_stations.append(sta_name) for sta_name in found_stations: print("Ex 1: Deleting station %s ...." % sta_name) lf_r = LFRequest.LFRequest(base_url + "/cli-form/rm_vlan") lf_r.addPostData({ "shelf": 1, "resource": resource_id, "port": sta_name, "suppress_preexec_cli": "yes", "suppress_preexec_method": 1 }) json_response = lf_r.formPost() sleep(0.05 ) # best to give LANforge a few millis between rm_vlan commands LFUtils.waitUntilPortsDisappear(resource_id, base_url, found_stations) print("Ex 1: Next we create stations...") #68727874560 was previous flags for sta_name in desired_stations: print("Ex 1: Next we create station %s" % sta_name) lf_r = LFRequest.LFRequest(base_url + "/cli-form/add_sta") # flags are a decimal equivalent of a hexadecimal bitfield # you can submit as either 0x(hex) or (dec) # a helper page is available at http://localhost:8080/help/add_sta # # You can watch console output of the LANforge GUI client when you # get errors to this command, and you can also watch the websocket # output for a response to this command at ws://localhost:8081 # Use wsdump ws://localhost:8081/ # # modes are listed at http://<YOUR_LANFORGE>/LANforgeDocs-5.4.1/lfcli_ug.html # or at https://www.candelatech.com/lfcli_ug.html # # mac address field is a pattern for creation: entirely random mac addresses # do not take advantage of address mask matchin in Ath10k hardware, so we developed # this pattern to randomize a section of octets. XX: keep parent, *: randomize, and # chars [0-9a-f]: use this digit # # If you get errors like "X is invalid hex chara cter", this indicates a previous # rm_vlan call has not removed your station yet: you cannot rewrite mac addresses # with this call, just create new stations lf_r.addPostData( LFUtils.staNewDownStaRequest(sta_name, resource_id=resource_id, radio=radio, ssid=ssid, passphrase=passphrase)) lf_r.formPost() sleep(0.1) LFUtils.waitUntilPortsAppear(resource_id, base_url, desired_stations) for sta_name in desired_stations: sleep(1) print("doing portSetDhcpDownRequest on " + sta_name) lf_r = LFRequest.LFRequest(base_url + "/cli-form/set_port") lf_r.addPostData(LFUtils.portSetDhcpDownRequest(resource_id, sta_name)) lf_r.formPost() # the LANforge API separates STA creation and ethernet port settings # We need to revisit the stations we create and amend flags to add # things like DHCP or ip+gateway, admin-{up,down} LFUtils.waitUntilPortsAppear(resource_id, base_url, desired_stations) for sta_name in desired_stations: sleep(1) print("Ex 1: station up %s" % sta_name) lf_r = LFRequest.LFRequest(base_url + "/cli-json/set_port") data = LFUtils.portDhcpUpRequest(resource_id, sta_name) lf_r.addPostData(data) lf_r.jsonPost() LFUtils.waitUntilPortsAppear(resource_id, base_url, desired_stations) # for sta_name in desired_stations: # print("Ex 1: sta down %s"%sta_name) # lf_r = LFRequest.LFRequest(base_url+"/cli-json/set_port") # lf_r.addPostData(LFUtils.portDownRequest(resource_id, sta_name)) # lf_r.jsonPost() # sleep(0.05) print("...done with example 1\n\n") sleep(4) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Example 2 - # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # uses URLs /cli-json/rm_vlan, /cli-json/add_sta # and those accept POST in json formatted text desired_stations = [] found_stations = [] start_id = 220 end_id = 222 desired_stations = LFUtils.portNameSeries("sta", start_id, end_id, padding_number) print("Example 2: using port list to find stations") sleep(1) url = base_url + "/port/1/%s/list?fields=alias" % (resource_id) lf_r = LFRequest.LFRequest(url) json_response = lf_r.getAsJson() if json_response == None: raise Exception("no reponse to: " + url) port_map = LFUtils.portListToAliasMap(json_response) #LFUtils.debug_printer.pprint(port_map) for sta_name in desired_stations: print("Ex 2: checking for station : " + sta_name) if sta_name in port_map.keys(): #print("found station : "+sta_name) found_stations.append(sta_name) for sta_name in found_stations: print("Ex 2: delete station %s ..." % sta_name) lf_r = LFRequest.LFRequest(base_url + "/cli-json/rm_vlan") lf_r.addPostData({ "shelf": 1, "resource": resource_id, "port": sta_name }) lf_r.jsonPost(show_error=False) sleep(0.05) LFUtils.waitUntilPortsDisappear(resource_id, base_url, found_stations) for sta_name in desired_stations: print("Ex 2: create station %s" % sta_name) lf_r = LFRequest.LFRequest(base_url + "/cli-json/add_sta") lf_r.addPostData( LFUtils.staNewDownStaRequest(sta_name, resource_id=resource_id, radio=radio, ssid=ssid, passphrase=passphrase)) lf_r.jsonPost() sleep(1) LFUtils.waitUntilPortsAppear(resource_id, base_url, desired_stations) # the LANforge API separates STA creation and ethernet port settings # We need to revisit the stations we create and amend flags to add # things like DHCP or ip+gateway, admin-{up,down} for sta_name in desired_stations: print("Ex 2: set port %s" % sta_name) lf_r = LFRequest.LFRequest(base_url + "/cli-json/set_port") data = LFUtils.portDhcpUpRequest(resource_id, sta_name) lf_r.addPostData(data) lf_r.jsonPost() sleep(0.05) print("...done with Example 2") sleep(1) print("Example 3: bring ports up and down") sleep(1) print("Ex 3: setting ports up...") desired_stations.insert(0, "sta0200") desired_stations.insert(1, "sta0201") desired_stations.insert(2, "sta0202") wait_for_these = [] for sta_name in desired_stations: lf_r = LFRequest.LFRequest(base_url + "/port/1/%s/%s?fields=port,device,down" % (resource_id, sta_name)) json_response = lf_r.getAsJson() if json_response['interface']['down'] is 'true': url = base_url + "/cli-json/set_port" lf_r = LFRequest.LFRequest(url) lf_r.addPostData(LFUtils.portDhcpUpRequest(resource_id, sta_name)) print("setting %s up" % sta_name) lf_r.jsonPost() wait_for_these.append(sta_name) LFUtils.waitUntilPortsAdminUp(resource_id, base_url, wait_for_these) sleep(4) print("Ex 3: setting ports down...") for sta_name in desired_stations: lf_r = LFRequest.LFRequest(base_url + "/port/1/%s/%s?fields=port,device,down" % (resource_id, sta_name)) json_response = lf_r.getAsJson() if json_response['interface']['down'] is 'false': url = base_url + "/cli-json/set_port" lf_r = LFRequest.LFRequest(url) lf_r.addPostData(LFUtils.portDownRequest(resource_id, sta_name)) print("setting %s down" % sta_name) lf_r.jsonPost() wait_for_these.append(sta_name) LFUtils.waitUntilPortsAdminDown(resource_id, base_url, wait_for_these) print("...ports are down") sleep(4) print("Example 4: Modify stations to mode /a") sleep(1) for sta_name in desired_stations: #lf_r = LFRequest.LFRequest(base_url+"/port/1/%s/%s"%(resource_id, sta_name)) lf_r = LFRequest.LFRequest(base_url + "/cli-json/set_port") lf_r.addPostData(LFUtils.portDownRequest(resource_id, sta_name)) lf_r.jsonPost() LFUtils.waitUntilPortsAdminDown(resource_id, base_url, desired_stations) for sta_name in desired_stations: lf_r = LFRequest.LFRequest(base_url + "/cli-json/add_sta") lf_r.addPostData({ "shelf": 1, "resource": resource_id, "radio": radio, "sta_name": sta_name, "mode": 1, # 802.11a see http://www.candelatech.com/lfcli_ug.php#add_sta }) print("using add_sta to set %s mode" % sta_name) lf_r.jsonPost() sleep(0.5) for sta_name in desired_stations: lf_r = LFRequest.LFRequest(base_url + "/cli-json/set_port") lf_r.addPostData(LFUtils.portUpRequest(resource_id, sta_name)) lf_r.get() LFUtils.waitUntilPortsAdminUp(resource_id, base_url, desired_stations) print("...done") sleep(4) print("Example 5: change station encryption from wpa2 to wpa3...") sleep(1) for sta_name in desired_stations: #lf_r = LFRequest.LFRequest(base_url+"/port/1/%s/%s"%(resource_id, sta_name)) lf_r = LFRequest.LFRequest(base_url + "/cli-json/set_port") lf_r.addPostData(LFUtils.portDownRequest(resource_id, sta_name)) lf_r.get() LFUtils.waitUntilPortsAdminDown(resource_id, base_url, desired_stations) for sta_name in desired_stations: lf_r = LFRequest.LFRequest(base_url + "/cli-json/add_sta") lf_r.addPostData({ "shelf": 1, "resource": resource_id, "radio": radio, "sta_name": sta_name, "mode": 0, # mode AUTO "flags": 1099511627776, # sets use-wpa3 "flags_mask": 1099511628800 # sets interest in use-wpa3, wpa2_enable (becomes zero) }) print("using add_sta to set %s wpa3" % sta_name) lf_r.jsonPost() sleep(0.5) for sta_name in desired_stations: lf_r = LFRequest.LFRequest(base_url + "/cli-json/set_port") lf_r.addPostData(LFUtils.portUpRequest(resource_id, sta_name)) lf_r.get() LFUtils.waitUntilPortsAdminUp(resource_id, base_url, desired_stations) print("...done") sleep(4) print("Example 7: alter TX power on %s..." % radio) # virtual stations do not have individual tx power states sleep(1) # see http://www.candelatech.com/lfcli_ug.php#set_wifi_radio lf_r = LFRequest.LFRequest(base_url + "/cli-json/set_wifi_radio") lf_r.addPostData({ "shelf": 1, "resource": resource_id, "radio": radio, "mode": NA, # tx power see: man 8 iwconfig, power is in dBm, auto or off "txpower": "auto", # meta flag tells lfclient to not check port before issuing command "suppress_preexec_method": "true", }) lf_r.jsonPost()
def main(): host = "ct524-debbie.jbr.candelatech.com" base_url = "http://%s:8080" % host resource_id = 1 # typically you're using resource 1 in stand alone realm radio = "wiphy0" start_id = 200 end_id = 202 padding_number = 10000 # the first digit of this will be deleted ssid = "jedway-wpa2-x64-3-1" passphrase = "jedway-wpa2-x64-3-1" clisock = 3990 cliprompt = 'lfgui# ' parser = argparse.ArgumentParser(description="test creating a station") parser.add_argument("-m", "--host", type=str, help="json host to connect to") parser.add_argument("-r", "--radio", type=str, help="radio to create a station on") parser.add_argument("-a", "--start_id", type=int, help="starting station id") parser.add_argument("-b", "--end_id", type=int, help="ending station id") parser.add_argument("-s", "--ssid", type=str, help="station ssid") parser.add_argument("-p", "--passwd", type=str, help="password for ssid") args = None try: args = parser.parse_args() if (args.host is not None): host = args.host, baseurl = base_url = "http://%s:8080" % host if (args.radio is not None): radio = args.radio if (args.start_id is not None): start_id = args.start_id if (args.end_id is not None): end_id = args.end_id if (args.ssid is not None): ssid = args.ssid if (args.passwd is not None): passphrase = args.passwd except Exception as e: logging.exception(e) usage() exit(2) # station numbers are heavily manipulated strings, often using manual padding # sta200 is not sta0200 nor sta00200, and we can format these numbers by adding # a 1000 or 10000 to the station id, and trimming the first digit off j_printer = pprint.PrettyPrinter(indent=2) json_post = "" json_response = "" found_stations = [] lf_r = LFRequest.LFRequest(base_url + "/port/1/1/wiphy2") wiphy0_json = lf_r.getAsJson() if (wiphy0_json is None) or (wiphy0_json['interface'] is None): print("Unable to find radio. Are we connected?") exit(1) desired_stations = LFUtils.portNameSeries("sta", start_id, end_id, padding_number) #LFUtils.debug_printer.pprint(desired_stations) print("Example 1: will create stations %s" % (",".join(desired_stations))) for sta_name in desired_stations: url = base_url + "/port/1/%s/%s" % (resource_id, sta_name) print("Ex 1: Checking for station : " + url) lf_r = LFRequest.LFRequest(url) json_response = lf_r.getAsJson(show_error=False) if (json_response != None): found_stations.append(sta_name) for sta_name in found_stations: print("Ex 1: Deleting station %s ...." % sta_name) lf_r = LFRequest.LFRequest(base_url + "/cli-form/rm_vlan") lf_r.addPostData({ "shelf": 1, "resource": resource_id, "port": sta_name }) json_response = lf_r.formPost() sleep(0.05 ) # best to give LANforge a few millis between rm_vlan commands LFUtils.waitUntilPortsDisappear(resource_id, base_url, found_stations) print("Ex 1: Next we create stations...") #68727874560 was previous flags for sta_name in desired_stations: print("Ex 1: Next we create station %s" % sta_name) lf_r = LFRequest.LFRequest(base_url + "/cli-form/add_sta") lf_r.addPostData( LFUtils.staNewDownStaRequest(sta_name, resource_id=resource_id, radio=radio, ssid=ssid, passphrase=passphrase)) lf_r.formPost() sleep(0.1) LFUtils.waitUntilPortsAppear(resource_id, base_url, desired_stations) for sta_name in desired_stations: sleep(1) print("doing portSetDhcpDownRequest on " + sta_name) lf_r = LFRequest.LFRequest(base_url + "/cli-form/set_port") data = LFUtils.portDhcpUpRequest(resource_id, sta_name) lf_r.addPostData(data) lf_r.jsonPost() LFUtils.waitUntilPortsAppear(resource_id, base_url, desired_stations) # Now lets do some cli-socket scripting gui_telnet = pexpect.spawn('telnet %s %s' % (host, clisock)) if gui_telnet is None: print("Unable to telnet to %s:%s" % (host, clisock)) exit(1) gui_telnet.expect('lfgui# ') gui_telnet.sendline("cv create 'WiFi Capacity' 'wct'") gui_telnet.expect('OK') gui_telnet.sendline("cv load wct wct-wpa2-x64-two-loops") gui_telnet.expect('OK') gui_telnet.sendline("cv click wct 'Auto Save Report'") gui_telnet.expect('OK') gui_telnet.sendline("cv click wct Start")