# Python Libraries and Network Connection

**URL:** <https://forum.inductiveautomation.com/t/python-libraries-and-network-connection/17534>\
**Category:** General Discussion\
**Tags:** python, scripting\
**Created:** [March 28, 2018, 7:12pm UTC](https://forum.inductiveautomation.com/t/python-libraries-and-network-connection/17534 "2018-03-28T19:12:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lautry](https://avatars.discourse-cdn.com/v4/letter/l/a8b319/32.png) [@lautry](https://forum.inductiveautomation.com/u/lautry)\
**Post date:** [March 28, 2018, 7:12pm UTC](https://forum.inductiveautomation.com/t/python-libraries-and-network-connection/17534/1 "2018-03-28T19:12:09Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 28, 2018, 7:41pm UTC](https://forum.inductiveautomation.com/t/python-libraries-and-network-connection/17534/2 "2018-03-28T19:41:32Z")

</div>

Try this:

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![lautry](https://avatars.discourse-cdn.com/v4/letter/l/a8b319/32.png) [@lautry](https://forum.inductiveautomation.com/u/lautry)\
**Post date:** [March 28, 2018, 8:15pm UTC](https://forum.inductiveautomation.com/t/python-libraries-and-network-connection/17534/3 "2018-03-28T20:15:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lautry](https://avatars.discourse-cdn.com/v4/letter/l/a8b319/32.png) [@lautry](https://forum.inductiveautomation.com/u/lautry)\
**Post date:** [March 29, 2018, 2:47pm UTC](https://forum.inductiveautomation.com/t/python-libraries-and-network-connection/17534/4 "2018-03-29T14:47:12Z")

</div>

Phil,

```auto
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. ☹

---

<div class="post-metadata">

**Author:** ![lautry](https://avatars.discourse-cdn.com/v4/letter/l/a8b319/32.png) [@lautry](https://forum.inductiveautomation.com/u/lautry)\
**Post date:** [March 29, 2018, 4:06pm UTC](https://forum.inductiveautomation.com/t/python-libraries-and-network-connection/17534/5 "2018-03-29T16:06:42Z")

</div>

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.

```auto
	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)

```

---

<div class="post-metadata">

**Author:** ![albetro.15](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/albetro.15/32/3922_2.png) [@albetro.15](https://forum.inductiveautomation.com/u/albetro.15)\
**Post date:** [March 30, 2018, 12:48pm UTC](https://forum.inductiveautomation.com/t/python-libraries-and-network-connection/17534/6 "2018-03-30T12:48:15Z")

</div>

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