0

I have a simple for loop one liner I use to check for things across a number of servers that have the same password set. I want to develop this one liner into a script that logs into a cluster of servers via IP address, prompts for a password and performs a command. Such as restarting a service. This is what I use:

    for i in {1..253}
do sshpass -p PASSWORDHERE ssh -o StrictHostKeyChecking=no username@192.168.1.${i} 'hostname
echo "Checking if foo.log exists: `ls -lh /var/log/foo.log | wc -l`"
echo "Checking if bar.log is present: `ls -lh /var/log/bar.log | wc -l`"
' 2>/dev/null; echo ""; done

My script-fu is weak and I really don't have much of a clue where to start. Incidentally I want to achieve this with a basic set of tools. I'm not able to install anything third party.

Any help appreciated.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

1 Answers1

1
for i in {1..253}
do 
    ip=192.168.1.${i}
    echo "Enter password for: $ip"
    read pswd
    case "$pswd" in
        *) password=$pswd;;
    esac
    sshpass -p "$password" ssh -o StrictHostKeyChecking=no username@$ip 'hostname
    echo "Checking if foo.log exists: `ls -lh /var/log/foo.log | wc -l`"
    echo "Checking if bar.log is present: `ls -lh /var/log/bar.log | wc -l`"
    ' 2>/dev/null
done

That should work. Remember, ctrl + c will kill this loop if you get tired of it running during testing, or just use a smaller range to debug it, like 1 to 5.

Lizardx
  • 3,058
  • 17
  • 18
  • 1
    Quote your variables... – jasonwryan Nov 11 '15 at 00:18
  • which ones? I'm not providing a robust full solution, just a quick thing to someone asking. Obviously if this were mine I'd be doing a lot more testing and checking of data before sending anything anywhere. But then again, I'd be getting paid to do it, lol. – Lizardx Nov 11 '15 at 00:22
  • All of them. If a password contains spaces, your script will break... – jasonwryan Nov 11 '15 at 00:24
  • Calling this a 'script' is a bit much, heh, I'd call it a quick hack done in an attempt to avoid doing real work. Quoted them. It's useful posting here to get a sense of the range of odd things people do that I would personally never consider, like using a space in a password. – Lizardx Nov 11 '15 at 00:28
  • @Lizardx it's good to just get in the habit of always doing it. See http://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells for a great discussion about it – Eric Renouf Nov 11 '15 at 00:52
  • Yeah, honestly, the reason I decided to stop being a google user and to start being a contributor on these so/se sites was precisely to be reminded of what I do and do not know, bad habits, etc. My real coding stuff tends to be somewhat absurdly robust. My personal pet shell peeve is not quoting strings. – Lizardx Nov 11 '15 at 01:07
  • By the way, re that link, personally I view the use of shell in any web facing environment as very close to insanity (I was astounded when I read on shellshock, I could not understand why anyone would even do that type of thing in the first place, for example), so most of the very good points the posters in that thread make really don't apply to normal non web facing shell things. However, in this here case, quoting is clearly required to avoid error. – Lizardx Nov 11 '15 at 01:10