1

I am making a Bash script that randomly generates and then try's to connect to an IP address. I cannot figure out how to make the SSH variable work to connect to the listed IP addresses.

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

I tried to assign a variable to the IP generator, so when the user hits yes. It will input the randomly generated Ip addresses. When it runs though, all I get is this: ssh printf %d.%d.%d.%dn 141 138 75 224

Could you please help me?

-CKJones

Cyrus
  • 84,225
  • 14
  • 89
  • 153
CKJones
  • 11
  • 2
  • 2
    `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 ...)"`) – erik258 Mar 06 '23 at 15:19
  • 2
    Variable assignment is not correct, it should be `STR="$(printf ....)`. Also, a pipe might be missing between done and while – LMC Mar 06 '23 at 15:21
  • 1
    why the for loop? you are discarding 99 addresses – Diego Torres Milano Mar 06 '23 at 17:03
  • @erik258 Beter under bash, use: `printf -v STR '%d.%d.%d.%d' $((RANDOM%256)){,,,}`. The 3 comas will repeat `$(...)` – F. Hauri - Give Up GitHub Mar 06 '23 at 17:17

0 Answers0