Skip to content
This repository was archived by the owner on Mar 30, 2020. It is now read-only.

Commit 7a14a4b

Browse files
committed
That time has come - a binary tool.
Added `ipaddr` source code, which is written in C. It can be compiled with `make build`.
1 parent 48a97a7 commit 7a14a4b

File tree

4 files changed

+175
-0
lines changed

4 files changed

+175
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
syntax: glob
2+
3+
ipaddr

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ nop:
1717
@echo "make tidy - Run the perl code through perltidy."
1818
@echo ""
1919

20+
build:
21+
gcc -o ipaddr ipaddr.c -Wall -Werror
22+
2023
clean:
2124
rm *.bak *~ || true
2225

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,32 @@ Alternatives
104104
* ..
105105

106106

107+
ipaddr
108+
------
109+
110+
Get IP addresses easily, either all IPs, all those which are IPv4/IPv6, or
111+
those for a device. Designed primarily for scripting.
112+
113+
Example:
114+
115+
$ ./ips -4
116+
lo 127.0.0.1
117+
eth0 80.68.84.102
118+
eth0 80.68.84.104
119+
120+
Or to see all IPv6 addreses on eth0:
121+
122+
$ ipaddr -6 -d eth0
123+
eth0 2001:41c8:10b:102::10
124+
eth0 fe80::216:3eff:fe08:16a4
125+
126+
**NOTE** Requires compilation via `make build`.
127+
128+
Alternatives:
129+
130+
* `ip -[46] addr show`
131+
* `ifconfig -a`
132+
107133

108134
maybe
109135
-----

ipaddr.c

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/**
2+
* ipaddr - Show the IP addresses associated with given devices.
3+
*
4+
* Usage:
5+
* ips [options]
6+
*
7+
* Options:
8+
*
9+
* --all Show all familes.
10+
* --ipv4 Show IPv4.
11+
* --ipv6 Show IPv6.
12+
* --device Show only the given device.
13+
*
14+
* Examples:
15+
*
16+
* $ ips -d eth0 -4
17+
* eth0 192.168.0.3
18+
*
19+
* $ ips -a
20+
* lo 127.0.0.1
21+
* eth0 192.168.0.3
22+
* lo ::1
23+
* eth0 fe80::62a4:4cff:fe2d:fab9
24+
* teredo 2001:0:53aa:64c:301f:13f3:4fe4:3a0b
25+
* teredo fe80::ffff:ffff:ffff
26+
*
27+
*/
28+
29+
#include <stdio.h>
30+
#include <sys/types.h>
31+
#include <ifaddrs.h>
32+
#include <netinet/in.h>
33+
#include <string.h>
34+
#include <arpa/inet.h>
35+
#include <stdlib.h>
36+
#include <getopt.h>
37+
38+
39+
int main (int argc, char * argv[])
40+
{
41+
int c;
42+
43+
/**
44+
* Flags.
45+
*/
46+
char *dev = NULL;
47+
int show_ipv4 = 0;
48+
int show_ipv6 = 0;
49+
50+
while (1)
51+
{
52+
static struct option long_options[] =
53+
{
54+
{"device", required_argument, 0, 'd'},
55+
{"all", no_argument, 0, 'a'},
56+
{"ipv4", no_argument, 0, '4'},
57+
{"ipv6", no_argument, 0, '6'},
58+
{0, 0, 0, 0}
59+
};
60+
61+
/* getopt_long stores the option index here. */
62+
int option_index = 0;
63+
64+
c = getopt_long(argc, argv, "d:a46", long_options, &option_index);
65+
66+
/* Detect the end of the options. */
67+
if (c == -1)
68+
break;
69+
70+
switch (c)
71+
{
72+
case '4':
73+
show_ipv4 = 1;
74+
break;
75+
case '6':
76+
show_ipv6 = 1;
77+
break;
78+
case 'a':
79+
show_ipv4 = 1;
80+
show_ipv6 = 1;
81+
break;
82+
case 'd':
83+
dev = strdup( optarg );
84+
break;
85+
case '?':
86+
/* getopt_long already printed an error message. */
87+
exit(1);
88+
break;
89+
90+
default:
91+
fprintf( stderr, "Unknown argument" );
92+
exit (1);
93+
}
94+
}
95+
96+
struct ifaddrs * ifAddrStruct=NULL;
97+
struct ifaddrs * ifa=NULL;
98+
void * tmpAddrPtr=NULL;
99+
100+
getifaddrs(&ifAddrStruct);
101+
102+
for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next)
103+
{
104+
/**
105+
* If we have a non-null entry.
106+
*/
107+
if ( ifa && ifa->ifa_addr )
108+
{
109+
110+
/**
111+
* And it is IPv4 and we're showing IPv4 ..
112+
*/
113+
if (ifa ->ifa_addr->sa_family==AF_INET && show_ipv4 )
114+
{
115+
116+
tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
117+
char addressBuffer[INET_ADDRSTRLEN];
118+
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
119+
120+
if ( ( dev == NULL ) ||
121+
( strcmp( dev, ifa->ifa_name ) == 0 ) )
122+
printf("%s %s\n", ifa->ifa_name, addressBuffer);
123+
}
124+
else if (ifa->ifa_addr->sa_family==AF_INET6 && show_ipv6 )
125+
{
126+
/**
127+
* IPv6 address, and we're showing them.
128+
*/
129+
tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
130+
char addressBuffer[INET6_ADDRSTRLEN];
131+
inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
132+
if ( ( dev == NULL ) ||
133+
( strcmp( dev, ifa->ifa_name ) == 0 ) )
134+
printf("%s %s\n", ifa->ifa_name, addressBuffer);
135+
}
136+
}
137+
}
138+
139+
if (ifAddrStruct!=NULL)
140+
freeifaddrs(ifAddrStruct);
141+
142+
return 0;
143+
}

0 commit comments

Comments
 (0)