Bash script to update Deluged Interface IP fails to run as cron job, but works when run manually - linux

I'm running a Debian Linux variant, OpenVPN, and Deluge.
My VPN provider has short IP leases, so every few days I need to update Deluge's interface IP so that it downloads only on tun0. I put together a script to automate this - it basically puts the current tun0 IP into $tun0ip, then does a grep check against the deluge daemon config file to see if that string is present (this is a dirty way to do it, but I guess it works).
My problem is this: When I call the script manually, it works as intended - it kills deluged and then relaunches it, specifying the new IP with deluged -i $tun0ip . However, when I run the script as a cron job, it fails - it passes a null or zero value to $tun0ip, and then deluged -i $tun0ip doesn't work without a valid IP specified, so the application fails to launch. Script below. Am I doing something wrong here? I really appreciate any help!
#!/bin/bash
tun0ip=$( ifconfig tun0 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')
if grep -q $tun0ip "/root/.config/deluge/core.conf"; then
#Great, the IP is correct - don't have to do anything.
echo "IP Looks good - no changes were made."
else
echo "tun0 IP not found in config file. Killing deluged and rebooting with $tun0ip as interface."
killall deluged
sleep 5
deluged -i $tun0ip
fi

I had to specifiy /sbin/ifconfig, like this:
tun0ip=$( /sbin/ifconfig tun0 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*'$

Related

Extracting IP address from cron not working

I have a script and I am running it as scheduled via cron. In that script I have line to extract IP address of that machine and that line is below
ip_address=$(ip addr show | grep -E -o '(inet ([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}))' | grep -v 'inet 127.0.0.1' |
sed 's/inet//g')
If I am just executing above line on bash command line its working and echo $ip_address giving me IP of that machine but If I am calling it via crobjob , echo $ip_address giving empty output.
even If I call entire script from bash shell, I am getting IP in the output but only via cron I am not getting the IP in the output.
Could someone please help ?
Thank you.

Execute a remote script on a local server in Linux using ssh

I have a script called "test" on a remote server's home folder that contains the following lines:
#!/bin/bash
tengbe=`ifconfig | grep -B1 192.168 | awk 'NR==1 { print $1 }' | sed 's/://g'`
Basically, it just stores the name of the interface of the local server into a variable once the script is called. When I ssh into the remote server to call the script, I get an error:
ssh remoteserver-IP './test'
./test: line 3: ifconfig: command not found
What might be wrong with my script? I have seen various answers that does not offer a solution to my issue.
Try:
$ ssh remotehost ifconfig
bash: ifconfig: command not found
ifconfig isn't in your PATH on the remote host. You can prove it with:
$ which ifconfig
/sbin/ifconfig
$ ssh remotehost 'echo $PATH'
(returns lots of dirs, none of which is /sbin)
To get around this either specify the full path to ifconfig:
$ ssh remotehost /sbin/ifconfig
... or configure $PATH before calling it:
$ ssh remotehost 'PATH=$PATH:/sbin ifconfig'
... or edit $HOME/.bashrc (or alternatives -- read about your shell's initialisation process) to add /sbin to $PATH all the time.
For security, in a script it's usually better to specify absolute paths, maybe via variables. So in your script:
#!/bin/bash
IFCONFIG=/sbin/ifconfig
tengbe=$(${IFCONFIG} | grep -B1 192.168 | awk 'NR==1 { print $1 }' | sed 's/://g')
Note, I've replaced your backticks with $() - this isn't required, but it's a good habit to adopt - What is the benefit of using $() instead of backticks in shell scripts?

Search and kill process, and start new process on bash script

I need a script for running a new process every hour.
I created a bash script that is scheduled to run every hour through cron. It only works the first time but fails otherwise.
If run from shell, it works perfectly.
Here is the script:
#!/bin/sh
ps -ef | grep tcpdump | grep -v grep | awk '{print $2}' | xargs kill
sleep 2
echo "Lanzando tcpdump"
tcpdump -ni eth0 -s0 proto TCP and port 25 -w /root/srv108-$(date +%Y%m%d%H%M%S).smtp.pcap
cron
#hourly /root/analisis.sh > /dev/null 2>&1
Why is the cron job failing?
This is the answer the OP added to the question itself.
Correction of the script after the comments (it works fine)
#!/bin/bash
pkill -f tcpdump
/usr/sbin/tcpdump -ni eth0 -s0 proto TCP and port 25 -w /root/srv108-$(date +%Y%m%d%H%M%S).smtp.pcap
That is, I just needed to use the full path to tcpdump.
The failure may be related to the cron job never finishing - you are starting a new tcpdump in the foreground, which will run forever.
Try this simplified script:
#!/bin/bash
killall tcpdump
tcpdump -ni eth0 -s0 proto TCP and port 25 -w /root/srv108-$(date +%Y%m%d%H%M%S).smtp.pcap&

Starting sshd automatically if it is down/failed

I am trying to create a Bash .sh script for a cronjob that starts the OpenSSH server if it is down or failed.
Last night the SSH server was down and when I tried to access it today (from work) the connection was refused ofc.
No traces in the /var/log/messages for the failure.
So the question is - how to determine is sshd running so if it is not to "sudo service ssh start" it?
Thanks in advance!
Fellas, I believe that I have managed to do the task:
#!/bin/bash
service="ssh"
if (( ! $(sudo service ssh status | cut -d" " -f 3 | cut -d"." -f 1) == "running" ))
then
sudo /etc/init.d/ssh restart
fi
I have changed the LogLevel to Verbose, I hope the next time I will track more clues regarding the failure of the sshd.
I'm implementing the following:
[ $(sudo service ssh status | grep running | grep -v grep | wc -l) == 0 ] && sudo /etc/init.d/ssh restart
as other people said It's Incorrect to start it without finding what caused this problem, but I wrote some help for the script you want
first you should check the status of your openssh server for example:
ps -aux | grep ssh
then write an if to check if it is down or not
you can check it with the result of previous step.
if that was down, start it
sudo service ssh start

Shell script to find the IP address of an Virtual machine created by using KVM/virsh command

I am looking for the shell script to find the IP address of a virtual machine created by using KVM/VIRSH.
I used the following steps to get it so, but couldn´t able to find it.
Ping the IP addresses in range
2.Use Virsh list command to list all the active VM
3.use Virsh dumpxml domainname and project the xml of vm
use grep command and fetch the Hardware address of vm
5.Display the hardware address of each vm
Now I would like to add one more step like fetching the IP address for that particular Hardware address using ¨arp -ne¨
I couldn´t able to figure out how to add the part.
could any one help me on this.
for i in {1..150}
do
ping -c 1 -n -q -r -t 1 -s 1 -W 1 192.168.1.$i > /dev/null &
done
for name in `virsh list | grep running | awk '{ print $2 }'`
do
# printf "\n$name\n "
arp -e | grep "`virsh dumpxml $name | grep "mac address"|sed "s/.*'\(.*\)'.*/\1/g"`" |
awk '{ printf $1 ; printf " " ; printf $3 "\n" }'
done
current output:
$ ./virshshell.sh
vm2 52:54:00:4b:7f:41
vm3 52:54:00:0e:4c:42
The output I am expecting is
$ ./virshshell.sh
vm2 52:54:00:4b:7f:41 192.*.*.*
vm3 52:54:00:0e:4c:42 192.*.*.*
Use nmap to do network discovery instead of ping. It can do what ping does but also much more, plus it runs way faster and takes care of the network-scope scanning that you're doing in your for loop.
$ nmap -T5 -n -PE 192.168.4.0/24 > /dev/null
$ ip neigh show | grep 192.168.4 | grep -v FAILED
192.168.4.92 dev eth0 lladdr 54:52:00:90:90:92 REACHABLE
192.168.4.11 dev eth0 lladdr fa:16:3e:fa:ac:07 REACHABLE
192.168.4.91 dev eth0 lladdr 54:52:00:90:90:91 REACHABLE
192.168.4.90 dev eth0 lladdr 54:52:00:90:90:90 REACHABLE

Resources