I have a script that runs on bash, in an RHEL server that connects to thousands of nodes and returns with the values of some 5 executed commands - It collects info from RHEL Servers only.
It works fine, but the problem is that some nodes end up freezing when I run the following commands:
rpm -q <package-name>
rpm --queryformat '%{installtime:date} %{name}\n' -q <package-name>
Now, since this positively stops my script, I want to set a timeout for the ssh command, and exit the ssh session if it keeps waiting for some remote command to execute for too long [say 10 seconds]. I want to timeout and exit that ssh session and move on to the next node when this happens. How do i do this?
Here's the part of the script where I currently pull out the information and store it in the variable called dump [Please ignore my poor scripting, Im new at this]
dump=$(ssh -o ServerAliveCountMax=1 -o ServerAliveInterval=10 -o ConnectTimeout=10 -o BatchMode=yes $i "cat /proc/meminfo | grep -i \"memtotal\" | cut -d \":\" -f2 | tr -d \" \" | tr -d \"kB\"; cat /etc/redhat-release | cut -d \" \" -f7; dmidecode | grep -i \"prod\" | grep -vi \"desktop\" | grep -iv \"id\" | cut -d \" \" -f3,4| tr \" \" \"_\" ; uptime | cut -d \" \" -f4,5 | tr \" \" \"_\" | tr -d \",\"; service kdump status 2>/dev/null | tr \" \" \"_\";");
Is there anyway to time this out if it keeps going for too long?
WHAT I ALREADY TRIED:
(ssh -q -o Batchmode=yes -o PasswordAuthentication=no -o ConnectTimeout=1 $i "rpm --queryformat '%{installtime:date} %{name}\n' -q \"kexec-tools\" | cut -d \" \" -f1,2,3,4|tr \" \" \"_\"" > /dev/null) & pid=$!
(sleep 10 && kill -HUP $pid ) 2>/dev/null & watcher=$!
if wait $pid 2>/dev/null; then
pkill -HUP -P $watcher
wait $watcher
else
echo -e "$i Unable to ssh" >> res && continue
fi
However, this way, I am not being able to store the result of the remote rpm command.
Any help is extremely appreciated.