Update for Python3
This commit is contained in:
@@ -11,16 +11,18 @@ from enumip import *
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
def usage():
|
||||
print
|
||||
print "Usage: nfcli --list --eth_addr=ADDR --ip_type=TYPE --ip_addr=IP, --netmask=MASK, --gateway=GW"
|
||||
print "Search and configure Prologix GPIB-ETHERNET Controllers."
|
||||
print "--help : display this help"
|
||||
print "--list : search for controllers"
|
||||
print "--eth_addr=ADDR : configure controller with Ethernet address ADDR"
|
||||
print "--ip_type=TYPE : set controller ip address type to TYPE (\"static\" or \"dhcp\")"
|
||||
print "--ip_addr=IP : set controller address to IP"
|
||||
print "--netmask=MASK : set controller network mask to MASK"
|
||||
print "--gateway=GW : set controller default gateway to GW"
|
||||
print()
|
||||
print("Usage: nfcli --list --eth_addr=ADDR --ip_type=TYPE --ip_addr=IP,",
|
||||
"--netmask=MASK, --gateway=GW")
|
||||
print("Search and configure Prologix GPIB-ETHERNET Controllers.")
|
||||
print("--help : display this help")
|
||||
print("--list : search for controllers")
|
||||
print("--eth_addr=ADDR : configure controller with Ethernet address ADDR")
|
||||
print('--ip_type=TYPE : set controller ip address type to TYPE',
|
||||
'("static" or "dhcp")')
|
||||
print("--ip_addr=IP : set controller address to IP")
|
||||
print("--netmask=MASK : set controller network mask to MASK")
|
||||
print("--gateway=GW : set controller default gateway to GW")
|
||||
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
@@ -37,19 +39,19 @@ def ValidateNetParams(ip_str, mask_str, gw_str):
|
||||
try:
|
||||
ip = socket.inet_aton(ip_str)
|
||||
except:
|
||||
print "IP address is invalid."
|
||||
print("IP address is invalid.")
|
||||
return False
|
||||
|
||||
try:
|
||||
mask = socket.inet_aton(mask_str)
|
||||
except:
|
||||
print "Network mask is invalid."
|
||||
print("Network mask is invalid.")
|
||||
return False
|
||||
|
||||
try:
|
||||
gw = socket.inet_aton(gw_str)
|
||||
except:
|
||||
print "Gateway address is invalid."
|
||||
print("Gateway address is invalid.")
|
||||
return False
|
||||
|
||||
# Validate network mask
|
||||
@@ -59,12 +61,12 @@ def ValidateNetParams(ip_str, mask_str, gw_str):
|
||||
|
||||
# Exclude restricted masks
|
||||
if (mask == 0) or (mask == 0xFFFFFFFF):
|
||||
print "Network mask is invalid."
|
||||
print("Network mask is invalid.")
|
||||
return False
|
||||
|
||||
# Exclude non-left-contiguous masks
|
||||
if (((mask + (mask & -mask)) & 0xFFFFFFFF) != 0):
|
||||
print "Network mask is not contiguous."
|
||||
print("Network mask is not contiguous.")
|
||||
return False
|
||||
|
||||
|
||||
@@ -78,7 +80,7 @@ def ValidateNetParams(ip_str, mask_str, gw_str):
|
||||
# Exclude restricted addresses
|
||||
# 0.0.0.0 is valid
|
||||
if ((gw != 0) and ((octet1 == 0) or (octet1 == 127) or (octet1 > 223))):
|
||||
print "Gateway address is invalid."
|
||||
print("Gateway address is invalid.")
|
||||
return False
|
||||
|
||||
# Validate IP address
|
||||
@@ -90,17 +92,17 @@ def ValidateNetParams(ip_str, mask_str, gw_str):
|
||||
|
||||
# Exclude restricted addresses
|
||||
if ((octet1 == 0) or (octet1 == 127) or (octet1 > 223)):
|
||||
print "IP address is invalid."
|
||||
print("IP address is invalid.")
|
||||
return False
|
||||
|
||||
# Exclude subnet network address
|
||||
if ((ip & ~mask) == 0):
|
||||
print "IP address is invalid."
|
||||
print("IP address is invalid.")
|
||||
return False
|
||||
|
||||
# Exclude subnet broadcast address
|
||||
if ((ip & ~mask) == (0xFFFFFFFF & ~mask)):
|
||||
print "IP address is invalid."
|
||||
print("IP address is invalid.")
|
||||
return False
|
||||
|
||||
return True
|
||||
@@ -139,9 +141,16 @@ def main():
|
||||
eth_addr = None
|
||||
|
||||
try:
|
||||
opts, args = getopt.getopt(sys.argv[1:], '', ['help', 'list', 'eth_addr=', 'ip_type=', 'ip_addr=', 'netmask=', 'gateway='])
|
||||
except getopt.GetoptError, err:
|
||||
print str(err)
|
||||
opts, args = getopt.getopt(sys.argv[1:], '',
|
||||
['help',
|
||||
'list',
|
||||
'eth_addr=',
|
||||
'ip_type=',
|
||||
'ip_addr=',
|
||||
'netmask=',
|
||||
'gateway='])
|
||||
except getopt.GetoptError as err:
|
||||
print(err)
|
||||
sys.exit(1)
|
||||
|
||||
# Check for unparsed parameters
|
||||
@@ -150,7 +159,7 @@ def main():
|
||||
sys.exit(1)
|
||||
|
||||
for o, a in opts:
|
||||
if o == "--help":
|
||||
if o == "--help":
|
||||
showhelp = True
|
||||
elif o == "--list":
|
||||
search = True
|
||||
@@ -168,22 +177,22 @@ def main():
|
||||
if (len(opts) == 0) or (showhelp):
|
||||
usage()
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if search:
|
||||
if not eth_addr is None:
|
||||
print "--list and --eth_addr are not compatible."
|
||||
print("--list and --eth_addr are not compatible.")
|
||||
invalid_args = True
|
||||
if not ip_type is None:
|
||||
print "--list and --ip_type are not compatible."
|
||||
print("--list and --ip_type are not compatible.")
|
||||
invalid_args = True
|
||||
if not ip_addr is None:
|
||||
print "--list and --ip_addr are not compatible."
|
||||
print("--list and --ip_addr are not compatible.")
|
||||
invalid_args = True
|
||||
if not netmask is None:
|
||||
print "--list and --netmask are not compatible."
|
||||
print("--list and --netmask are not compatible.")
|
||||
invalid_args = True
|
||||
if not gateway is None:
|
||||
print "--list and --gateway are not compatible."
|
||||
print("--list and --gateway are not compatible.")
|
||||
invalid_args = True
|
||||
else:
|
||||
|
||||
@@ -191,19 +200,19 @@ def main():
|
||||
eth_addr = eth_addr.strip().replace(":", "").replace("-", "")
|
||||
eth_addr = eth_addr.decode('hex')
|
||||
except:
|
||||
print "Invalid Ethernet address."
|
||||
print("Invalid Ethernet address.")
|
||||
sys.exit(1)
|
||||
|
||||
if len(eth_addr) != 6:
|
||||
print "Invalid Ethernet address."
|
||||
print("Invalid Ethernet address.")
|
||||
sys.exit(1)
|
||||
|
||||
if ip_type in ["Static", "static"]:
|
||||
if ip_type.lower() in ["static"]:
|
||||
ip_type = NF_IP_STATIC
|
||||
elif ip_type in ["Dynamic", "dynamic", "Dhcp", "dhcp"]:
|
||||
elif ip_type.lower in ["dynamic", "dhcp"]:
|
||||
ip_type = NF_IP_DYNAMIC
|
||||
else:
|
||||
print "--ip_type must be 'static' or 'dhcp'."
|
||||
print("--ip_type must be 'static' or 'dhcp'.")
|
||||
sys.exit(1)
|
||||
|
||||
if ip_type == NF_IP_STATIC:
|
||||
@@ -227,17 +236,17 @@ def main():
|
||||
if ip_addr is None:
|
||||
ip_addr = "0.0.0.0"
|
||||
else:
|
||||
print "--ip_addr not allowed when --ip_type=dhcp."
|
||||
print("--ip_addr not allowed when --ip_type=dhcp.")
|
||||
invalid_args = True
|
||||
if netmask is None:
|
||||
netmask = "0.0.0.0"
|
||||
else:
|
||||
print "--netmask not allowed when --ip_type=dhcp."
|
||||
print("--netmask not allowed when --ip_type=dhcp.")
|
||||
invalid_args = True
|
||||
if gateway is None:
|
||||
gateway = "0.0.0.0"
|
||||
else:
|
||||
print "--gateway not allowed when --ip_type=dhcp."
|
||||
print("--gateway not allowed when --ip_type=dhcp.")
|
||||
invalid_args = True
|
||||
|
||||
if invalid_args:
|
||||
@@ -246,33 +255,33 @@ def main():
|
||||
|
||||
|
||||
global seq
|
||||
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
|
||||
|
||||
# sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
|
||||
|
||||
iplist = enumIp();
|
||||
if len(iplist) == 0:
|
||||
print "Host has no IP address."
|
||||
print("Host has no IP address.")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
|
||||
devices = {}
|
||||
|
||||
for ip in iplist:
|
||||
print "Searching through network interface:", ip
|
||||
|
||||
|
||||
for ip in iplist:
|
||||
print("Searching through network interface:", ip)
|
||||
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||
|
||||
port = 0
|
||||
|
||||
|
||||
try:
|
||||
s.bind((ip, port))
|
||||
except socket.error, e:
|
||||
print "Bind error on send socket:", e
|
||||
except socket.error as e:
|
||||
print("Bind error on send socket:", e)
|
||||
sys.exit(1)
|
||||
|
||||
port = s.getsockname()[1]
|
||||
|
||||
port = s.getsockname()[1]
|
||||
|
||||
r = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
r.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
|
||||
@@ -281,12 +290,12 @@ def main():
|
||||
|
||||
try:
|
||||
r.bind(('', port))
|
||||
except socket.error, e:
|
||||
print "Bind error on receive socket:", e
|
||||
except socket.error as e:
|
||||
print("Bind error on receive socket:", e)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
d = Discover(s, r)
|
||||
print
|
||||
print()
|
||||
|
||||
for k in d:
|
||||
d[k]['host_ip'] = ip
|
||||
@@ -296,15 +305,16 @@ def main():
|
||||
r.close()
|
||||
|
||||
if search:
|
||||
print
|
||||
print "Found", len(devices), "Prologix GPIB-ETHERNET Controller(s)."
|
||||
print()
|
||||
print("Found", len(devices), "Prologix GPIB-ETHERNET Controller(s).")
|
||||
for key in devices:
|
||||
PrintDetails(devices[key])
|
||||
print
|
||||
print()
|
||||
else:
|
||||
if eth_addr in devices:
|
||||
|
||||
print "Updating network settings of Prologix GPIB-ETHERNET Controller", FormatEthAddr(eth_addr)
|
||||
print("Updating Prologix GPIB-ETHERNET Controller settings",
|
||||
FormatEthAddr(eth_addr))
|
||||
|
||||
device = devices[eth_addr]
|
||||
|
||||
@@ -317,11 +327,11 @@ def main():
|
||||
|
||||
try:
|
||||
s.bind((device['host_ip'], port))
|
||||
except socket.error, e:
|
||||
print "Bind error on send socket:", e
|
||||
except socket.error as e:
|
||||
print("Bind error on send socket:", e)
|
||||
sys.exit(1)
|
||||
|
||||
port = s.getsockname()[1]
|
||||
port = s.getsockname()[1]
|
||||
|
||||
r = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
r.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
@@ -331,27 +341,31 @@ def main():
|
||||
|
||||
try:
|
||||
r.bind(('', port))
|
||||
except socket.error, e:
|
||||
print "Bind error on receive socket:", e
|
||||
except socket.error as e:
|
||||
print("Bind error on receive socket:", e)
|
||||
sys.exit(1)
|
||||
|
||||
result = Assignment(s, r, eth_addr, ip_type, ip_addr, netmask, gateway)
|
||||
print
|
||||
print()
|
||||
|
||||
if len(result) == 0:
|
||||
print "Network settings update failed."
|
||||
print("Network settings update failed.")
|
||||
else:
|
||||
if result['result'] == NF_SUCCESS:
|
||||
print "Network settings updated successfully."
|
||||
print("Network settings updated successfully.")
|
||||
else:
|
||||
print "Network settings update failed."
|
||||
print("Network settings update failed.")
|
||||
else:
|
||||
print "Prologix GPIB-ETHERNET Controller", FormatEthAddr(eth_addr), "already configured for DHCP."
|
||||
print("Prologix GPIB-ETHERNET Controller",
|
||||
FormatEthAddr(eth_addr),
|
||||
"already configured for DHCP.")
|
||||
else:
|
||||
print "Prologix GPIB-ETHERNET Controller", FormatEthAddr(eth_addr), "not found."
|
||||
|
||||
print("Prologix GPIB-ETHERNET Controller",
|
||||
FormatEthAddr(eth_addr),
|
||||
"not found.")
|
||||
|
||||
|
||||
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user