-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for GNU hostname command #42
base: master
Are you sure you want to change the base?
Conversation
Hi, |
Glad to know it works well. Thanks for taking a look! Hope it makes it in the next releases :) |
@costales I saw there is this library In def get_net_ip(self):
return ' '.join([i['addr'] for ifaceName in interfaces() for i in ifaddresses(ifaceName).setdefault(AF_INET, [{'addr':None}]) if i['addr'] and not i['addr'].startswith('127.')]) By excluding loopback ips in subnet 127.0.0.0/8 (127.0.0.1 - 127.255.255.254) and interfaces that have no IP address we can reproduce @devansh08 can you confirm this works in Arch Linux too? |
@a13ssandr0 Yes this does work on Arch for me :) But I don't think Coming to the point of using Although based on your idea (great idea btw 👍 ) I found another way to get IP addresses using the import socket
def get_net_ip(self):
return socket.gethostbyname(socket.getfqdn()) |
@devansh08 you're right, using Here you can see my physical network interface has two IPs, while the third belongs to #bash
$ hostname --all-ip-addresses
192.168.1.50 192.168.0.5 192.168.122.1 #python
>>> from netifaces import interfaces, ifaddresses, AF_INET
>>> ' '.join([i['addr'] for ifaceName in interfaces() for i in ifaddresses(ifaceName).setdefault(AF_INET, [{'addr':None}]) if i['addr'] and not i['addr'].startswith('127.')])
'192.168.1.50 192.168.0.5 192.168.122.1'
Good idea using built-ins, but your code only returns import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80)) #you could also use `('<broadcast>', 0)` instead of Google DNS
print(s.getsockname()[0])
s.close() |
Oh... My Another similar solution from the same thread: import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(0)
s.connect(('10.255.255.255', 1))
ip = s.getsockname()[0] Not sure if hitting |
@devansh08 any IP outside the machine is fine, even if it isn't reachable, because we only need to reach the local router, actual routing is not needed;
|
I noticed on clicking the "Paste your current local IP" the command
hostname --all-ip-addresses
is run. But with the GNU version of the hostname that I have pre installed on Arch Linux does not have this option. Instead it uses--ip-addresses
option. So I've added a check for valid IP addresses that the command returns (as for me the command returned an error message string) and some logic to try the GNU version's option before falling back to 127.0.0.1.Lemme know your thoughts. Thanks !
FYR, some info on the GNU version of hostname