Using SSH to grep keywords from multiple servers Using SSH to grep keywords from multiple servers shell shell

Using SSH to grep keywords from multiple servers


Here is an example:

KEYWORD=fooSERVERLIST=Linux.prod.datOUTPUTLIST=output.datfor host in $(cat ${SERVERLIST}); do    if [[ -n "$(ssh ${host} grep '${KEYWORD}' /etc/hosts && echo Y)" ]]; then        echo ${host} >> ${OUTPUTLIST}    fidone


Try GNU parallel

parallel --tag ssh {} grep -l "KEYWORD" /etc/hosts :::: Linux.prod.dat

parallel run command multiple times substituting{}with lines from Linux.prod.dat file.--tag switch adds value from the Linux.prod.dat on the beginning of the file. So, the output of the command will look like:

server1 /etc/hostsserver5 /etc/hostsserver7 /etc/hosts

Where server1, server5, etc. will be names of the servers where /etc/hosts contains KEYWORD