Line | |
---|
1 | #
|
---|
2 | # inet2hex - Internet address conversion, dotted-decimal to hex
|
---|
3 | #
|
---|
4 | inet2hex ()
|
---|
5 | {
|
---|
6 | local IFS
|
---|
7 |
|
---|
8 | IFS=.
|
---|
9 | set -- $1
|
---|
10 |
|
---|
11 | if (( $# != 4 )); then
|
---|
12 | echo "inet2hex: incorrect input format: $1" >&2
|
---|
13 | echo "inet2hex: usage: inet2hex XX.XX.XX.XX" >&2
|
---|
14 | return 2
|
---|
15 | fi
|
---|
16 |
|
---|
17 | printf "0x%02x%02x%02x%02x\n" $1 $2 $3 $4
|
---|
18 | }
|
---|
19 |
|
---|
20 | #
|
---|
21 | # hex2inet - Internet address conversion, hex to dotted-decimal
|
---|
22 | #
|
---|
23 | hex2inet ()
|
---|
24 | {
|
---|
25 | local x1 x2 x3 x4
|
---|
26 | local rev
|
---|
27 |
|
---|
28 | OPTIND=1
|
---|
29 | while getopts "r" o
|
---|
30 | do
|
---|
31 | case "$o" in
|
---|
32 | r) rev=true;;
|
---|
33 | *) echo "hex2inet: usage: hex2inet [-r] [0x]XXXXXXXX" >&2 ; exit 2;;
|
---|
34 | esac
|
---|
35 | done
|
---|
36 | shift $(( $OPTIND - 1 ))
|
---|
37 |
|
---|
38 | case "$1" in
|
---|
39 | 0x*) h=${1#??} ;;
|
---|
40 | *) h=$1 ;;
|
---|
41 | esac
|
---|
42 |
|
---|
43 | if (( ${#h} != 8 )); then
|
---|
44 | echo "hex2inet: $h not in inet format" >&2
|
---|
45 | echo "hex2inet: usage: hex2inet [0x]XXXXXXXX" >&2
|
---|
46 | return 2
|
---|
47 | fi
|
---|
48 |
|
---|
49 | x1=$(( 0x${h:0:2} ))
|
---|
50 | x2=$(( 0x${h:2:2} ))
|
---|
51 | x3=$(( 0x${h:4:2} ))
|
---|
52 | x4=$(( 0x${h:6:2} ))
|
---|
53 |
|
---|
54 | if [ -z "$rev" ] ; then
|
---|
55 | printf "%d.%d.%d.%d\n" $x1 $x2 $x3 $x4
|
---|
56 | else
|
---|
57 | printf "%d.%d.%d.%d\n" $x4 $x3 $x2 $x1
|
---|
58 | fi
|
---|
59 | return 0
|
---|
60 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.