set a timeout and error message on my script - linux

I have a script that im trying to create an error message and timeout for if the openssl takes to long. Here is the script. Can anyone help me? I'm a little lost.
FILENAME=$1
while read -r ip; do
echo "${ip}"
echo -n | openssl s_client -connect "${ip}:443" -showcerts 2>/dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' | openssl x509 -noout -dates
done < <(cut -d "," -f2 $FILENAME | tail -n +2)

You could use timeout from gnu core utils package(manual)
Maybe do something like:
while read -r ip; do
timeout [timeout duration] [your ssl command]
if [ $? -eq 124 ]; then
echo FAIL
else
echo OK
fi
done

Related

Using ssh inside a script to run another script that itself calls ssh

I'm trying to write a script that builds a list of nodes then ssh into the first node of that list
and runs a checknodes.sh script which it's self is just a for i loop that calls checknode.sh
The first 2 lines seems to work ok, the list builds successfully, but then I get either get just the echo line of checknodes.sh to print out or an error saying cat: gpcnodes.txt: No such file or directory
MYSCRIPT.sh:
#gets the master node for the job
MASTERNODE=`qstat -t -u \* | grep $1 | awk '{print$8}' | cut -d'#' -f 2 | cut -d'.' -f 1 | sed -e 's/$/.com/' | head -n 1`
#builds list of nodes in job
ssh -qt $MASTERNODE "qstat -t -u \* | grep $1 | awk '{print$8}' | cut -d'#' -f 2 | cut -d'.' -f 1 | sed -e 's/$/.com/' > /users/issues/slow_job_starts/gpcnodes.txt"
ssh -qt $MASTERNODE cd /users/issues/slow_job_starts/
ssh -qt $MASTERNODE /users/issues/slow_job_starts/checknodes.sh
checknodes.sh
for i in `cat gpcnodes.txt `
do
echo "### $i ###"
ssh -qt $i /users/issues/slow_job_starts/checknode.sh
done
checknode.sh
str=`hostname`
cd /tmp
time perf record qhost >/dev/null 2>&1 | sed -e 's/^/${str}/'
perf report --pretty=raw | grep % | head -20 | grep -c kernel.kallsyms | sed -e "s/^/`hostname`:/"
When ssh -qt $MASTERNODE cd /users/issues/slow_job_starts/ is finished, the changed directory is lost.
With the backquotes replaced by $(..) (not an error here, but get used to it), the script would be something like
for i in $(cat /users/issues/slow_job_starts/gpcnodes.txt)
do
echo "### $i ###"
ssh -nqt $i /users/issues/slow_job_starts/checknode.sh
done
or better
while read -r i; do
echo "### $i ###"
ssh -nqt $i /users/issues/slow_job_starts/checknode.sh
done < /users/issues/slow_job_starts/gpcnodes.txt
Perhaps you would also like to change your last script (start with cd /users/issues/slow_job_starts)
You will find more problems, like sed -e 's/^/${str}/' (the ${str} inside single quotes won't be replaced by a host), but this should get you started.
EDIT:
I added option -n to the ssh call.
Redirects stdin from /dev/null (actually, prevents reading from stdin).
Without this option only one node is checked.

How to use mailx command along with ssh in linux

I am trying to run mailx after remoting into another linux box, but for some reason the command doesn't work. I also want to add the content of a csv file in the mail I am trying to send, but it is not working.
SERVER=host1
rm conncheck.csv
`netstat -na 63.111.184.111 442|grep "ESTABLISHED"|grep "63.111.184.111:442" >> conncheck.csv`
`netstat -na 63.111.184.201 572|grep "ESTABLISHED"|grep "63.111.184.201:572" >> conncheck.csv`
wordcount=`grep "ESTABLISHED" conncheck.csv|wc -l`
if [ $wordcount == 2 ]; then
`scp conncheck.csv $SERVER:/tmp/`
ssh -o "StrictHostKeyChecking=no" $SERVER `cat /tmp/conncheck.csv | mailx -s "LiquidityFIX connection is up" recepient#email.com < /dev/null'
else
ssh -o "StrictHostKeyChecking=no" $SERVER mailx -s 'LiquidityFIX connection is down <eom>' recepient#email.com < /dev/null'
fi
~
I think I am getting the command incorrect. Kindly advise how to get this working
in this line:
ssh -o "StrictHostKeyChecking=no" $SERVER `cat /tmp/conncheck.csv | mailx -s "LiquidityFIX connection is up" recepient#email.com < /dev/null'
you need to change the ` to ' right before the cat

Script output in terminal differs from MOTD output

I wrote a small script monitor my TLS certificates expiration.
The following is the output when I run /etc/update-motd.d/05-ssl in the terminal. The permissions on the script is 633 root
TLS certs Valid until
● facebook.com Thu Jun 06 2019
● google.com Tue Jun 18 2019
However when I log in via ssh my MOTD only shows
TLS certs Valid until
I suspect this is related to the piping I am doing in the last line when I print the output.
#!/bin/bash
ssl_domains="facebook.com google.com"
currentTime=$(date +%s)
output="TLS certs| Valid until"
for domain in $ssl_domains; do
certTime=$(openssl s_client -servername ${domain} -connect ${domain}:443 < /dev/null 2>/dev/null | openssl x509 -noout -enddate 2>/dev/null | cut -d= -f2)
certLineTime=$(date -d "${certTime}" +"%a %b %d %Y")
certTimestamp=$(date -d "${certTime}" +%s)
if [ "${certTimestamp}" -ge "${currentTime}" ]; then
sign="\e[36m●\e[0m"
else
sign="\e[1;33m▲\e[0m"
fi
output+="\n$sign $domain| $certLineTime"
done
echo -e "$output" | column -t -s '|'
Try adding
export LANG='en_US.UTF-8'
at the top of your script.

One ping two results

I'm sure there is an easy answer to this but not being too fluent in BASH I'm just going round in circles.
I want to do this
ping -c1 8.8.8.8 1>/dev/null 2>/dev/null
SUCCESS=$?
if [ $SUCCESS -eq 0 ] etc....
But then also assign that ping result to a variable so that I can get the time delay
ping -c1 8.8.8.8 1>/dev/null 2>/dev/null | DELAY=awk '{print $11}'
SUCCESS=$?
if [ $SUCCESS -eq 0 ] etc....
echo $DELAY
Thanks
There are multple issues with your code. One is already addressed by tink. If you do
ping -c1 8.8.8.8 1>/dev/null 2>/dev/null | cat
you first put all the stdout of the ping in /dev/null and then hope t get some stdout in your pipe. That won't work.
Second, the syntax
ping -c1 8.8.8.8 1>/dev/null 2>/dev/null | DELAY=awk '{print $11}'
is not correct. You will probably get a message
-bash: {print $11}: command not found
That is: the DELAY=awk syntax is used to set the variable DELAY to 'awk' for the execution the '{print $1}' command. That is not what you want to do.
Note also, that $? is the exit-code of the last command. So, if the previous line would have worked, $? would be the result of the awk and no longer of the ping.
Finally, when I do a ping, awk '{print $11}' just gives me a bunch of empty lines.
So, what to do? Fro example:
tempfile=/tmp/tempfile.$$
host=8.8.8.8
ping -c1 $host > $tempfile
success=$?
if [ $success -eq 0 ] ; then
sed -n 's/.*time=//p' $tempfile
else
echo "AAaarrghh... My Ping FAILED"
fi
rm $tempfile
or something like that
Thanks everyone and yes I knew my code had errors but it was about trying to describe the problem. I ended up going with this:-
PING=$(ping -c1 8.8.8.8 2>&1);
SUCCESS=$?
DELAY=$(echo $PING | awk '{print $14}' | awk -F= '{print $2}')
if [ $SUCCESS -eq 0 ]
then
echo $DELAY
else
echo 0
fi

Checking if String is in a command answer

I'm struggling with a problem in linux bash.
I want a script to execute a command
curl -s --head http://myurl/ | head -n 1
and if the result of the command contains 200 it executes another command.
Else it is echoing something.
What i have now:
CURLCHECK=curl -s --head http://myurl | head -n 1
if [[ $($CURLCHECK) =~ "200" ]]
then
echo "good"
else
echo "bad"
fi
The script prints:
HTTP/1.1 200 OK
bad
I tried many ways but none of them seems to work.
Can someone help me?
I would do something like this:
if curl -s --head http://myurl | head -n 1 | grep "200" >/dev/null 2>&1; then
echo good
else
echo bad
fi
You need to actually capture the output from the curl command:
CURLCHECK=$(curl -s --head http://myurl | head -n 1)
I'm surprised you're not getting a "-s: command not found" error
You can use this curl command with -w "%{http_code}" to just get http status code:
[[ $(curl -s -w "%{http_code}" -A "Chrome" -L "http://myurl/" -o /dev/null) == 200 ]] &&
echo "good" || echo "bad"
using wget
if wget -O /dev/null your_url 2>&1 | grep -F HTTP >/dev/null 2>&1 ;then echo good;else echo bad; fi

Resources