Convert bash script to Windows - windows

I wrote the following script for Linux in order to detect drops in my network connection:
#!/bin/bash
echo "### RUNNING ###"
echo "### $(date) ###"
while true;do
now=$(date +"%T")
if [[ "$(ping -c 1 8.8.8.8 | grep '100.0% packet loss' )" != "" ]]; then
echo "!!! KO ($now)" >> "log_connectivity_$(date +"%F")"
else
echo "OK ($now)" >> "log_connectivity_$(date +"%F")"
fi
sleep 5s
done
What it does is, within a loop, to ping 8.8.8.8 once and, if packet is lost it prints KO and the time and, otherwise, it prints OK and the time.
I would like to translate this bash script into a Windows script, but I have no idea. I would be very grateful if you could help me with this.
Thanks in advance ;)

Related

Ifplugd: Error "Incorrect action argument"

I have an error in the ifplugd.
Whenever a new ethernet cable is plugged into my appliance, then ifplugd should ping the Gateway.
This is what the script looks like:
ping.sh
#!/bin/bash
echo "in ifplugd" >> /tmp/ifplugd.txt
if [ "$2" == "up" ]; then
logger IfPlugD executes script
echo "Pinging 10.10.10.1 5 times"
echo "Executing State $2 ifdown ifup on :: $1 :: $(date)" >> /var/ifplugdlog.txt
ping 10.10.10.1 -c 5;
fi
Sadly, it $2 variable returns nothing.
I tried to check, why it returns nothing, so I ran the ifplugd.action script, which is runs automatically everytime a state changes. It looks like the following:
ifplugd.action
#!/bin/sh
set -e
case "$2" in
up)
run-parts --arg="$1" --arg="$2" /etc/ifplugd/action.d/
;;
down)
run-parts --reverse --arg="$1" --arg="$2" /etc/ifplugd/action.d/
*)
echo "ifplugd.action: Incorrect action argument" >&2
exit 1
;;
esac
It runs with the following arguments in the /etc/default/ifplugd-file
ifplugd
INTERFACES="enp2s0 enp3s0 enp6s0"
HOTPLUG_INTERFACES="enp2s0 enp3s0 enp6s0"
ARGS="-q -f -d10 -w -I"
SUSPEND_ACTION="stop"
And when I remove the network cable and attach it again, it still gives me no response, since my the if clause is getting ignored in my ping.sh, because the $2 variable is null.
Can someone help me, why I get the error "ifplugd: Incorrect action arguments" and how to fix it?
Thanks a lot.
Package seems to be out of date - used another package and made a cron

ping script - host down vs host not on network

I have the following script where I am trying to differentiate between a server that is down and a server that is no longer on the network.
If I use the ping command on the command line on a server that is just down and echo the $? I get a 1 as expected.
If I use the ping command on the command line on a server that is no longer on the network and echo the $? I get a 2 as expected. I can't seem to capture this behavior in my script. On the script below, the server that is no longer on the network does not appear at all in the badhosts output file. I am using the dev null on the ping line as I don't want to get the host unknown lines on the output which will skew the results.
Thanks in advance for any assistance.
#!/bin/ksh
# Take a list of hostnames and ping them; write any failures
#set -x
for x in `cat hosts`
do
ping -q -c 1 $x > /dev/null 2> /dev/null
if [ "$?" -eq 1 ];then
echo $x is on network but down >> badhosts
elif [ "$?" -eq 2 ];then
echo $x is not on the network >> badhosts
fi
done
I modified my script at the suggestion of Raman as follows and this works.
Thanks Raman !!
#!/bin/ksh
# Take a list of hostnames and ping them; write any failures
set -x
for x in `cat hosts`
do
ping -c 1 $x > /dev/null 2> /dev/null
pingerr=$?
if [ $pingerr -eq 1 ]; then
echo $x is on network but down >> badhosts
fi
if [ $pingerr -eq 2 ]; then
echo $x is not on the network >> badhosts
fi
done

Need to print current CPU usage and Memory usage in file continuously

I have prepared the below script, but it's not adding any data to the output file.
My intention is to get the current CPU usage and Memory usage and print them on log file.
What is wrong with my below script? I will run this script file in CentOS machine.
#!/usr/bin/bash
HOSTNAME=$(hostname)
mkdir -p /root/scripts
LOGFILE=/root/scripts/xcpuusagehistory.log
touch $LOGFILE
a=0;
b=1;
while [ "$a" -lt "$b" ]
do
CPULOAD=`top -d10 | grep "Cpu(s)"`
echo "$CPULOAD on Host $HOSTNAME" >> $LOGFILE
done
while true
do
cpu_load="$(top -b -n1 -d10 | grep "Cpu(s)")"
echo "$cpu_load on Host $HOSTNAME" >> "$log_file"
sleep 1
done
See top batch mode (-b) in the man page.

SCP loop stops executing after some time

