Stopping the Ping process in bash script? - linux

I created a bash script to ping my local network to see which hosts is up and I have a problem in stopping the Ping process by using ctrl+C once it is started
the only way i found to suspend it but even the kill command doesn't work with the PID of the Ping
submask=100
for i in ${submask -le 110}
do
ping -n 2 192.168.1.$submask
((submask++))
done

Ctrl + C exit ping, but another ping starts. So you can use trap.
#!/bin/bash
exit_()
{
exit
}
submask=100
while [ $submask -le 110 ]
do
fping -c 2 192.168.77.$submask
((submask++))
trap exit_ int
done

I suggest you to limit the amount of packets sent with ping with the option -c.
I also corrected the bash syntax, guessing what you intend to do.
Finally, it is faster to run all the ping processes in parallel with the operand &.
Try:
for submask in ${100..110}
do
echo ping -c 1 192.168.1.$submask &
done

Related

Killing Socat and restarting it via a bash script

I need a bash script which kills als SOCAT-Proccesses and restarts them again. I managed it via crontab after a reboot, but this produces too much downtime to reboot again after there are too many SOCAT-Proccesses.
I used
#!/bin/sh
killall socat &
sleep 3s
socat UDP4-LISTEN:PORT,fork,su=nobody UDP6:[IPV6]:PORT & disown
socat TCP4-LISTEN:PORT2,fork,su=nobody TCP6:[IPV6]:PORT2 & disown
exit
Now I have the problem that the script
does not run completely in background
often stops after executing the first SOCAT-Command.
I also tried nohup, but it also does not run completely in background.
What exactly should instigate this script to run?
Do you want to run it periodically, manually or when socat fails?
We certainly should understand why socat is failing in the first place.
I think it would be a good idea to output some logging from your script and kill socat with SIGKILL (-9).
A starting point here:
$ cat /usr/local/bin/restart_ip_addr_fam_bridge.sh
#!/bin/sh
IPV6="::1"
PORT=10001
PORT2=10002
while [ true ]; do
killall -9 socat 2> /dev/null
socat -T3600 UDP4-LISTEN:$PORT,reuseaddr,fork,su=nobody UDP6:[$IPV6]:$PORT &
socat TCP4-LISTEN:$PORT2,reuseaddr,fork,su=nobody TCP6:[$IPV6]:$PORT2 &
# Wait for a request to run or you could wait for a fixed time here
while [ ! -f /tmp/req_restart_ip_addr_fam_bridge ]; do
sleep 3
done
rm -f /tmp/req_restart_ip_addr_fam_bridge
printf "%s: Restarting IP address family bridge\n" "$(date '+%D %T')" >> /tmp/restart_ip_addr_fam_bridge.log
chmod 666 /tmp/restart_ip_addr_fam_bridge.log
# Avoid busy wait
sleep 5
done
exit
You can run it at startup, for example, adding this line to /etc/rc.local:
/usr/local/bin/restart_ip_addr_fam_bridge.sh &
And request it to restart your socat bridges with:
$ touch /tmp/req_restart_ip_addr_fam_bridge
See the running log with:
$ cat /tmp/restart_ip_addr_fam_bridge.log
08/20/22 15:14:43: Restarting IP address family bridge
Test your socat bridges:
$ nc -6 -l ::1 10002 | $ nc -4 127.0.0.1 10002
Typed from IPv4 TCP client | Typed from IPv4 TCP client
Be careful restart_ip_addr_fam_bridge.sh here is running as root from rc.local script.
Probably this is not very desirable/safe depending on your application.
EDIT: Added timeout for socat UDP bridge as recommended by #dest-unreach.

Shutting Down Windows Network Using Bash

Here's my current code, which works but is slow
for i in {1..255..1}; do
for j in {1..255..1}; do
ip="10.8.$i.$j"
sudo net rpc shutdown -I $ip -U Username%Password -t 1 -f
echo $ip
done
done
I would like to be able to go through these IPs and attempt to shut them down. But if there is not a PC at that IP it has to wait for it to timeout before attempting the next one. So how can I find and shutdown all windows PCs on a network? (they all have the same credentials)
A trivial solution is to just run a pile of them in parallel:
for i in {1..255..1}; do
for j in {1..255..1}; do
ip="10.8.$i.$j"
sudo net rpc shutdown -I $ip -U Username%Password -t 1 -f &
echo $ip
done
wait
done
This runs 255 at a time and waits for them all to finish. Smarter and more flexible parallelization can be had through xargs, sem or parallel if Windows supports that.

Script to check connection every 5 minutes and write result to file (without ping) in LINUX

I need to check my connection to a spesific port every 5 minutes, currently i can't use ping command, so i need other alternative to do this.I want to execute this command in shell script
Can someone help me to show some example for this case?
port=80
ip=8.8.8.8
checkIntervalSecs=5
timeoutSecs=1
while true ; do
if $(nc -z -v -w$timeoutSecs $ip $port &>/dev/null); then
echo "Server is up!"
else
echo "Server is down!"
fi
sleep $checkIntervalSecs
done
This runs until you kill it. For an explanation of the nc command, it is basically taken from SO question #IporSircer suggested.

How to kill port forwarding once you have have finished using it

In order to sync my home and work file systems I need to go via an intermediary computer and use port forwarding. Let us call the home computer A, the intermediate one B and the work computer C. From the command line I do this
ssh -N -f -L 2025:C:22 me_B#B && unison foo ssh://me_C#localhost:2025/foo
I would like to put this one-liner into a bash script. How can I make it quit gracefully at the end and not leave any port forwarding still set up?
ssh -N -f -L 2025:C:22 me_B#B &
pid=$! # ssh PID
rc=$? # ssh return code
# set up to kill ssh when this script finishes
function finish {
kill $pid
}
trap finish EXIT
[ $rc -eq 0 ] && unison foo ssh://me_C#localhost:2025/foo

bash ping ip run command on reply [duplicate]

This question already has answers here:
Checking host availability by using ping in bash scripts
(11 answers)
Closed 5 years ago.
I want to check the ping replies on two IP addresses and if they are both up, then I execute a command.
For example:
ping 8.8.8.8 on response do
ping 8.8.4.4 on response
execute command
Is there a simple bash script to do this?
According to the manpage on ping:
If ping does not receive any reply packets at all it will exit with code 1. If a packet count and deadline are both specified, and fewer than count packets are received by the time the deadline has arrived, it will also exit with code 1. On other error it exits with code 2. Otherwise it exits with code 0. This makes it possible to use the exit code to see if a host is alive or not.
Thus you can rely on the exit code to determine whether to continue in your script.
ping 8.8.8.8 -c 1
if [ $? = 0 ]
then
echo ok
else
echo ng
fi
Try ping only 1 time with -c 1 option. Change to any number as you like.
$? is the exit code of the previous command. You can refer ping's exit code with it.
Modify the above code snippet to what you want.
Bash commands to return yes or no if a host is up.
Try hitting a site that doesn't exist:
eric#dev ~ $ ping -c 1 does_not_exist.com > /dev/null 2>&1; echo $?
2
Try hitting a site that does exist:
eric#dev /var/www/sandbox/eric $ ping -c 1 google.com > /dev/null 2>&1; echo $?
0
If it returns 0, then the host is evaluated to be responsive. If anything other than zero, then it was determined that the host is unreachable or down.

Resources