IP generator bash script help

Hello all, I am working on a script to randomly generate IP addresses and then try to ssh to all of them. When I run the script I get : ssh printf %d.%d.%d.%dn 141 138 75 224

My code:

for i in {1..100}
do
 STR="printf "%d.%d.%d.%d\n" "$((RANDOM % 256))" "$((RANDOM % 256))" "$((RANDOM % 256))" "$((RANDOM % 256))""
done
while true; do
    read -p "Start SSH connection? " yn
    case $yn in
        [Yy]* ) echo ssh $STR; break;;
        [Nn]* ) exit;;
        * ) echo "Invalid Input";;
    esac
done

Could you please help me?

Thanks!

This post was flagged by the community and is temporarily hidden.

You forgot to execute the command in the variable $STR. If you do:

# `$STR`
ssh 159.202.233.107
#
#!/bin/bash
RANDOM=25
for i in {1..100}
do
        STR="printf %d.%d.%d.%d\n $((RANDOM % 256)) $((RANDOM % 256)) $((RANDOM % 256)) $((RANDOM % 256))"
done
while true; do
    read -p "Start SSH connection? " yn
    case $yn in
        [Yy]* )
                echo ssh `$STR`
                ;;
        [Nn]* )
                exit
                ;;
        * )
                echo "Invalid Input"
                ;;
    esac
done
1 Like

Thank you :slight_smile: this helped a lot!

would there be a way to log the results of the connections?

Fixed version

for i in {1…100}
do
STR=$(printf “%d.%d.%d.%d\n” “$((RANDOM % 256))” “$((RANDOM % 256))” “$((RANDOM % 256))” “$((RANDOM % 256))”)
done
while true; do
read -p "Start SSH connection? " yn
case $yn in
[Yy]* ) echo ssh $STR; break;;
[Nn]* ) exit;;
* ) echo “Invalid Input”;;
esac
done

What do you mean log the results? When you ssh into a box you, if provided the correct credentials, will be let into the box?

echo ssh $STR this doesn’t run ssh, it just echoes it to the screen. STR="printf "%d.%d.%d.%d\n" this doesn’t run printf it is literally that string. You need command substitution ("$(printf ...)")

This topic was automatically closed after 121 days. New replies are no longer allowed.