0

I am working on a bash script that uses pssh to run external commands, then join the output of the commands with the IP of each server. pssh has an option -o that writes a file for each server into a specified directory, but if the commands do not run, you just have an empty file. What I am having issues with is updating these empty files with something like "Server Unreachable" so that I know there was a connection issue reaching the server and to not cause problems with the rest of the script.

Here is what I have so far:

#!/bin/bash

file="/home/user/tools/test-host"

now=$(date +"%F")
folder="./cnxhwinfo-$now/"
empty="$(find ./cnxhwinfo-$now/ -maxdepth 1 -type f -name '*' -size 0 -printf '%f%2d')"
command="echo \$(uptime | awk -F'( |,|:)+' '{d=h=m=0; if (\$7==\"min\") m=\$6; else {if (\$7~/^day/) {d=\$6;h=\$8;m=\$9} else {h=\$6;m=\$7}}} {print d+0,\"days\",h+0,\"hours\",m+0,\"minutes\"}'),  \$(hostname | awk '{print \$1}'), \$(sudo awk -F '=' 'FNR == 2 {print \$2}' /etc/connex-release/version.txt), \$(lscpu | awk -F: 'BEGIN{ORS=\", \";} NR==4 || NR==6 || NR==15 {print \$2}' | sed 's/ *//g') \$(free -k | awk  '/Mem:/{print \$2}'), \$(df -Ph | awk '/var_lib/||/root/ {print \$2,\",\"\$5,\",\"}')"

pssh -h $file -l user -t 10 -i -o /home/user/tools/cnxhwinfo-$now -x -tt $command
echo "Server Unreachable" | tee "./cnxhwinfo-$now/$empty"

ls ./cnxhwinfo-$now >> ./cnx-data-$now
cat ./cnxhwinfo-$now/* >> ./cnx-list-$now
paste -d, ./cnx-data-$now ./cnx-list-$now >>./cnx-data-"$(date +"%F").csv"

I was trying to use find to locate the empty files and write "Server" unavailable using tee with this:

echo "Server Unreachable" | tee "./cnxhwinfo-$now/$empty"

if the folder specified doesn't already exist i get this error:

tee: ./cnxhwinfo-2019-09-03/: Is a directory

And if it does exist (ie, i run the script again), it instead creates a file named after the IP addresses returned by the find command, like this:

192.168.1.2 192.168.1.3 192.168.1.4 1

I've also tried:

echo "Server Unreachable" | tee <(./cnxhwinfo-$now/$empty)

The find command outputs the IP addresses on a single line with a space in between each one, so I thought that would be fine for tee to use, but I feel like I am either running into syntax issues, or am going about this the wrong way. I have another version of this same script that uses regular ssh and works great, just much slower than using pssh.

0

1 Answer 1

1

empty should be an array, assuming none of the file names will contain any whitespace in their names.

readarray -t empty < <(find ...)

echo "Server unreachable" | (cd ./cnxhwinfo-$now/; tee "${empty[@]}" > /dev/null)

Otherwise, you are building a single file name by concatenating the empty file names.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I haven't used arrays before but this worked: readarray -t empty < <(find ./cnxhwinfo-$now/ -maxdepth 1 -type f -name '*' -size 0 -printf '%f\n') echo "Server unreachable" | (cd ./cnxhwinfo-$now/; tee "${empty[@]}" > /dev/null) I had to change my find command just a bit - \n instead of %2d!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.