0

i want remote log for several hosts and check some configuration. for that i wrote a small script. it have two files. one for ip address list and other for script.

cat iplist
192.168.1.10
192.168.1.11
192.168.1.12

My script:

cat script.sh
#!/bin/bash
while read ip;
 do
    sshpass -p 'root' ssh  root@$ip "hostname"
    echo "$ip" 
done < iplist

above script only display one hostname and one IP only

1 Answer 1

1

ssh reads in from stdin and consumes all of your input file ip_list when its run the first time. You can use -n option to ssh command line to instruct it to not so.

Other ways are to use a different file descriptor for reading from ip_list or redirect input with ... </dev/null.

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

2 Comments

i didn't get it. could you please explain
You pass -n option such as ssh -n root@$ip "hostname". Otherwise, ssh would read in standard input (in your case, you are passing input from a file, so it'd read in the whole file in one go). See the man page and read -n's description: man7.org/linux/man-pages/man1/ssh.1.html

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.