-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipgen
More file actions
executable file
·46 lines (36 loc) · 734 Bytes
/
Copy pathipgen
File metadata and controls
executable file
·46 lines (36 loc) · 734 Bytes
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
#!/bin/sh
help_and_exit() {
cat << EOF
Usage: $0 NAME [PREFIX]
Naming things is hard, so is IP allocation.
Generate an IP address from name and a prefix.
Example:
> $0 foo 10.1
10.1.172.189
> $0 bar 192.168.1
192.168.1.55
EOF
exit 1
}
name="$1"
prefix="$2"
if [ ! -z "$prefix" ]; then
prefix="${prefix%.}."
fi
dots=$(echo "$prefix" | grep -oF . | wc -l)
segs=$((4 - $dots))
if [ -z "$name" ]; then
help_and_exit
fi
ip="$prefix"
hash=$(echo -n "$name" | md5sum)
for i in $(seq $segs); do
byte="${hash:0:2}"
hash="${hash:2}"
if [ "$i" != 1 ]; then
ip="$ip."
fi
current=$(printf '%d' 0x$byte)
ip="$ip$current"
done
echo "$ip"