-
-
Save erhhung/1567fe8f7e77c28e0913b751e3fc0c0e to your computer and use it in GitHub Desktop.
Perl script from eons ago for getting my local IP address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# | |
# myip - get the IP address of this host | |
# | |
# Usage: myip [-6] [prefix] | |
# myip -p | |
# | |
# -6 - get IPv6 address instead of IPv4 | |
# -p - get public IPv4 address from ISP | |
# prefix - match the first NIC having partial | |
# or full name. e.g., eth, wlan, en1 | |
# | |
# Author: Erhhung Yuan (erhhung@alum.mit.edu) | |
use feature qw(say); | |
use strict; | |
my $inet6 = 0; | |
my $prefix = shift(@ARGV); | |
if ($prefix =~ /^-(?![6p]$)/) | |
{ | |
say "myip - get the IP address of this host"; | |
say "Usage: myip [-6] [prefix]"; | |
say " myip -p"; | |
say ""; | |
say " -6: get IPv6 address instead of IPv4"; | |
say " -p: get public IPv4 address from ISP"; | |
say " prefix: match the first NIC having partial"; | |
say " or full name. e.g., eth, wlan, en1"; | |
exit; | |
} | |
if ($prefix eq "-p") { | |
my $ip = `/usr/bin/dig +short myip.opendns.com`; | |
$ip =~ s/^\s+|\s+$//g; | |
say $ip; | |
exit; | |
} | |
if ($prefix eq "-6") { | |
$inet6 = 1; | |
$prefix = shift(@ARGV); | |
} | |
my $pattern; | |
if ($prefix) { | |
$pattern = "(" . quotemeta($prefix) . "\\w*)"; | |
} else { | |
$pattern = "(\\w+)"; | |
} | |
my $dev; | |
my @data = `/sbin/ifconfig`; | |
foreach my $line (@data) | |
{ | |
# find line with matched device | |
if ($line =~ /^$pattern:?\s+/i) | |
{ | |
if ($1 =~ /^(lo|docker)/) { | |
# skip loopback & docker | |
$dev = undef; | |
} else { | |
$dev = $1; | |
} | |
} | |
elsif ($line =~ /^\w+/) { | |
$dev = undef; | |
} | |
elsif ($dev && ((!$inet6 && $line =~ /\s+inet\s(addr:\s?)?([0-9.]+)/) || | |
($inet6 && $line =~ /\s+inet6\s(addr:\s?)?([0-9a-f:\/.]+)/i))) { | |
say $2; | |
exit; | |
} | |
} | |
exit 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment