Ifplugd: Error "Incorrect action argument" - bash

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

Related

Bash script and netctl

I'm trying to create a little script to automate the wlan-login. (I don't have a GUI or something for my network-management.)
Here is the problem:
The command "netctl list" usually lists all available (wireless) profiles. It works fine if I just drop the command in a terminal, but used in a script it returns really weird stuff. The output is the same as I'd combine the output of "ls" and "netctl list". Please note the specific codepart is below:
PS3="Choose a profile: "
prof_select=$(netctl list)
select profile in $prof_select
do
echo "[+] Stopping all other profiles..."
`netctl stop-all`
wait
echo "[+] Starting profile" $profile "..."
`netctl start $profile`
wait
break
done
FYI: I'm using Arch Linux. I have (for wireless connections) iw and wpa_supplicant installed, and on the manual way everything works fine.
The complete script is:
#!/bin/bash
# Select IF
PS3='Choose an interface: '
if_select=$(ip -o link show | awk -F': ' '{print $2}')
select interface in $if_select
do
echo "[+] Shutting down IF"
# Shut down the interface
ip link set dev $interface down
# Break, otherwise endless loop
break
done
# Spoof MAC?
read -p "[?] Want a spoofed MAC? (Y/N)" answer
while true
do
case $answer in
[yY]* )
echo "[+] Current MAC: " $(ip link show $interface | grep link/ether)
wait
ip link set dev $interface address 16:60:51:50:DB:82
wait
echo "[+] New MAC: " $(ip link show $interface | grep link/ether)
break;;
[nN]* ) break;;
*) echo "[-] This might be an answer, but not the one im looking for :/"; exit 0;;
esac
done
IFS=$'\t *' read -r -a profiles < <(netctl list)
#prof_select=$(netctl list)
select profile in "${profiles[#]}"
do
echo "[+] Stopping all other profiles..."
netctl stop-all
wait
echo "[+] Starting profile" $profile "..."
netctl start $profile
wait
break
done
exit 0

trying to create my bash script

I am trying to create a simple bash script.
(just started working on bash scripting)
the script is simple. There is an issue with rsyslog service and from time to time dies.
I am trying to make a script to check if service is dead to restart it
till now I want to check if my conditions are ok but it seems I am getting an error. Can you advice me ?
Here is the script:
#!/bin/bash
a="dead"
b="running"
while i in $(/etc/init.d/rsyslog status | grep -o 'running\|dead');
do
if
[ "$a" == "$i" ];
then
echo "service rsyslog is dead "
if
[ "$b" == "$i" ];
then
echo "service rsyslog is running"
else
echo "nothing to do";
fi;
done
-------------
I am getting the following syntax error.
./rsyslogcheck.sh: line 17: syntax error near unexpected token done'
./rsyslogcheck.sh: line 17:done'
Thank you in advance!!
There are several problems here:
Invalid while loop syntax
Unnecessary loop: it seems you don't need a loop at all
Missing closing fi of an if that was opened
I suppose you're looking for something like this:
#!/bin/bash
status=$(/etc/init.d/rsyslog status | grep -o 'running\|dead')
case "$status" in
dead) echo "service rsyslog is dead";;
running) echo "service rsyslog is running";;
*) echo "nothing to do";;
esac

Convert bash script to 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 ;)

Problem with pidof in Bash script

