bash - read a list of IP address and execute commands depending on OS type -
i working on script read file having multiple ip addresses, login each system (no ssh keys involved , manually entering password), execute command depending on os type , print result.
here how ips defined in hosts.txt file
#~] cat hosts.txt 10.6.3.131 10.6.3.132 10.6.3.11 10.6.3.12
when execute below script, following errors shown. also, script not prompt 2nd , rest ips in list. causing this?
# ./hq-test1.sh root@10.6.3.131's password: awk: cmd. line:1: nr==1{print awk: cmd. line:1: ^ unexpected newline or end of string bash: line 2: [: !=: unary operator expected 10.6.3.131 == pass
this script used in test :
#!/bin/bash while read host; if ssh root@$host ' status=`awk 'nr==1{print $1}' /etc/*release` [ $status != "centos" ] '; echo "$host == fail" else echo "$host == pass" fi done < hosts.txt
your script isn't closing stdin
while running ssh
command first host of list processed.
i simplified test, awk
able check os name , temporary variable unnecessary.
while read host; if ssh -n root@$host '[ "$(awk "/centos/{print}" /etc/*release)" ] ' echo "$host == pass" else echo "$host == fail" fi done < hosts.txt
Comments
Post a Comment