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 [email protected].${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.

1 Answer 1

for i in {1..253}
    echo "Enter password for: $ip"
    read pswd
    case "$pswd" in
        *) password=$pswd;;
    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

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.

  • 1
    Quote your variables...
    – jasonwryan
    Nov 11, 2015 at 0: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, 2015 at 0:22
  • All of them. If a password contains spaces, your script will break...
    – jasonwryan
    Nov 11, 2015 at 0: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, 2015 at 0:28
  • @Lizardx it's good to just get in the habit of always doing it. See unix.stackexchange.com/questions/171346/… for a great discussion about it Nov 11, 2015 at 0:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .