Pinging server in Bash script, - bash

can anyone help me figure out whats wrong in this script ?
im a begineer
echo "whats the server address ?"
read server
ping -c 4 $server > /dev/null
result=$?
if [ $result = 0 ]
echo " ping succ"
else echo "ping unsuccessful" fi

Your if syntax is wrong. It should be as follows. Notice the then and the closing fi
if ...; then
#code
else
#code
fi
You can shorten your code by doing the pinging inside if
if ping -c 4 $server > /dev/null; then
echo "ping succ"
else
echo "ping unsuccessful"
fi

You just missed to close the if condition
echo "whats the server address ?"
read server
ping -c 4 $server > /dev/null
result=$?
if [ $result = 0 ]
echo "ping successful"
else
echo "ping unsuccessful"
fi

Related

How to check internet connectivity using pinging multiple IP's

I need a bash script for checking internet connectivity.
I used the below one first.
#!/bin/bash
#
while true; do
if ping -c 1 1.1.1.1 &> /dev/null
then
echo "internet working"
else
echo "no internet"
fi
sleep 5
done
It works fine but fails sometimes. So I was looking for something that would do ping test on multiple IP's, this way only one of the IP has to ping successfully to assume connectivity.
I am extremely new to bash, so apologies for any mistakes.
How do I correct the below script so it works as intended?
#!/bin/bash
#
while true; do
count= '0'
if ping -c 1 1.1.1.1 &> /dev/null
then
count= '1'
fi
if ping -c 1 8.8.8.8 &> /dev/null
then
count= count + '1'
fi
if ping -c 1 www.google.com &> /dev/null
then
count= count + '1'
fi
if [ count -lt 1 ]
then
echo "no internet"
else
echo "internet working"
fi
sleep 5
done
Chain the commands in the if statement:
if ping -c 1 1.1.1.1 || ping -c 1 8.8.8.8 || ping -c 1 www.google.com
then
echo "One of the above worked"
else
echo "None of the above worked" >&2
fi
And if needed then you can ofcause still redirect the output of the ping command.
if ping ... > /dev/null # redirect stdout
if ping ... 2> /dev/null # redirect stderr
if ping ... &> /dev/null # redirect both (but not POSIX) `>/dev/null 2>&1` is though.
I'd use a function for this task, so that I can pass the hosts to be pinged as arguments:
#!/bin/bash
# Check internet connectivity
# Returns true if ping succeeds for any argument. Returns false otherwise
ckintconn () {
for host
do
ping -c1 "$host" && return
done
return 1
} &>/dev/null
if ckintconn 1.1.1.1 8.8.8.8 www.google.com
then
echo "internet working"
else
echo "no internet"
fi

Linux Command Results in a bash Variable

I am trying to write a Ping result bash code.
this way it work, but i'm not a fan of using the last ouput function:
#!/bin/bash
ping -q -c2 10.10.50.120 > /dev/null
resp=$?
echo "$resp"
if [ "$resp" == 0 ]
then
echo "ok"
else
echo "not ok"
fi
Ouput:
0
ok
but this way it doesn't work:
#!/bin/bash
resp=$(ping -q -c2 10.10.50.120 > /dev/null)
echo "$resp"
if [ "$resp" == 0 ]
then
echo "ok"
else
echo "not ok"
fi
Ouput:
not ok
Can anyone help me to find out how to write it correctly?
I wanted to avoid the "$?" function.
That's great. So just use if.
if output=$(ping -q -c2 10.10.50.120); then
# or like: if ping -q -c2 10.10.50.120 >/dev/null; then
echo "ok"
else
echo "not ok"
fi
echo "Anyway, ping command ouptutted: $output"
It is possible to save the exit status to a variable without the "$?" function?
No.

Bash | Host-check

I have a little problem with my bash script
i got a school project where i have to make a bash script to check if the host is up every 5 minuttes and if fails send email
i had problems with the "fi" statement but fixed the error.
now when i run the script i get an error: line 17 to many arguments"
it initiate the ping command (my Anti virus is blocking the ICMP, so i know the ping lines work)
#!/bin/bash
#Server-status script
FAILS=0
EMAIL_ADDRESS="Critical-error#gruppe4.net" ##Email capabilities
SERVER="192.168.1.1" ###Host to check
SLEEP=300 ###Defining Sleep
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
use declare -i to use FAILS as integer and initialize to 0
declare -i FAILS=0
then sum 1
FAILS=$FAILS+1
here is my code(I've commented the mail commmand):
#!/bin/bash
#Server-status script
declare -i FAILS=0
EMAIL_ADDRESS="Critical-error#gruppe4.net" ##Email capabilities
SERVER="192.168.1.1" ###Host to check
SLEEP=1 ###Defining Sleep
echo "1-FAILS[$FAILS]"
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
echo "2-FAILS[$FAILS]"
if [ $FAILS -gt 1 ]; then
FAILS=0
echo "Server $SERVER is offline!" # \ | mail -s "Server offline" "$EMAIL_ADDRESS"
fi
sleep $SLEEP #check again in SLEEP seconds
done
output:
sh-4.3$ bash -f main.sh
1-FAILS[0]
2-FAILS[1]
2-FAILS[2]
Server 192.168.1.1 is offline!
2-FAILS[1]
2-FAILS[2]
Server 192.168.1.1 is offline!
2-FAILS[1]
2-FAILS[2]
Server 192.168.1.1 is offline!
2-FAILS[1]
I hope this can help
Regards
Claudio
You are expanding the $FAILS variable with content [FAILS + 1], getting an invalid [] syntax.
Change FAILS=$"[FAILS + 1]" to:
FAILS=$((FAILS+1))

Bash Exit Status codes for Ping

I am working on a small script that checks if a host is up or down.
until [ "$STATUS" -eq "0" ]
do
ping -c 1 192.168.0.3
echo The host is down
STATUS=`echo $?`
done
It is supposed to change the status to 0 if it pings a host that is up and exit the until loop. But it doesnt. Even if I echo out the value of $? the value is always zero.
Can anyone help me figure this out please? :)
Thanks in advance
You have echo The host is down after ping command. So $? takes the exit status of the echo command not the ping command.
ping -c 1 192.168.0.3
STATUS=$?
if [ $STATUS -ne 0 ]; then
echo "The host is down"
fi
You placed echo after saving the status that's why you always get 0:
ping -c 1 192.168.0.3
echo The host is down ## Always changes $? to 0
STATUS=`echo $?`
One better way to do it could be:
until ping -c 1 192.168.0.3; do
echo "The host is down"
done
Longer version:
until ping -c 1 192.168.0.3; STATUS=$?; [ "$STATUS" -eq "0" ]; do
echo "The host is down"
done

Changing SSH port with a bash script on CentOS

SO I wrote a script that can change the SSH port on CentOS but for some reason I'm encountering this error:
sshchangecOS6.sh: line 36: syntax error: unexpected end of file
This is the script:
#! /bin/bash
# This script changes the ssh port for logins on CentOS 5 and 6
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 2
read -r -p "Would you like to change the ssh port? [Y/N] " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
then
read -p "What would you like to change the port to? (Chose between 1024-65535) " sshportconfig
if (( ("$sshportconfig" > 1024) && ("$sshportconfig" < 65535) )); then
echo "Port $sshportconfig" >> /etc/ssh/sshd_config
echo "--------------------------------------------------------------------"
echo ""
echo ""
echo "SSH port has been changed to: $sshportconfig. Written by Sincere the Minotaur."
echo ""
echo ""
echo "--------------------------------------------------------------------"
else
echo "Port chosen is incorrect."
exit 1
fi
else
sshPort=$(grep "Port" /etc/ssh/sshd_config) | head -n 1
echo "--------------------------------------------------------------------"
echo ""
echo ""
echo "SSH is still: $sshPort"
echo "Written by Sincere the Minotaur."
echo ""
echo "---------------------------------------------------------------------"
exit 1
fi
exit 0
Could someone explain where the errors are?
There is no fi for the first if if [[ $EUID -ne 0 ]]; then. You need to close every if with a fi.

Resources