I've written a script for me to start and stop my Perforce server. To shutdown the server I use the kill -SIGTERM command with the PID of the server daemon. It works as it should but there are some discrepancies in my script concerning the output behavior.
The script looks as follows:
#!/bin/sh -e
export P4JOURNAL=/var/log/perforce/journal
export P4LOG=/var/log/perforce/p4err
export P4ROOT=/var/local/perforce_depot
export P4PORT=1666
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
. /lib/lsb/init-functions
p4start="p4d -d"
p4stop="p4 admin stop"
p4user=perforce
case "$1" in
start)
log_action_begin_msg "Starting Perforce Server"
daemon -u $p4user -- $p4start;
echo "\n"
;;
stop)
echo "BLABLA"
echo "$(pidof /usr/local/bin/p4d)"
#daemon -u $p4user -- $p4stop;
p4dPid="$(pidof /usr/local/bin/p4d)"
echo $p4dPid
if [ -z "$(pidof /usr/local/bin/p4d)" ]; then
echo "ERROR: No Perforce Server running!"
else
echo "SUCCESS: Found Perforce Server running!\n\t"
echo "Shutting down Perforce Server..."
kill -15 $p4dPid;
fi
echo "\n"
;;
restart)
stop
start
;;
*)
echo "Usage: /etc/init.d/perforce (start|stop|restart)"
exit 1
;;
esac
exit 0
When p4d is running the stop block works as intended, but when there is no p4d running the script with stop only outputs BLABLA and an empty new line because of the echo "$(pidof /usr/local/bin/p4d)". The error message stating that no server is running is never printed. What am I doing wrong here?
PS: The part if [ -z "$(pidof /usr/local/bin/p4d)" ]; then has been changed from if [ -z "$p4dPid" ]; then for debug reasons.
EDIT: I narrowed down the problem. If I don't use the p4dPid variable and comment out the lines p4dPid="$(pidof /usr/local/bin/p4d)" and echo $p4dPid the if block is processed and the error messages is printed. Still I don't unterstand what is causing this behavior.
EDIT 2: Problem solved!
The -e in #!/bin/sh -e was causing the shell to exit the script after any statement returning a non-zero return value.
When your service is not running, the command
echo "$(pidof /usr/local/bin/p4d)"
is processed as
echo ""
because pidof did not return any string. So the command outputs an empty line.
If you do not want this empty line, then just remove this statement, after all you print an error message when the process is not running.
Problem solved!
The -e in #!/bin/sh -e was causing the shell to exit after any statement returning a non-zero return value.

Making bash script to check connectivity and change connection if necessary. Help me improve it?

My connection is flaky, however I have a backup one. I made some bash script to check for connectivity and change connection if the present one is dead. Please help me improve them.
The scripts almost works, except for not waiting long enough to receive an IP (it cycles to next step in the until loop too quick). Here goes:
#!/bin/bash
# Invoke this script with paths to your connection specific scripts, for example
# ./gotnet.sh ./connection.sh ./connection2.sh
until [ -z "$1" ] # Try different connections until we are online...
do
if eval "ping -c 1 google.com"
then
echo "we are online!" && break
else
$1 # Runs (next) connection-script.
echo
fi
shift
done
echo # Extra line feed.
exit 0
And here is an example of the slave scripts:
#!/bin/bash
ifconfig wlan0 down
ifconfig wlan0 up
iwconfig wlan0 key 1234567890
iwconfig wlan0 essid example
sleep 1
dhclient -1 -nw wlan0
sleep 3
exit 0
Here's one way to do it:
#!/bin/bash
while true; do
if ! [ "`ping -c 1 google.com; echo $?`" ]; then #if ping exits nonzero...
./connection_script1.sh #run the first script
sleep 10 #give it a few seconds to complete
fi
if ! [ "`ping -c 1 google.com; echo $?`" ]; then #if ping *still* exits nonzero...
./connection_script2.sh #run the second script
sleep 10 #give it a few seconds to complete
fi
sleep 300 #check again in five minutes
done
Adjust the sleep times and ping count to your preference. This script never exits so you would most likely want to run it with the following command:
./connection_daemon.sh 2>&1 > /dev/null & disown
Have you tried omitting the -nw option from the dhclient command?
Also, remove the eval and quotes from your if they aren't necessary. Do it like this:
if ping -c 1 google.com > /dev/null 2>&1
Trying using ConnectTimeout ${timeout} somewhere.

Resources