How to check ssh availability if check ssh OK then connect to server 1 and if ssh NOT OK then connect to server 2? - bash

I have 2 servers.
The first server is always active and the second server is always in the stanby state.
I'm trying to write a bash script to check the status of these two servers before doing the task.
If the first host dies (or the connect SSH to server 1 not OK) then it will connect SSH to the second host for execution to task.
Any idea on how to do it?

EDIT2: since OP mentioned that there is a file named IP.list from which server names should be read then following may help you here.
while read server
do
if [[ -n $(ssh user_name#"$server" "ls -ld /tmp") ]]
then
echo "$server is up."
echo "Exiting from script now.."
exit;
else
echo "$server is NOT up, look for another server."
fi
done < "IP.txt"
EDIT: Since OP is saying ping is not an option to check server's availability then using ssh only for servers. Considering that your source server(from where you are running the script) and target server(whose status you are checking are having passwordless connection between them so that you will NOT be prompted for any password)
if [[ -n $(ssh user_name#server_name "ls -ld /tmp") ]]
then
echo "Server 1 is up."
else
echo "Server is NOT up, look for another server."
fi
You could simple do ping to SERVER1 and see if that is happening successfully in case it not it means server is not up and you could move your things to another stand up server.
if [[ $(ping -c5 SERVER1 | grep -q "5 packets transmitted, 5 received") -eq 0 ]]
then
echo "Server1 is up."
else
echo "Server is NOT up, look for another server."
fi

Related

How to check the SSH login status of routers in bash script with password prompts

I am running a task where i need to check the SSH login status on 400 remote routers. I have made scripts using expect in bash which SSH the remote routers and run some commands over it. However, there are some routers that are not responding to SSH. I am using if statement to avoid those routers which are failing on SSH. Sample code to check the status on remote router works only if we have password less entry or the private key saved. Could you please help how can I check the SSH status on the remote routers?
If I get the password prompt while doing SSH to the router, I can say that the server is able to SSH the router. There is no need to supply password to it.
#!/bin/bash
ssh -q -o BatchMode=yes -o ConnectTimeout=7 username#IP exit
echo $?
if [ $? -ne 0 ]
then
# Do stuff here if example.com SSH is down
echo "Can not connect to the device"
fi
Well,
If you are using expect package then there is timeout option there as well.
Else your shell code above is correct way of doing it except just a few corrections:
#!/bin/bash
ssh -q -o BatchMode=yes -o ConnectTimeout=7 username#IP date
ret=$?
echo $ret
if [ $ret -ne 0 ]
then
# Do stuff here if example.com SSH is down
echo "Can not connect to the device"
fi
You can see, we are assigning $? immediately to a var. If you don't, then $? will contain the return value of echo $? command which will be always 0. Hence giving you true for all ssh.
Also I suggest to run some other command rather than exit with ssh.
Hope this helps
===Edited====>>>
well since you don't have passwordless ssh enabled. You can try to telnet to port 22, if 22 port is open then it will show connected and if its not open then it won't you can grep on it.
Here is the modified code: (Provided that ssh is running on 22 port otherwise change the port in code.)
#!/bin/bash
echo "" | telnet $IP 22 | grep "Connected"
ret=$?
echo $ret
if [ $ret -ne 0 ]
then
# Do stuff here if example.com SSH is down
echo "Can not connect to the device"
fi

script to check if I can access multiple servers

I made this script to check if I can connect to a list of many servers:
for SERVER in $(cat servers.txt); do
ssh root#$SERVER && echo OK $SERVER || echo ERR $SERVER
done
The problem is that if is the first time I’m connecting to the server, the server asks the classic question “The authenticity of host ‘x.x.x.x’ can't be established... bla bla bla” Then I have to respond yes or not and it losses the purpose of making it a script, is there any way to bypass that so I can add it to the script?
Also, There are some servers in which I don't have my keys in them but they have the option to enter a password. In that case it will wait until I try a password to continue with the script execution, so I was wondering if there is a way to improve this script so if the server asks for a password then set it to ERR $SERVER and continue with the script?
Thank you for your help.
Do you actually need to establish an SSH connection?
Or is simply opening a socket connection to the host on the SSH port enough for you to determine that the server is online?
servers.txt
hostA.example.com
hostB.example.com
hostC.example.com
port-probe.sh
#!/bin/bash
PORT=22
TIMEOUT=3
for SERVER in $(cat servers.txt); do
# Open a socket and send a char
echo "-" | nc -w $TIMEOUT $SERVER $PORT &> /dev/null
# Check exit code of NC
if [ $? -eq 0 ]; then
echo "$SERVER is Available."
else
echo "$SERVER is Unavailable."
fi
done
You can use the -o flag to set options in SSH:
for SERVER in $(cat servers.txt); do
ssh -o StrictHostKeyChecking=no -o BatchMode=yes root#$SERVER exit && echo OK $SERVER || echo ERR $SERVER
done
Check out the manpage for ssh_config: man ssh_config for all of the options available with the -o flag.
If you have quite a few servers what you may want to do is connect to all of them simultaneously. This way it only waits maximum 2 minutes (default TCP connect timeout) if any of the servers is unresponsive.
Connect to each server without allocating a terminal and execute echo . command, redirect the output into a named file. Issue these commands in a loop asynchronously. Then wait till all commands complete, iterate over the log files and check which ones have dots in it. Then report the servers whose log files do not have a dot in it.
E.g.:
#/bin/bash
servers="$#"
for server in $servers; do
ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no -Tn $server echo . >$server.log 2>$server.error.log &
done
wait # After 2 minutes all connection attempts timeout.
for server in $servers; do
[[ -s $server.log ]] || echo "Failed to connect to $server" >2
done

Bash script that accepts TCP connection, when a client connection occurs, send the time of day as a response to the client

I'm not going to lie this is an homework assignment, but I've been googling constantly to try to get some idea on how the heck to approach this particular question.
"Create a script named lab6s6 that accepts TCP connections. When a client connection
occurs, send the time of day as a response to client. You may choose any port number as
the listening port, and don’t forget to close your connections."
I'm running the latest fedora OS on my virtualbox
So far after doing some research I've come across this particular piece of code
$ exec {file-descriptor}<>/dev/{protocol}/{host}/{port}
What i've come up with after doing some research would be
exec 3<>/dev/TCP/127.0.0.1/8000
So from my general understanding the file descriptor tends to always be set to 3 (is this because of the stdin, stdout, stderr, what is the purpose of this?) also the "<>" which represents reading and writing, and the directory is a way to actually use those protocols. and lastly, for my ip I read somewhere that I shouldnt be using the loopback that this wouldn't work but I'll be honest I was a bit clueless while reading the article, and for the port I never really understood that, is it like the higher the number the more available your signal is?
and another side question, do I need to install any other type of software to even accomplish something like this? If anyone could clarify if I'm basically opening up like a phone line on my computer to be able to talk to other computers at are on my LAN, is that even possible?
I'm not asking for direct answers, but if someone could nudge me in the right direction I would appreciate it greatly!
Thanks again!
I have prepared for you two scripts: client and server
after giving them the execution right: chmod u+x script_name you can run them in any order (client -> server or server -> client)
bash_server.sh
#!/usr/bin/env bash
#define port on which the server will listen
#and the output file that will be used to store the client port to send an answer
readonly PORT_LISTEN=22222;
readonly SERVER_FILE=server_file_tmp.out;
echo "Removing the server temporary file: ${SERVER_FILE}";
rm -f "${SERVER_FILE}";
#will open/bind/listen on PORT_LISTEN and whenever some information is received
#it will write it in the SERVER FILE
echo "Starting the server on port: ${PORT_LISTEN} with configuration file: ${SERVER_FILE}";
nc -k -l "${PORT_LISTEN}" | tee "${SERVER_FILE}" &
echo "Waiting for connection..."
#active listening to entry connection
while true;
do
#get always information about the external connection trying to connect to our open port
tmpNetworkString=$(lsof -i:"${PORT_LISTEN}" | grep "localhost:${PORT_LISTEN} (ESTABLISHED)" | awk '{print $9}');
echo -n "${tmpNetworkString}";
if [ -s "${SERVER_FILE}" ] && [ ! -z "${tmpNetworkString}" ]; then
answerPORT=$(cat "${SERVER_FILE}");
echo "Connection received on port ${PORT_LISTEN}...";
incomingIP=$(echo $tmpNetworkString | cut -d':' -f1);
incomingPort=$(echo $tmpNetworkString | cut -d'-' -f1 | cut -d':' -f2);
echo ">>Incoming traffic IP: ${incomingIP}";
echo ">>Incoming traffic Port: ${incomingPort}";
echo "Answering on IP: ${incomingIP}, port: ${answerPORT}...";
#wait client port to be ready
nc -z "${incomingIP}" "${answerPORT}";
isOpen=$?;
while [ ! "${isOpen}" -eq 0 ];
do
nc -z "${incomingIP}" "${answerPORT}";
isOpen=$?;
done
echo $(date) | nc -q 2 "${incomingIP}" "${answerPORT}";
echo "Closing the server, port: ${PORT_LISTEN}";
fuser -k -n tcp "${PORT_LISTEN}";
echo "Removing the server temporary file: ${SERVER_FILE}";
rm -f "${SERVER_FILE}";
exit 0;
fi
done
bash_client.sh
#!/usr/bin/env bash
#define port on which the client will listen
#and the output file that will be used to store the answer from the server
readonly PORT_LISTEN=33333;
readonly CLIENT_FILE=client_file_tmp.out;
readonly SERVER_PORT=22222;
readonly SERVER_IP=localhost
echo "Removing the client temporary file: ${CLIENT_FILE}";
rm -f "${CLIENT_FILE}";
#will open/bind/listen on PORT_LISTEN and whenever some information is received
#it will write it in the CLIENT FILE
echo "Starting the server on port: ${PORT_LISTEN} with configuration file: ${CLIENT_FILE}";
nc -k -l "${PORT_LISTEN}" > "${CLIENT_FILE}" &
echo "Connecting to the server: ${SERVER_IP}, on port: ${SERVER_PORT} and waiting for answer";
#sending port information for answer:
#wait client port to be ready
nc -z "${SERVER_IP}" "${SERVER_PORT}";
isOpen=$?;
while [ ! "${isOpen}" -eq 0 ];
do
nc -z "${SERVER_IP}" "${SERVER_PORT}";
isOpen=$?;
done
echo "${PORT_LISTEN}" | nc -q 2 "${SERVER_IP}" "${SERVER_PORT}";
while true;
do
if [ -s "${CLIENT_FILE}" ]; then
echo "Answer received from server...";
echo "##############################";
echo "##############################";
cat "${CLIENT_FILE}";
echo "##############################";
echo "##############################";
#sleep 10;
echo "Closing the open port of the client, port: ${PORT_LISTEN}";
fuser -k -n tcp "${PORT_LISTEN}";
echo "Removing the answer file: ${CLIENT_FILE}";
rm -f "${CLIENT_FILE}";
exit 0;
fi
done

Checking SSH failure in a script

Hi what is the best way to check to see if SSH fails for whatever reason?
Can I use a IF statement ( if it fails then do something)
I'm using the ssh command in a loop and passing my hosts names form a flat file.
so I do something like:
for i in `cat /tmp/hosts` ; do ssh $i 'hostname;sudo ethtool eth1'; done
I get sometime this error or I just cannot connect
ssh: host1 Temporary failure in name resolution
I want to skip the hosts that I cannot connect to is SSH fails. What is the best way to do this? Is there a runtime error I can trap to bypass the hosts that I cannot ssh into for whatever reason, perhaps ssh is not allowed or I do not have the right password ?
Thanking you in advance
Cheers
To check if there was a problem connecting and/or running the remote command:
if ! ssh host command
then
echo "SSH connection or remote command failed"
fi
To check if there was a problem connecting, regardless of success of the remote command (unless it happens to return status 255, which is rare):
if ssh host command; [ $? -eq 255 ]
then
echo "SSH connection failed"
fi
Applied to your example, this would be:
for i in `cat /tmp/hosts` ;
do
if ! ssh $i 'hostname;sudo ethtool eth1';
then
echo "Connection or remote command on $i failed";
fi
done
You can check the return value that ssh gives you as originally shown here:
How to create a bash script to check the SSH connection?
$ ssh -q user#downhost exit
$ echo $?
255
$ ssh -q user#uphost exit
$ echo $?
0
EDIT - I cheated and used nc
Something like this:
#!/bin/bash
ssh_port_is_open() { nc -z ${1:?hostname} 22 > /dev/null; }
for host in `cat /tmp/hosts` ; do
if ssh_port_is_open $host; then
ssh -o "BatchMode=yes" $i 'hostname; sudo ethtool eth1';
else
echo " $i Down"
fi
done

Ping Tool to check if server is online

Is this tool that I created from various SOF threads valid? Will it work? I want to have a ping test done to a server every minute. If it fails 5 times in a row it sends an email out. It then flushes and resets the script pretty much to check again.
#!/bin/bash
# ping checker tool
numOfFails=0
incrememnt=1
EMAILMESSAGE="/tmp/emailmessage.txt"
while true; do
if ! ping -c 1 google.com ; then #if ping exits nonzero...
numOfFails=$(($num + $increment))
else
numOfFails=0
fi
if ((numOfFails > 4)); then
numOfFails=0
echo "SAN is offline!" > $EMAILMESSAGE
mail -s "SAN offline" "test#test.com" < $EMAILMESSAGE
fi
sleep 60 #check again in one minute
done
Your code won't work at all, this is a revised version:
#!/bin/bash
# ping checker tool
FAILS=0
EMAIL_ADDRESS="example#example.com"
SERVER="192.168.1.1"
SLEEP=60
while true; do
ping -c 1 $SERVER >/dev/null 2>&1
if [ $? -ne 0 ] ; then #if ping exits nonzero...
FAILS=$[FAILS + 1]
else
FAILS=0
fi
if [ $FAILS -gt 4 ]; then
FAILS=0
echo "Server $SERVER is offline!" \
| mail -s "Server offline" "$EMAIL_ADDRESS"
fi
sleep $SLEEP #check again in SLEEP seconds
done
Change example#example.com and 192.168.1.1 for your email address and the IP address of the server you are testing. I recommend using and IP address instead of a hostname to prevent mixing name resolution errors with connection errors.
Please be advised that although this will work I would recommend running a slightly different script from cron instead of having it running continuously like you seem to want, when running from cron you would not need to monitor that the script is running since if it stops for some reason the monitoring of the server stops as well.
Something like this run from crontab every minute.
#!/bin/bash
# ping checker tool
TMP_FILE="/tmp/ping_checker_tool.tmp"
if [ -r $TMP_FILE ]; then
FAILS=`cat $TMP_FILE`
else
FAILS=0
fi
EMAIL_ADDRESS="example#example.com"
SERVER="192.168.1.1"
ping -c 1 $SERVER >/dev/null 2>&1
if [ $? -ne 0 ] ; then #if ping exits nonzero...
FAILS=$[FAILS + 1]
else
FAILS=0
fi
if [ $FAILS -gt 4 ]; then
FAILS=0
echo "Server $SERVER is offline!" \
| mail -s "Server offline" "$EMAIL_ADDRESS"
fi
echo $FAILS > $TMP_FILE
Consider using Pingdom. It provides this service for you.
One thing you have not considered is once your site goes down, you will continue to get email messages every minute, until the site is up again, or until you stop this script.
A good approach is to switch states from reporting when the site is down to reporting when the site is up, once you have detected that it is down. And then back again, once it is back up.
Essentially you one receive an email reporting 'site down', then another later on, hopefully, reporting 'site is up'.
Pingdom does this for you, very nicely.
I have been investigating how to do this so that I can activate/deactivate services depending on whether my phone is at home. I have come up with the following:
#!/bin/bash
HOST_TO_CHECK=<hostname>
if ping -qc 20 $HOST_TO_CHECK >/dev/null; then
echo "Host $HOST_TO_CHECK is up"
else
echo "Host $HOST_TO_CHECK is down"
fi
Replace <hostname> with the host you wish to check.
The script will ping the host 20 times.
The reason it does this is that my mobile doesn't always respond to pings immediately.
Obviously you can replace the echo commands with something to actually do something useful :-)
You can then schedule the script to check every 5 minutes by adding it to your crontab:
*/5 * * * * /opt/pingcheck.sh

Resources