1

Given an input file which consists of lines of IP addresses and strings, how can I loop through each line, and execute a command using the IP address and string? An example of the command that I want to run for each line is:

ssh user@192.168.0.10 cat /etc/component10/version | grep 'Version\|Project' >> /tmp/component_ver.txt

Assume that a password is not required. I would like the script to be robust enough to answer yes if the "...(yes/no)?" prompt is encountered during login.

Sample INPUT file:

192.168.0.10 component10  
192.168.0.20 component20  
192.168.0.30 component30  
  • Please read mywiki.wooledge.org/BashFAQ/001 and remember. Random people who really aren't even remotely closed to provide a correct answer do mostly responde within 2 minutes. – Valentin Bajrami Aug 18 '16 at 14:09
  • 1
    Is the "to answer yes if the ...(yes/no)?" part of your question referring to the ssh prompting you with "The authenticity of host ..... blah-blah ..... Are you sure you want to continue connecting (yes/no)?" question ? If yes, it can be prevented by adding -o "StrictHostKeyChecking no" to your ssh command line, right after word ssh – MelBurslan Aug 18 '16 at 14:22
  • Yes, this is the message I sometimes run into. – Bernie Aug 18 '16 at 14:31
  • If the numbers in the IP address and the componentNN string are always the same (and the list is short), you could loop over just the numbers, with something like for x in 10 20 30; do ssh 192.168.0.$x cat /foo/component$x/bar | ... ; done. Or with for x in $(seq 1 10) ... for consecutive numbers. – ilkkachu Aug 18 '16 at 14:44
1

The following code will do what you want.

while read -r server _; do ssh -n -o StrictHostKeyChecking=no root@"$server" "grep -E 'Version|Project' /etc/component10/version" >> /tmp/component_ver.txt; done < serverfile
  • 1
    I wonder if "component10" is supposed to be a variable, associated to the corresponding IP? (just based on their sample input and command) – Jeff Schaller Aug 18 '16 at 14:25
  • @JeffSchaller I presume it's a copy of /etc/hosts file like in ip hostname format. I used the throw away variable _ to ignore the second field. Not really sure if that would be taken in consideration. Waiting for the OP... – Valentin Bajrami Aug 18 '16 at 14:29
  • Sorry for being vague. Yes, component10 is the string in the input file. – Bernie Aug 18 '16 at 14:32
  • @Bernie what is the purpose of that. The IP is the one you are using to connect to the remote host right? What role does the second field play in this? Also have you tried the code? Does it produce the desired result and if not, can you show what's not working? – Valentin Bajrami Aug 18 '16 at 14:34
  • 1
    I changed the _ to $component, then used that variable in the path (i.e.: /etc/$component/version) and the script worked great. As soon as I have reputation 15+, I will give you the checkmark! BTW, to answer your question, the input file is a list of hosts and the components installed on said host. For this reason, the component is pertinent to the script. – Bernie Aug 18 '16 at 15:14
0
cat INPUT|while read host component; do 
ssh -o StrictHostKeyChecking=no user@$host cat /etc/$component/version | grep 'Version\|Project' >> /tmp/component_ver.txt
done
  • For some reason, this code didn't iterate through the list in the input file. It only ran for the first line. – Bernie Aug 18 '16 at 15:16
  • Without -n, ssh reads its stdin to feed it to the remote command. That's why it works only on the first line. – Stéphane Chazelas Aug 24 '16 at 14:25
  • Note that grep will be run on the local machine. Running it on the remote machine would avoid transfering the lines you don't want. – Stéphane Chazelas Aug 24 '16 at 14:28
  • Note that the content of $component will end up being passed in the shell code interpreted by the remote shell. It may need sanitized if it contains characters special to the login shell of the remote user. (think of component='$(reboot)' for instance). – Stéphane Chazelas Aug 24 '16 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.