PlanB
Posts: 102
Joined: Tue Oct 28, 2014 6:44 am

External ip address

Tue Jan 23, 2018 7:00 am

I like this script because it will deliver the pi external ip address without asking a website like info.io to get it
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try: s.connect(('10.255.255.255', 1)); IP = s.getsockname()[0] # doesn't even have to be reachable
except: IP = 'unknown'
finally: s.close()
print IP
Unfortunately it only works for 3G dongles not wifi dongles working through a router. Networking confuses me. Why is it usual to ask someone else for your own external ip address? Why can't the network fess up itself?

pcmanbob
Posts: 7705
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: External ip address

Tue Jan 23, 2018 11:04 am

Most 3G connections do not actually have a true wan address they have an address on your service providers network, so you are sitting behind there router in much the same way as a wifi connection to a router when using a broadband connection.

most 3g providers use CgNAT https://en.wikipedia.org/wiki/Carrier-grade_NAT
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

PlanB
Posts: 102
Joined: Tue Oct 28, 2014 6:44 am

Re: External ip address

Wed Jan 24, 2018 1:17 pm

Can someone explain the difference between

print((([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")] or [[(s.connect(("8.8.8.8", 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) + ["no IP found"])[0])

and

print((([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1], [[(s.connect(("8.8.8.8", 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) + ["no IP found"])[0])

the latter's claim to fame is it works on LANs without an internet connection, but on testing they both do.

Return to “Troubleshooting”