Hello
i want to create this list:
2a05:b80:0:235::9f/1159
2a05:b80:0:235::a0/1160
2a05:b80:0:235::a1/1161
2a05:b80:0:235::a2/1162
2a05:b80:0:235::a3/1163
so write this shell as well:
#Global VAR
STR=159
END=200
#INI NET IPV6 PART
SUM1=`expr $END - $STR`
for ((i=STR;i<=END;++i)); do
echo -e "2a05:b80:0:235"$x >> host-part.txt
done
#INI HOST IPV6 PART
STR2=159
END2=200
SUM2=`expr $END2 - $STR2`
for ((i=STR2;i<=END2;++i)); do
x=$( printf "%x" $i ) ; echo $x
echo -e "::"$x >> netpart.txt
done
if [ $SUM1 == $SUM2 ]; then
paste -d '' host-part.txt netpart.txt > IP-LIST.txt
## creating file for port ranges ##
STR3=1159
END3=1200
for ((i=STR3;i<=END3;++i)); do
echo -e "/"$i >> port.txt
done
SUM3=`expr $END3 - $STR3`
if [ $SUM1 == $SUM3 ]; then
paste -d '' IP-LIST.txt port.txt > final-config
else
echo "Error on sum ip port"
fi
it do what i want but the question is how can i ask user to enter number instead of having three SRT and END variable:
STR
STR2
STR3
END
END2
END3
and how can i make it More functional and principled?
RudiC
January 31, 2018, 11:35am
2
Some comments:
in your host-part.txt
creation loop, you're using the undefined $x
variable.
the final fi
is missing for the first if
construct, and there is no error handling (else branch) either.
EDIT: How about
read -p "enter STR1 END1 STR2 END2: " STR1 END1 STR2 END2
if (( END2 - STR2 != END1 - STR1 )); then echo different ranges.; fi
DLT=$((STR2 - STR1))
for ((i=STR1;i<=END1;++i))
do printf "2a05:b80:0:235::%X/%d\n" $i $((i+DLT))
done
enter STR1 END1 STR2 END2: 159 200 1159 2000
2a05:b80:0:235::9F/1159
2a05:b80:0:235::A0/1160
2a05:b80:0:235::A1/1161
2a05:b80:0:235::A2/1162
2a05:b80:0:235::A3/1163
.
.
.
tx
but output is just a line:
[root@localhost ~]# sh mm
enter STR1 END1 STR2 END2:159 200 1159 2000
2a05:b80:0:235::0/0
RudiC
January 31, 2018, 12:43pm
4
My fault, sorry; I used a line from your scipt above instead of mine.
Use
for ((i=STR1;i<=END1;++i))
in the for
loop.
RudiC
January 31, 2018, 12:52pm
5
Or, if your shell (which, btw, you fail to mention) allows for "process substitution" ( so - don't use sh
to run the script!):
printf "2a05:b80:0:235::%X/%d\n" $(paste <(seq $STR1 $END1) <(seq $STR2 $END2))
2a05:b80:0:235::9F/1159
2a05:b80:0:235::A0/1160
2a05:b80:0:235::A1/1161
2a05:b80:0:235::A2/1162
2a05:b80:0:235::A3/1163
still oneline is output of
read -p "enter STR1 END1 STR2 END2:"
if (( END2 - STR2 != END1 - STR1 )); then echo different ranges.; fi
DLT=$((STR2 - STR1))
#for ((i=STR1;i<=END1;++i))
for ((i=STR1;i<=END1;++i))
do
printf "2a05:b80:0:235::%X/%d\n" $i $((i+DLT))
$END2))
done
let me say again what this script do:
this script try to get range of ip and range of port then point each port to ip and save them into one file,
2a05:b80:0:235::XX
is the main ip and this script try to create list of ipv6
my code try to create list of ips then list of ports then paste them together into one file,
---------- Post updated at 04:24 PM ---------- Previous update was at 01:15 PM ----------
oh,
printf "2a05:b80:0:235::%X/%d\n" $i $((i+DLT))
%X
>>>> %x
nimafire:
still oneline is output of
read -p "enter STR1 END1 STR2 END2:"
if (( END2 - STR2 != END1 - STR1 )); then echo different ranges.; fi
DLT=$((STR2 - STR1))
#for ((i=STR1;i<=END1;++i))
for ((i=STR1;i<=END1;++i))
do
printf "2a05:b80:0:235::%x/%d\n" $i $((i+DLT))
$END2))
done
First line is missing list of variables to read, replace line 1 with:
read -p "enter STR1 END1 STR2 END2:" STR1 END1 STR2 END2
Remove line highlighted in red
Edit: Below is a example using bash with user friendly prompting (entering IPs in Hex with validation):
#!/bin/bash
hex_to_dec() {
[[ $2 =~ ^([[:xdigit:]])+$ ]] &&
printf -v $1 "%d" $((16#$2)) &&
(($1 <= 16#FFFF ))
}
while true; do
read -p "Start IP in hex: " sx
hex_to_dec STR1 $sx && break
echo "Invalid Start IP please reenter"
done
while true; do
read -p "End IP in hex: " ex
hex_to_dec END1 $ex && ((END1 >= STR1)) && break
echo "Invalid End IP please reenter"
done
read -p "Start port: " STR2
DLT=$((STR2 - STR1))
for ((i=STR1;i<=END1;++i))
do
printf "2a05:b80:0:235::%x/%d\n" $i $((i+DLT))
done