0

I have tried to ssh to multiple vms. I used for loop. I want to fetch the username and IP and loop into multiple VMs.

export IFS=","
for read f1 f2 in $(cat sample.txt);
do sshpass -p 'password' ssh $f1@$f2 "uname -a" ;
done

sample.txt contains

abc,111.111.111.111

def,111.222.333.444

Looks like I am facing an error, syntax error near unexpected token f1' for read f1 f2 in $(cat LNodeInput.txt);' I have tried it in few other ways too but still throws an error.

1
  • 1
    for doesn't work that way, change to while and you'll be very close. Good luck. Commented Mar 29, 2018 at 3:59

2 Answers 2

1
#!/bin/bash
while IFS="," read -r f1 f2
do
 sshpass -p 'password' ssh "$f1@$f2" "uname -a" < /dev/null ;
done < sample.txt

be careful sample.txt should have the last line as empty line, otherwise it will not read the last line of it.

EDIT:

< /dev/null is to stop ssh from "eating" input from sample.txt. Otherwise, the command will only ssh to the first server.

Sign up to request clarification or add additional context in comments.

14 Comments

It works!! but it displays the uname of the first IP, am not sure why does it display the output of the next IP. Any suggestions? @user8470034
@Anna sorry, I didn't get what you mean. you mean it only display one result?
if that is the case, is that your file only have 2 records? do you have an extra empty line at the end of the sample.txt file? pls be careful about that(as I mentioned in my answer)
Yes, I have added the empty line. Yes, it displays only one result - uname of the first IP and not the second one. To be extra sure I tried it manually and it worked. The code seems not to pick up the IP of the next machine.
Yes understood!But facing Permission denied, please try again later.Tested it manually and it worked
|
0
for a in `cat servers`;do echo ;echo $a ; sshpass -p 'password'  ssh $a uname ; done

Like this what ever u want u can add before done.. If u want an output collected in a file add > file1 at the end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.