12-03-2020 08:25 PM
I have a python script that I’m using for B-series switches to configure multiple ports at the same time.
#@METADATASTART
#@ScriptDescription "Change port from static to NAC".
#@DetailDescriptionStart
##############################################################################
#
#Purpose: "Change port from static to NAC".
#
# Version : 1.0
#
# This script deletes selected ports from all vlans, sets "display-string" and
# "description-string" to "NAC_Port". Finally, the configuration is saved.
##############################################################################
#@DetailDescriptionEnd
#
#@MetaDataEnd
import re
import commands
def main():
if "port" in emc_vars:
ports = emc_vars["port"]
else:
print "Please select at least one PORT if you start the script"
return 0
cli_result = emc_cli.send("clear port vlan " + ports)
main()
However, I get the following output:
deerl1sw006(su)->clear port vlan ge.1.7,ge.1.8
Invalid Port in [port-string].
The B-Series switch needs to use ; instead of , to separate interfaces. Is there anyway to fix this?
Chuck
Solved! Go to Solution.
12-03-2020 11:23 PM
Hello Chuck,
try this:
#@METADATASTART
#@ScriptDescription "Change port from static to NAC".
#@DetailDescriptionStart
##############################################################################
#
#Purpose: "Change port from static to NAC".
#
# Version : 1.0
#
# This script deletes selected ports from all vlans, sets "display-string" and
# "description-string" to "NAC_Port". Finally, the configuration is saved.
##############################################################################
#@DetailDescriptionEnd
#
#@MetaDataEnd
import re
import commands
def main():
if "port" in emc_vars:
ports = emc_vars["port"].split(",") #create a list with all ports
else:
print "Please select at least one PORT if you start the script"
return 0
portcounter = 0
for port in ports: #walk through the list
if portcounter == 0:
portstring = port
else:
portstring = portstring + ";" + port #fill in the separator you need
portcounter += 1
cli_result = emc_cli.send("clear port vlan " + portstring)
main()
12-04-2020 03:33 PM
Not nice (and not tested) but only to show you how functions and variables works together:
import re
import commands
def writeConfig(my_portstring):
cli_result = emc_cli.send("clear port vlan " + my_portstring)
cli_result = emc_cli.send("set port alias " + my_portstring + " NAC")
cli_result = emc_cli.send("set macauthentication port enable " + my_portstring)
cli_result = emc_cli.send("set multiauth port mode auth-reqd " + my_portstring)
cli_result = emc_cli.send("set multiauth port numusers 4 " + my_portstring)
cli_result = emc_cli.send("set pwa portcontrol enable " + my_portstring)
cli_result = emc_cli.send("save config")
def port_list():
if "port" in emc_vars:
ports = emc_vars["port"].split(",") #create a list with all ports
else:
print "Please select at least one PORT if you start the script"
return 0
portcounter = 0
for port in ports: #walk through the list
if portcounter == 0:
portstring = port
else:
portstring = portstring + ";" + port #fill in the separator you need
portcounter += 1
return portstring
def main():
my_portstring = port_list()
writeConfig(my_portstring)
main()
12-04-2020 03:21 PM
12-04-2020 03:17 PM
Hello Chuck,
post your complete code, or post with line numbers, please. So we can see what lines are the problem.
12-04-2020 03:12 PM
Thanks! A bit embarrassed I missed something so easy. However, now I’m getting this:
Traceback (most recent call last):
File "<string>", line 45, in <module>
File "<string>", line 36, in main
NameError: global name 'portstring' is not defined
12-04-2020 02:52 PM
Here is the error:
“def(port_list)” should be: “def port_list():”