I am currently trying to create a script that will list the IP Addresses of a computer for diagnostic purposes to let the users know if the Network Adapters are indeed connected, and what the IP is.
I tried installing a few libraries, ifaddr and netifaces to be exact, but had no luck.
Could anyone point me in the correct direction? If I could install either library, I believe I could get what I need to work properly.
Also: I tried to check the status by pinging the IP that the ethernet adapter is assigned, but it appears that I can ping nearly any address on an IP address and receive a response.
from java.net import NetworkInterface
for interface in NetworkInterface.getNetworkInterfaces():
try:
hwaddr = ":".join(["%02x" % (x & 0xff) for x in interface.getHardwareAddress()])
except:
hwaddr = "unknown"
print "%s {%s}" % (interface, hwaddr)
for addr in interface.getInterfaceAddresses():
print " "+str(addr)
I can’t believe I didn’t realize it was all wrapped under the for loop, and I apparently needed to initialize the addr2 as a blank string before the for loop.
If it can help anyone, here’s the updated code.
from java.net import NetworkInterface
#IP addresses
arr = ["192.168.35.60","192.168.35.61","192.168.35.62","192.168.35.63"]
compIP = "169.254.42.80"
cogIP = "169.254.1.81"
addr2 = ""
for interface in NetworkInterface.getNetworkInterfaces():
try:
hwaddr = ":".join(["%02x" % (x & 0xff) for x in interface.getHardwareAddress()])
except:
hwaddr = "unknown"
print "%s {%s}" % (interface, hwaddr)
for addr in interface.getInterfaceAddresses():
print " "+str(addr)
addr2 += str(addr)
anyExternalIP = any(c in addr2 for c in arr)
if (anyExternalIP == True):
system.tag.write("Diagnostics/Ethernet Connections/connection_ExternalPort",1)
else:
system.tag.write("Diagnostics/Ethernet Connections/connection_ExternalPort",0)
if (addr2.find(compIP) !=(-1)):
system.tag.write("Diagnostics/Ethernet Connections/connection_ComputerIP",1)
else:
system.tag.write("Diagnostics/Ethernet Connections/connection_ComputerIP",0)
if (addr2.find(cogIP)!=(-1)):
system.tag.write("Diagnostics/Ethernet Connections/connection_CognexCard",1)
else:
system.tag.write("Diagnostics/Ethernet Connections/connection_CognexCard",0)
Thats right, you were changin a variable that didn’t exist. Python has no problem creating variables on the go but here you were creating the variable as a reference to itself (when it doesnt exist yet).
P.s. even if its a script that will be done on a tag change, if you test the code in the script editor you’ll most likely find all the problems of this kind =)