def WLC_login(): cli_args = _parse_args() login = { 'user': cli_args.user, 'host': cli_args.target, 'password': cli_args.password } wlc = WLC(login) try_again = 3 login_ok = False while try_again > 1: try: wlc.open() login_ok = True break except HTTPError as err: if 401 == err.code: print "Password failed, try again." try_again -= 1 wlc.password = getpass() if not login_ok: print "Too many login failures, exiting" sys.exit(1) return wlc
def WLC_login(): cli_args = _parse_args() login = { 'user': cli_args.user, 'host': cli_args.target, 'password': cli_args.password } wlc = WLC( login ) try_again = 3 login_ok = False while try_again > 1: try: wlc.open() login_ok = True break; except HTTPError as err: if 401 == err.code: print "Password failed, try again." try_again -= 1 wlc.password = getpass() if not login_ok: print "Too many login failures, exiting" sys.exit(1) return wlc
def find_client(wlc, *vargs, **kvargs): """ create a single dictionary containing information about specific stations. Filter can be username, mac or ip. sessions on a WLC can be found by any combination of mac, ip or username, this fn only uses the most specific attr. """ if 'macaddress' in kvargs: rsp = wlc.rpc.act_findclient(macaddress=kvargs['macaddress']) elif 'ipaddress' in kvargs: rsp = wlc.rpc.act_findclient(ipaddress=kvargs['ipaddress']) elif 'username' in kvargs: rsp = wlc.rpc.act_findclient(username=kvargs['username']) else: raise ValueError("You must provide a mac, ip or username") ret_data = {} # empty dictionary # It is possible for a find client request to return a session which # is not on the same WLC that we initiated the request to. The WLC # will use the Mobility Domain plumbing to locate sessions on other # WLCs. This fn will attempt to connect to the alternate WLC to get # session stats. for session in rsp.findall('.//FINDCLIENT-ENTRY'): dp = session.get('dp-system-ip') sessionid = session.get('session-id') if wlc.facts['ip-addr'] != session.get('dp-system-ip'): print( '### session on different WLC, connect to new WLC using existing credentials ###' ) altwlc = WLC(user=wlc._user, host=dp, password=wlc._password) altwlc.open() rpc = altwlc.RpcMaker('get-stat') else: rpc = wlc.RpcMaker('get-stat') rpc.data = E('USER-SESSION-STATUS') rpc.data.set('session-id', sessionid) sessstats = rpc() sessstat = sessstats.find('.//USER-SESSION-STATUS') locstat = sessstats.find('.//USER-LOCATION-MEMBER') ret_data[sessstat.get('mac-addr')] = dict( sessstat.attrib) # copy the attributes into a dictionary ret_data[sessstat.get('mac-addr')].update(locstat.attrib) return ret_data
def find_client( wlc, *vargs, **kvargs ): """ create a single dictionary containing information about specific stations. Filter can be username, mac or ip. sessions on a WLC can be found by any combination of mac, ip or username, this fn only uses the most specific attr. """ if 'macaddress' in kvargs: rsp = wlc.rpc.act_findclient( macaddress = kvargs['macaddress'] ) elif 'ipaddress' in kvargs: rsp = wlc.rpc.act_findclient( ipaddress = kvargs['ipaddress'] ) elif 'username' in kvargs: rsp = wlc.rpc.act_findclient( username = kvargs['username'] ) else: raise ValueError("You must provide a mac, ip or username") ret_data = {} # empty dictionary # It is possible for a find client request to return a session which # is not on the same WLC that we initiated the request to. The WLC # will use the Mobility Domain plumbing to locate sessions on other # WLCs. This fn will attempt to connect to the alternate WLC to get # session stats. for session in rsp.findall('.//FINDCLIENT-ENTRY'): dp = session.get('dp-system-ip') sessionid = session.get('session-id') if wlc.facts['ip-addr'] != session.get('dp-system-ip'): print('### session on different WLC, connect to new WLC using existing credentials ###') altwlc = WLC( user=wlc._user, host=dp, password=wlc._password ) altwlc.open() rpc = altwlc.RpcMaker('get-stat') else: rpc = wlc.RpcMaker('get-stat') rpc.data = E('USER-SESSION-STATUS') rpc.data.set('session-id', sessionid) sessstats = rpc() sessstat = sessstats.find('.//USER-SESSION-STATUS') locstat = sessstats.find('.//USER-LOCATION-MEMBER') ret_data[sessstat.get('mac-addr')] = dict(sessstat.attrib) # copy the attributes into a dictionary ret_data[sessstat.get('mac-addr')].update(locstat.attrib) return ret_data
import demowlcutils from demowlcutils import ppxml, WLC_login from pprint import pprint as pp from jnpr.wlc import WirelessLanController as WLC # wlc = WLC_login() wlc = WLC(host='a',user='******',password='******') def vlan_create( wlc, *vargs, **kvargs ): """ create a VLAN """ print "So you want to CREATE a VLAN" print "Take a look at the create_vlan_j2.py file ..." return True def vlan_delete( wlc, *vargs, **kvargs ): """ delete a VLAN """ if 'number' in kvargs: r = wlc.rpc.delete_vlan( number = kvargs['number'] ) elif 'name' in kvargs: # need to translate name to number since we can # only delete a VLAN by its number v = wlc.rpc.get_vlan( name = kvargs['name']) num = v.find('VLAN').attrib['number'] r = wlc.rpc.delete_vlan( number = num ) else: raise ValueError("You must provide either 'name' or 'number'")
import demowlcutils from demowlcutils import ppxml, WLC_login from pprint import pprint as pp from jnpr.wlc import WirelessLanController as WLC wlc = WLC(host='a', user='******', password='******') r = wlc.RpcMaker( target='vlan', name='Jeremy') # you can access the following attributes, refer to the jnpr.wlc.builder # file for more details # r.cmd # r.target # r.args