0

I'm having an issue with the xfce4-genmon-plugin. I'm using it to monitor my vpn connection. Whenever I'm connected to my VPN (NordVPN using openpyn), it displays a symbol and the server I'm connected to with this command : $(ps ax | grep $(pidof openvpn) | perl -n -e '//(\w+) .nordvpn.com/ && print $1')

I recently decided I would also like it to display my current IP address (WAN)

I've written a short bash script that regularly checks the IP address with the command $(wget http://checkip.dyndns.org/ -O - -o /dev/null | cut -d: -f 2 | cut -d\< -f 1) This is stored in an environment variable called IPPUBLIQUE and it works fine.

The next step was to write a script for the generic monitor plugin which is here :

#!/bin/bash
echo $IPPUBLIQUE
ip=$IPPUBLIQUE
echo $ip
ip="IP:$ip"
echo $ip
checkvpn=$(nmcli con |grep tun0)

if [ -n "$checkvpn" ]
 then
   echo "<img>/home/richard/Scripts/vpn.png</img>"
   echo "<txt>$ip; "$(ps ax | grep $(pidof openvpn) | perl -n -e '/\/(\w+)    \.nordvpn\.com/ && print $1')"</txt>"
   echo "<click>/home/richard/bin/./killvpngenmon.sh</click>"
   echo "<tool>Connected to VPN</tool>"
   echo "<img>/home/richard/Scripts/novpn.png</img>"

 else
   echo "<img>/home/richard/Scripts/novpn.png</img>"
   echo "<txt>$ip</txt>"
   echo "<click>gksudo /home/richard/bin/autovpn</click>"
   echo "<tool>Not connected to VPN</tool>"
   echo "<img>/home/richard/Scripts/vpn.png</img>"
fi

When I test the script in a console, the values of each variable is correct and are printed out with the echo commands. They ll contain the correct IP addresses. However the monitor only displays the $ip as "IP: ". No matter what I do, it refuses to display my IP address, but if I set the variable manually (for example ip=192.168.0.1 instead of ip=$IPPUBLIQUE) then it works.

Console Output

Genmon-plugin output

Can anyone help figure out what is going on and how I can correct it?

Thanks a lot

RHarris
  • 23
  • 6
  • Yes, it will not work unless you have set a value to it, set the value as `IPPUBLIQUE` to the IP after `#!/bin/bash` or pass it as one of the arguments from the command line – Inian May 30 '18 at 09:16
  • What do you mean? Would you mind showing me an example? – RHarris May 30 '18 at 09:37
  • The script you have doesn't set the value to the variable, How else do you expect it to contain the IP – Inian May 30 '18 at 09:39
  • Actually the IPPUBLIQUE is set in another script and when I run in the console I get this output : 77.136.204.197 77.136.204.197 IP: 77.136.204.197 /home/richard/Scripts/novpn.png IP: 77.136.204.197 gksudo /home/richard/bin/autovpn Not connected to VPN /home/richard/Scripts/vpn.png So the variables seem set, but not displayed afterwards – RHarris May 30 '18 at 09:43

2 Answers2

0

So, the script run from genmon doesn't have the value of IPPUBLIQUE. Why don't you just get the public ip within the script?

#!/bin/bash
IPPUBLIQUE=$(wget http://checkip.dyndns.org/ -O - -o /dev/null | cut -d: -f 2 | cut -d\< -f 1)
echo $IPPUBLIQUE
.... rest of the script ...

You can export the value of IPPUBLIQUE to a file:

echo "IPPUBLIQUE=$(wget http://checkip.dyndns.org/ -O - -o /dev/null | cut -d: -f 2 | cut -d\< -f 1)" > /tmp/.IPPUBLIQUE

And then import the file from your script:

#!/bin/bash
. /tmp/.IPPUBLIQUE
echo $IPPUBLIQUE
.... rest of the script ...
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • I didn't want to run #IPPUBLIQUE=$(wget http://checkip.dyndns.org/ -O - -o /dev/null | cut -d: -f 2 | cut -d\< -f 1) because the script is run every 5 seconds. I'll try exporting it to a file. Thanks a lot – RHarris May 30 '18 at 12:37
0

I tried creating a file and importing it afterwards in the genmon script.

One script gets the WAN address and store it in the file /tmp/.adresse : (here it is)

#!/bin/bash
ip=$(wget http://checkip.dyndns.org/ -O - -o /dev/null | cut -d: -f 2 | cut -d\< -f 1)
ip="${ip:1}"
echo $ip > /tmp/.adresse

as suggested by Kamil Cuk (thanks), which works fine however the second part doesn't work but I found a work around (basically if I store the content of the file with 'cat' in a variable, it doesn't display but it will if I execute cat directly)

Code 1 (doesn't work)

#!/bin/bash

ip= cat /tmp/.adresse
echo $ip
checkvpn=$(nmcli con |grep tun0)
echo $checkvpn

if [ -n "$checkvpn" ]
 then

  echo "<img>/home/richard/Scripts/vpn.png</img>"
  echo "<txt>$ip; "$(ps ax | grep $(pidof openvpn) | perl -n -e '/\/(\w+)\.nordvpn\.com/ && print $1')"</txt>"
  echo "<click>/home/richard/bin/./killvpngenmon.sh</click>"
  echo "<tool>Connected to VPN</tool>"
  echo "<img>/home/richard/Scripts/novpn.png</img>"

 else

  echo "<img>/home/richard/Scripts/novpn.png</img>"
  echo "<txt>$ip</txt>"
  echo "<click>gksudo /home/richard/bin/autovpn</click>"
  echo "<tool>Not connected to VPN</tool>"
  echo "<img>/home/richard/Scripts/vpn.png</img>"

fi

Code 2 (The workaround)

#!/bin/bash

checkvpn=$(nmcli con |grep tun0)
echo $checkvpn

if [ -n "$checkvpn" ]
 then

  echo "<img>/home/richard/Scripts/vpn.png</img>"
  echo "<txt>"$(cat /tmp/.adresse)"; "$(ps ax | grep $(pidof openvpn) | perl -n -e '/\/(\w+)\.nordvpn\.com/ && print $1')"</txt>"
  echo "<click>/home/richard/bin/./killvpngenmon.sh</click>"
  echo "<tool>Connected to VPN</tool>"
  echo "<img>/home/richard/Scripts/novpn.png</img>"

 else

  echo "<img>/home/richard/Scripts/novpn.png</img>"
  echo "<txt>"$(cat /tmp/.adresse)"</txt>"
  echo "<click>gksudo /home/richard/bin/autovpn</click>"
  echo "<tool>Not connected to VPN</tool>"
  echo "<img>/home/richard/Scripts/vpn.png</img>"

fi

Everything works so it's all good, but I don't understand why code 1 doesn't work, can anybody explain me why solution 2 works but not 1 ?

Thanks for all your help

RHarris
  • 23
  • 6