1

I ended up making this script on a DD-WRT router that is creating two unwanted files and I can't seem to understand why. I'm not a scripting genius, especially with bash, so any help would be appreciated.

#!/bin/sh

sleep 30
mkdir /tmp/myvpn; cd /tmp/myvpn

# ... doing stuff...

while [ 1 ]
do
  r1=`wget -q http://ipinfo.io/ip` > /dev/null
  r2=`$(wget -q "http://ipinfo.io/$r1/country")` > /dev/null
  if [ $r2 -eq "XX" ] 
    then echo "XX OK" > /tmp/myvpn/result_check_vpn.txt
    else
        echo "XX NOK" > /tmp/myvpn/result_check_vpn.txt
        killall -q openvpn
        sleep 5
        openvpn --config /tmp/openvpncl/openvpn.conf --route-up /tmp/myvpn/route-up.sh --down-pre /tmp/myvpn/route-down.sh --daemon
        sleep 25
        sh /proc/net/ip_conntrack_flush
  fi
sleep 180
done

This script creates two unwanted files in /tmp/myvpn called "country" and "ip". I must be doing something wrong. Basically the script tries to check if the VPN is established right by checking if I effectively changed country (it is necessary for me to perform this check).

1
  • 1
    I don't understand. If you don't want the files, why are you downloading them? Your wget commands explicitly download those two files. Please edit your question and explain what you expected r1=`wget -q http://ipinfo.io/ip` > /dev/null to do. Also, the first step in debugging is to start deleting lines until you identify the specific line that's giving you the error.
    – terdon
    Apr 28, 2017 at 18:04

1 Answer 1

6

The files are being created by these two commands:

r1=`wget -q http://ipinfo.io/ip` > /dev/null
r2=`$(wget -q "http://ipinfo.io/$r1/country")` > /dev/null

OK, the second is a syntax error, but I asume that's a typo. The wget command's format is:

wget http://www.example.com/file

It will then download file and save it in the current directory. Since you haven't told us what you expected to happen, I will guess that you wanted to save the file contents in the variables. If so, you need to tell wget to print to standard output using -O -:

r1="$(wget -qO - http://ipinfo.io/ip)"
r2="$(wget -qO - http://ipinfo.io/$r1/country)" 

You can't redirect the output (> /dev/null) of course, since that means nothing is printed.

1
  • That was it, thank you very much. I forgot the -O command param. I was so far into it that I completely forgot that the first purpose of the wget commands is to actually download files... Anyway, thank you very much for your patience and help
    – Yass T
    Apr 29, 2017 at 14:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .