-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipcalc
More file actions
executable file
·41 lines (36 loc) · 792 Bytes
/
ipcalc
File metadata and controls
executable file
·41 lines (36 loc) · 792 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
#!/bin/bash
help() {
echo "Usage: ipcalc [HHHHHHHH | HH.H.HH.H | DDDDDDDDDDDD | DDD.DD.D.DDD]"
echo "Convert IPv4-Adresses HEX <--> DEC"
exit 1
}
[ "$1" == "--help" ] && help
if [[ $1 == *.*.*.* ]] ; then
IN1=$(echo $1 | cut -d. -f1)
IN2=$(echo $1 | cut -d. -f2)
IN3=$(echo $1 | cut -d. -f3)
IN4=$(echo $1 | cut -d. -f4)
elif [ ${#1} == 8 ] ; then
IN1=${1:0:2}
IN2=${1:2:2}
IN3=${1:4:2}
IN4=${1:6:2}
elif [ ${#1} == 12 ] ; then
IN1=${1:0:3}
IN2=${1:3:3}
IN3=${1:6:3}
IN4=${1:9:3}
else
help
fi
if [ ${#IN1} == 3 ] || [ ${#IN2} == 3 ] || [ ${#IN3} == 3 ] || [ ${#IN4} == 3 ] ; then
printf '%02x' "$IN1"
printf '%02x' "$IN2"
printf '%02x' "$IN3"
printf '%02x\n' "$IN4"
else
printf '%d.' "0x$IN1"
printf '%d.' "0x$IN2"
printf '%d.' "0x$IN3"
printf '%d\n' "0x$IN4"
fi