I am trying to understand best way to run multiple command on a remote host with ssh in a parallel way and Display the output on the screen while squeezing the warning and errors on the screen.
This post is mainly in connection with previous post where KamilCuk answered the POST with some beautiful tricks however i have some more open questions.
Below code works for the given commands, however few of my systems really old and do not understand the nproc command to get the number of processor on the system hence i need to use grep -c processor /proc/cpuinfo which is not working if i do so in the current solution like tmp=$(ssh "$server" bash -c 'grep -c "^processor" /proc/cpuinfo; free -g').
also would be look to incorporate conditions like if nproc is there then use the file /proc/cpuinfo to count the CPU, this is Just one thing similar to many other.
So, what i am looking forward if there are more ways to do it if you have different subsets of command where you need to get values from commands and files.
#!/bin/bash
read -rsp $'Please Enter password below: ' SSHPASS
export SSHPASS
work() {
server=$1
# ONE connection
tmp=$(ssh "$server" bash -c 'nproc; free -g')
# parsing later
cpu_info=$(<<<"$tmp" awk 'NR==1')
mem_info=$(<<<"$tmp" awk '/Mem:/{printf $2}')
swap_info=$(<<<"$tmp" awk '/Swap:/{printf $2}')
# outputting
printf "%-40s %5s %5s %5s\n" "$server" "$cpu_info" "$mem_info" "$swap_info"
}
export -f work
< /home/user1/mem xargs -P0 -n1 -d'\n' bash -c 'work "$@"' _
I tried Below where the idea is borrowed from But it just prints the SEREVR name and nothing else:
#!/bin/bash
read -rsp $'Please Enter password below: ' SSHPASS
export SSHPASS
for SERVER in '$(cat /home/user1/mem)'
do
sshpass -e ssh -q -t -oStrictHostKeyChecking=no $SERVER << EOF
cpu_info=$(grep processor /proc/cpuinfo | awk 'NF==3{count++} END {printf count}')
mem_info=$(free -g | awk /Mem:/{printf' '})
swap_info=$(free -g | awk /Swap:/{printf' '})
printf "%-40s %5s %5s %5s\n" "$server" "$cpu_info" "$mem_info" "$swap_info"
EOF
done | tee 2>/dev/null
I may have multiple mistakes please excuse me for the same just trying to learn.
EDIT Note:
I Know there will be a definite questions why not use ssh keys or already existed tooling like ansible , salt etc, the reason is only , due to company security policies somewhat we can not use any of them else there is no way re-inventing the wheel, however we are using Ansible and other tooling heavily in the normal environment.
tmp=$(ssh "$server" bash -c '"grep -c ^processor /proc/cpuinfo; free -g"')Also in your last script, you should use'EOF'to avoid expansion be done locally.ssh user@IP 'command1 args1 && command2 args2'[I put the quotes to force eval at the remote machine, otherwise you have to be very careful with quoting]keys&tools such as ansible. However,in Parallel, as you see the First code provided executes theprallel threadsfor ssh connections withxargs.xargs -PN- note to self "read more carefully" - haven't done that before (cute parallelization).. -P0 you possible might want to nice/renice things so your interactive process still has control of the box. ACTUALLY you do want to control how many concurrent outbound connections you have [corporate firewall might get upset otherwise] - so do -P N where N>0