Extracting IP address from cron not working - linux

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.

Related

Exclude some errors from python script in linux

I want to exclude some erros from log.
I'm using a Raspberry 3b+.
I use
main.py >> log.txt 2>&1 | grep -v "Network is unreachable"
My error that I don't want is
[tcp # 0x19ccea0] Connection to tcp://192.168.1.32:554?timeout=0 failed: Network is unreachable
I can put the whole error in grep because the ip adress may change
However this error is still in my log.txt file when I run my main.py
You need to pipe to grep, and then redirect grep's output to the file. You're redirecting all the output to the file, so nothing goes to grep.
main.py 2>&1 | grep -v "Network is unreachable" >> log.txt

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?

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

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]*'$

Shell script to compare remote directories

I have a shell script that I am using to compare directory contents. The script has to ssh to different servers to get a directory listing. When I run the script below, I am getting the contents of the server that I am logged into's /tmp directory listing and not that of the servers I am trying to ssh to. Could you please tell me what I am doing wrong?
The config file used in the script is as follows (called config.txt):
server1,server2,/tmp
The script is as follows
#!/bin/sh
CONFIGFILE="config.txt"
IFS=","
while read a b c
do
SERVER1=$a
SERVER2=$b
COMPDIR=$c
`ssh user#$SERVER1 'ls -l $COMPDIR'`| sed -n '1!p' >> server1.txt
`ssh user#$SERVER2 'ls -l $COMPDIR'`| sed -n '1!p' >> server2.txt
done < $CONFIGFILE
When I look at the outputs of server1.txt and server2.txt, they are both exactly the same - having the contents of /tmp of the server the script is running on (not server1 or 2). Doing the ssh +dir listing on command line works just fine. I am also getting the error "Pseudo-terminal will not be allocated because stdin is not a terminal". Adding the -t -t to the ssh command isnt helping either
Thank you
I have the back ticks in order to execute the command.
Backticks are not needed to execute a command - they are used to expand the standard output of the command into the command line. Certainly you don't want the output of your ssh commands to be interpreted as commands. Thus, it should work fine without the backticks:
ssh user#$SERVER1 "ls -l $COMPDIR" | sed -n '1!p' >>server1.txt
ssh user#$SERVER2 "ls -l $COMPDIR" | sed -n '1!p' >>server2.txt
(provided that double quotes to allow expansion of $COMPDIR are used).
first you need to generate keys to login to remote without keys
ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host
then try to ssh without pass
ssh remote-host
then try to invoke in your script but first make sanity check
var1=$(ssh remote-host) die "Cannot connect to remote host" unless $var1;

Bash Script to execute Docker container operations and extract container IP Address

I am trying to write a Linux bash script that creates and runs docker containers that run a simple python web server, extract the container IP Address and make an HTTP Request to the server through a browser. There seem to be some mistakes in my script and I need help in editing it. I am new to bash scripting. I use Ubuntu 13.10
My Script:
#!/bin/bash
for i in {1..2}
do
echo "Creating and Running Container $i "
sudo docker run -name $i PythonServer
IP=sudo docker inspect $i | grep IPAddress | cut -d '"' -f 4
echo "Ip Address is:"
echo "${IP}"
xdg-open 'http://$IP:7111/execute'
done
EDIT: Line 6 of the code doesn't seem to be working as echo "${IP}" is giving a blank output. I want to assign the result of sudo docker inspect $i | grep IPAddress | cut -d '"' -f 4 to IP variable i.e I want to capture the IP Address of the created container and use it in xdg-open 'http://$IP:7111/execute' to make the HTTP Request dynamically. But I'm not getting the right format in which this bash script line should be written: IP=sudo docker inspect $i | grep IPAddress | cut -d '"' -f 4
And also what is the correct format in which I can use IP value in xdg-open 'http://$IP:7111/execute'
This above mentioned script presently shows this message on execution:
vishal#bl-lin-01:~/docker/PythonServer/new$ ./BashScript.sh
Creating and Running Container 1
WARNING: Docker detected local DNS server on resolv.conf. Using default external servers: [8.8.8.8 8.8.4.4]
^Cserving at port 7111
Traceback (most recent call last):
File "/scripts/InitScript.py", line 40, in <module>
httpd.serve_forever()
File "/usr/lib/python2.7/SocketServer.py", line 225, in serve_forever
r, w, e = select.select([self], [], [], poll_interval)
KeyboardInterrupt
unknown option: 1
valid options are:
-license
-copyright
-crlf
-end
-link
-path_name
-tab
-ascii
-apple_macro
-assert_macro
-minmax
-unnamed
default is all checks on; otherwise options specify desired checks
Ip Address is:
Thanks.
This Edited Bash Script works fine now..
#!/bin/bash
for i in {1..2}
do
echo "Creating and Running Container $i "
sudo docker run -d -name $i PythonServer
IP=$(sudo docker inspect $i | grep IPAddress | cut -d '"' -f 4)
echo "Ip Address is:$IP"
xdg-open "http://$IP:7111/execute"
done
I ran the containers in Daemon mode and used the Command Substitution feature of bash to assign the output of the command to variable IP
The best way to obtain docker container IP address is
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $CONTAINER_NAME

Resources