-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathip
executable file
·77 lines (70 loc) · 1.04 KB
/
ip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/perl
#
# output local ip interfaces (all by default)
#
# -samy kamkar
#
# usage: ip [iface | 'gw']
use strict;
use Socket;
my $IFCONFIG = '/sbin/ifconfig';
my $eth = lc shift;
# ip
if ($eth =~ /^(\d+\.){3}\d+$/)
{
print "$eth\n";
exit;
}
# gateway or no interface/host specified
elsif (!$eth || $eth eq 'gateway' || $eth eq 'gw')
{
foreach (`/usr/sbin/netstat -nr`)
{
if (/^default\s+(\S+)/)
{
if ($eth eq 'gw' || $eth eq 'gateway')
{
print "$1\n";
exit;
}
else
{
print "gateway: $1\n";
last;
}
}
}
}
# hostname (use inet_ntoa to support /etc/hosts + resolution)
elsif (`$IFCONFIG` !~ /^$eth:/m)
{
my $inet = inet_aton($eth);
if ($inet)
{
my $addr = inet_ntoa($inet);
print "$addr\n";
# print ((`dig +short $eth`)[0]);
exit;
}
}
# ?
else { die "unsupported arg: $eth (@ARGV)\n"; }
my $card;
foreach (`$IFCONFIG -a`)
{
if (/^(\w+):/)
{
$card = $1;
}
elsif (/inet (\S+)/)
{
if ($eth && lc $card eq $eth)
{
print "$1\n";
}
elsif (!$eth)
{
print "$card: $1\n";
}
}
}