1

I have simple script to check for uptime of bunch of hosts. But ssh command is taking 'uptime' command also as a hostname and throwing error after first iteration.

while read line
do

  echo "Turning off Monitoring for Host: $line"
  monitoring_off

  check_uptime $line

done < "$HOSTFILE"

check_uptime() {
 ssh $1 'uptime'
}

Here is the output of my script execution:

*****************************
14:08:44 up 203 days,  2:34,  1 user,  load average: 1.22, 1.11, 0.79

ssh: Could not resolve hostname uptime: nodename nor servname provided, or not known

Could you please me in locating my mistake in here

user518730
  • 11
  • 1

1 Answers1

2

This is essentially a duplicate of https://stackoverflow.com/questions/13800225/shell-script-while-read-line-loop-stops-after-the-first-line

Taken from the answer:

The problem is that [the function] runs ssh commands and by default ssh reads from stdin which is your input file. As a result, you only see the first line processed, because ssh consumes the rest of the file and your while loop terminates.

To prevent this, pass the -n option to your ssh command to make it read from /dev/null instead of stdin.

check_uptime() {
 ssh -n $1 'uptime'
}
Wraezor Sharp
  • 406
  • 2
  • 7