Python Libraries and Network Connection

Hello all,

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.

Thanks for any help!

Try this:

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)
2 Likes

This will work! Thanks so much. I had to alter it a little for what I need, but the NetworkInterface library is what I needed all along.

Phil,

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.42.81"

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)
			elif (anyExternalIP == False):
				system.tag.write("Diagnostics/Ethernet Connections/connection_ExternalPort",0)
			if (addr2.find(compIP)==True):
				system.tag.write("Diagnostics/Ethernet Connections/connection_ComputerIP",1)
			elif (addr2.find(compIP) == False):
				system.tag.write("Diagnostics/Ethernet Connections/connection_ComputerIP",0)
			if (addr2.find(cogIP)==True):
				system.tag.write("Diagnostics/Ethernet Connections/connection_CognexCard",1)
			elif (addr2.find(cogIP)==False):
				system.tag.write("Diagnostics/Ethernet Connections/connection_CognexCard",0)	
#anyExternalIP = any(c in addr2 for c in arr)
#allExternalIP = all(c in addr2 for c in arr)
#if (anyExternalIP==True):
#	system.tag.write("Diagnostics/Ethernet Connections/connection_ExternalPort",1)
#	print (anyExternalIP)
#if (anyExternalIP==False):
#	system.tag.write("Diagnostics/Ethernet Connections/connection_ExternalPort",0)
#	print (anyExternalIP)

What do you see in this script that would be preventing it from running on a Tag Change?

I am having issues with iterations and receiving a proper “addr2” in which I can use the any() function on. :frowning:

I got it!

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)
2 Likes

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 =)