Complex tools for a simple task
It's common for us, users and admins, to check what is the ip of the host we are logged in to. Currently on Linux, this task is commonly done by the ip and ifconfig tools. However, I find them way too complex for the simple task of checking what's the IP. their output is full of confusing, irrelevant details, and they have tons of command-line arguments.
But you can pipe it all through grep, sed and awk!
Well... I'm not even going to explain why it isn't friendly.
myip for the rescue
So after I failed to find a simple tool for displaying IPs, I started writing it myself. Some command line samples:
1. Show primary IP:
# myip
192.168.1.100
2. Show all IPs:
# myip --all
1.2.3.4
192.168.1.100
192.168.122.1
3. Show IP of a specific interface:
# myip eth0
1.2.3.4
More info & download on the github page, Check it out and submit pull requests 🙂
You could probably use the
socket
module to do most of this without executing external commands. That will also give the added benefit of working in non-Linux operating systems (BSD, OS X, Solaris, etc ...). It would make the code much simpler as well.Evgeny, thanks for the tip. indeed I wasn't fond of running external system commands. My favorite method would've been using /sys or /proc, but they don't seem to hold IP interface info (why?!). I was actually surprised to see that 'ip addr' fetches its data through Netlink. (Ugly. But I'll consider using it as well).
As for socket, a friend already suggested that, I'll certainly look into it. I believe it'll have some downsides (probably cannot fetch all IPs present on the system, and I planned adding an option to display interface names alongside the IPs), but maybe it's worth it.
As for cross-platform support, good point. If socket will get it done it's perfect. If not, I believe I'll make OS support more plugin-like, focus on Linux & OS X (by far more popular than other unices), and wait for pull requests for support for all those weird OSes 🙂
Good idea Oren. At this point, I do it automatically with my eyes, but you are right, it is kinda stupid to eye-grep the ifconfig output just to get my IP address.
Consider uploading it to pypi so we can install easy install it with pip 🙂
Thanks Pablo, and that's right, I'll upload to pypi soon, just wasn't sure it's ready.
I have much simpler script (less then 10 lines) in perl that dos not parse outputs. instead it calls getpeername on UDP socket that gets a remote address (but doesn not really connects it. because it's UDP).
my script does not accept interface names, but it does accept remote IP, which can also be an IP-range, so it's very easy to find out "what IP do I have when connecting in that network ?". my script is also fairly cross-platform, because it uses common socket functionality, not system specific external tools.
Yoram, sounds super cool! link?
Here is an extended version (with POD, so --help will work).
#!/usr/zlocal/bin/perl
use strict;
use warnings;
use Getopt::Long qw/HelpMessage/ ;
use IO::Socket::INET;
my $peer = {PeerAddr => inet_ntoa(INADDR_BROADCAST),Broadcast => -1};
GetOptions (
'help' => sub { HelpMessage(-verbose => 1) },
'broadcast' => \$peer->{Broadcast},
'peer-addr:s' => sub {$peer->{PeerAddr} = $_[1]; $peer->{Broadcast} = 0 if $peer->{Broadcast} == -1}
);
my $s = IO::Socket::INET->new (Proto => 'udp',%$peer);
die "$!\n" unless $s;
printf ("Address is %s\n",$s->sockhost);
__END__
=head1 NAME
checkip.pl - display your ip address
=head1 SYNOPSIS
checkip.pl [--help]|[--peer-addr=]
=head1 OPTIONS
=over
=item --help
Show this help
=item --peer-addr
The address of remote host. can be used to ensure result for specific interface.
=item --broadcast
Broadcas the address (usefull if it is actually a group, such as 192.168.0.0).
Someone noticed me that I posted the script with local-specific shabeng, that was really supid thing to do. if you use this script, please modify it to have a standard shabeng....