So I have these two versions of the same script. Both are attempting to copy my profile to all the servers on my infra ( about 5k ). The problem I am having is that no matter which version I use, I always get the process stuck somewhere around 300 servers. It does not matter if I do it sequentially or in parallel, both version fail and both at a random server. I dont get any error message (Yes I know Im redirecting error messages to null now), it simply stops executing after reaching a random point close to 300 servers and it just lingers there doing nothing.
The best run I could get did it for about 357 servers.
Probably there is some detail I unknow that is causing this. Could someone advise?
Sequential
#!/bin/bash
clear
echo "$(date) - Process started"
all_count="$( cat all_servers.txt | wc -l )"
while read server
do
scp -B -o "StrictHostKeyChecking no" ./.bash_profile rouser#${server}:/home/rosuer/ && echo "$server - Done!" >> ./log.log || echo "$server - Failed!" >> ./log.log
done <<< "$( cat all_servers.txt )"
echo "$(date) - Process completed!!"
Parallel
#!/bin/bash
clear
echo "$(date) - Process started"
all_count="$( cat all_servers.txt | wc -l )"
while read server
do
scp -B -o "StrictHostKeyChecking no" ./.bash_profile rouser#${server}:/home/rosuer/ && echo "$server - Done!" >> ./log.log || echo "$server - Failed!" >> ./log.log &
done <<< "$( cat all_servers.txt )"
wait
echo "$(date) - Process completed!!"
Let's start with better input parsing. Instead of parsing a bash herestring from a posix command substitution via a while read loop, I've got the while read loop running through your server list directly via pipeline (this assumes one server per line in that file. I can fix this if that's not the case). If the contents of all_servers.txt was too long for a command line, you'd experience an error and/or premature termination.
I've also removed extraneous ./ items and I assume that rouser's home directory on each server is in fact /home/rouser (scp defaults to the home directory if given a relative path or no path at all).
Sequential
#!/bin/bash
clear
echo "$(date) - Process started"
all_count="$( cat all_servers.txt | wc -l )"
while read server
do
scp -B -o "StrictHostKeyChecking no" .bash_profile rouser#${server}: \
&& echo "$server - Done!" >> log.log \
|| echo "$server - Failed!" >> log.log
done < all_servers.txt
echo "$(date) - Process completed!!"
Parallel
For the Parallel solution, I've enclosed your conditional in parentheses just in case the pipeline was backgrounding the wrong process.
#!/bin/bash
clear
echo "$(date) - Process started"
all_count="$( cat all_servers.txt | wc -l )"
while read server
do
(
scp -B -o "StrictHostKeyChecking no" .bash_profile rouser#${server}: \
&& echo "$server - Done!" >> log.log
|| echo "$server - Failed!" >> log.log
) &
done < all_servers.txt
wait
echo "$(date) - Process completed!!"
SSH keys
I highly recommend learning more about SSH. The scp -B flag was unknown to me because I'm used to using SSH keys and ssh-agent, which will make such connectivity seamless (use passwordless keys if you're running this in a cron job).

the bash script only reboot the router without echoing whether it is up or down

#!/bin/bash
ip route add 10.105.8.100 via 192.168.1.100
date
cat /home/xxx/Documents/list.txt | while read output
do
ping="ping -c 3 -w 3 -q 'output'"
if $ping | grep -E "min/avg/max/mdev" > /dev/null; then
echo 'connection is ok'
else
echo "router $output is down"
then
cat /home/xxx/Documents/roots.txt | while read outputs
do
cd /home/xxx/Documents/routers
php rebootRouter.php "outputs" admin admin
done
fi
done
The other documents are:
lists.txt
10.105.8.100
roots.txt
192.168.1.100
when i run the script, the result is a reboot of the router am trying to ping. It doesn't ping.
Is there a problem with the bash script.??
If your files only contain a single line, there's no need for the while-loop, just use read:
read -r router_addr < /home/xxx/Documents/list.txt
# the grep is unnecessary, the return-code of the ping will be non-zero if the host is down
if ping -c 3 -w 3 -q "$router_addr" &> /dev/null; then
echo "connection to $router_addr is ok"
else
echo "router $router_addr is down"
read -r outputs < /home/xxx/Documents/roots.txt
cd /home/xxx/Documents/routers
php rebootRouter.php "$outputs" admin admin
fi
If your files contain multiple lines, you should redirect the file from the right-side of the while-loop:
while read -r output; do
...
done < /foo/bar/baz
Also make sure your files contain a newline at the end, or use the following pattern in your while-loops:
while read -r output || [[ -n $output ]]; do
...
done < /foo/bar/baz
where || [[ -n $output ]] is true even if the file doesn't end in a newline.
Note that the way you're checking for your routers status is somewhat brittle as even a single missed ping will force it to reboot (for example the checking computer returns from a sleep-state just as the script is running, the ping fails as the network is still down but the admin script succeeds as the network just comes up at that time).

Resources