Merci à Claude. Vraiment.
This commit is contained in:
commit
eaada1b6c4
4 changed files with 129 additions and 0 deletions
17
Makefile
Normal file
17
Makefile
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
CC = gcc
|
||||||
|
CFLAGS = -Wall -Wextra -std=c11 -O2
|
||||||
|
TARGET = ifaddr
|
||||||
|
SRC = getifaddr.c
|
||||||
|
|
||||||
|
.PHONY: all clean install
|
||||||
|
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
$(TARGET): $(SRC)
|
||||||
|
$(CC) $(CFLAGS) -o $@ $^
|
||||||
|
|
||||||
|
install: $(TARGET)
|
||||||
|
install -m 755 $(TARGET) /usr/local/bin/$(TARGET)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(TARGET)
|
||||||
43
README.md
Normal file
43
README.md
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
# getifaddr
|
||||||
|
|
||||||
|
Petite commande C qui retourne l'adresse IPv4 d'une interface réseau par son nom.
|
||||||
|
|
||||||
|
Utilisée par [myovh-zone](https://github.com/pazof/myovh-zone) pour mettre à jour
|
||||||
|
la zone DNS OVH avec l'IP courante d'une interface.
|
||||||
|
|
||||||
|
## Compilation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
make
|
||||||
|
# ou directement :
|
||||||
|
gcc -Wall -O2 -o ifaddr getifaddr.c
|
||||||
|
```
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo make install
|
||||||
|
# installe dans /usr/local/bin/ifaddr
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```bash
|
||||||
|
ifaddr <interface>
|
||||||
|
```
|
||||||
|
|
||||||
|
### Exemples
|
||||||
|
|
||||||
|
```bash
|
||||||
|
ifaddr eth0 # → 192.168.1.42
|
||||||
|
ifaddr enp3s0 # → 203.0.113.5
|
||||||
|
ifaddr lo # → 127.0.0.1
|
||||||
|
```
|
||||||
|
|
||||||
|
Retourne l'IP sur stdout, code retour 0 si trouvé, 1 sinon —
|
||||||
|
ce qui permet de l'utiliser directement en pipe ou depuis un process C#.
|
||||||
|
|
||||||
|
## Dépendances
|
||||||
|
|
||||||
|
POSIX uniquement : `getifaddrs(3)`, `inet_ntop(3)`. Aucune lib externe.
|
||||||
|
Fonctionne sur Linux et BSD (macOS inclus).
|
||||||
69
getifaddr.c
Normal file
69
getifaddr.c
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
/*
|
||||||
|
* getifaddr.c — retourne l'adresse IPv4 d'une interface réseau par son nom
|
||||||
|
*
|
||||||
|
* Usage : ifaddr <interface>
|
||||||
|
* Exemple : ifaddr eth0
|
||||||
|
* ifaddr enp3s0
|
||||||
|
*
|
||||||
|
* Dépendances : POSIX, getifaddrs(3)
|
||||||
|
* Compile : gcc -o ifaddr getifaddr.c
|
||||||
|
*
|
||||||
|
* Paul Schneider — reconstitué depuis pazof/myovh-zone
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ifaddrs.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
if (argc != 2) {
|
||||||
|
fprintf(stderr, "Usage: %s <interface>\n", argv[0]);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *ifname = argv[1];
|
||||||
|
struct ifaddrs *ifaddr_list = NULL;
|
||||||
|
|
||||||
|
if (getifaddrs(&ifaddr_list) == -1) {
|
||||||
|
perror("getifaddrs");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int found = 0;
|
||||||
|
for (struct ifaddrs *ifa = ifaddr_list; ifa != NULL; ifa = ifa->ifa_next) {
|
||||||
|
/* On cherche uniquement IPv4 (AF_INET) */
|
||||||
|
if (ifa->ifa_addr == NULL)
|
||||||
|
continue;
|
||||||
|
if (ifa->ifa_addr->sa_family != AF_INET)
|
||||||
|
continue;
|
||||||
|
if (strcmp(ifa->ifa_name, ifname) != 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
char buf[INET_ADDRSTRLEN];
|
||||||
|
struct sockaddr_in *sa = (struct sockaddr_in *)ifa->ifa_addr;
|
||||||
|
|
||||||
|
if (inet_ntop(AF_INET, &sa->sin_addr, buf, sizeof(buf)) == NULL) {
|
||||||
|
perror("inet_ntop");
|
||||||
|
freeifaddrs(ifaddr_list);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s\n", buf);
|
||||||
|
found = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
freeifaddrs(ifaddr_list);
|
||||||
|
|
||||||
|
if (!found) {
|
||||||
|
fprintf(stderr, "ifaddr: interface '%s' not found or has no IPv4 address\n", ifname);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
BIN
ifaddr
Executable file
BIN
ifaddr
Executable file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue