Solved restarting network

I am having trouble restarting network services.
I made a very simple shell script (OpenVPN kill switch) that checks sockstat every 60 seconds to see if OpenVPN is running, if not, it runs the command `ifconfig ue0 down' (not tun0, the VPN device, but the ethernet connection).
It works just fine, but once this script kills the internet, I cannot get it back.

I have tried running:
Code:
# ifconfig ue0 up
But that doesn't even give me the ability to connect back through SSH on my LAN. Even though when I run `ifconfig ue0' I can see the IP, 192.168.0.101, which is the IP I use to connect with.
I have tried:
Code:
# /etc/rc.d/netif restart && /etc/rc.d/routing restart
But that does nothing ether. I cannot connect through SSH over LAN or even ping www.google.com, it just says the hostname does not resolve. (Right after I run the command, I can ping www.google.com just fine, but after a few seconds it goes back to saying cannot resolve host)
I also tried:
Code:
# dhclient ue0
and
Code:
# /etc/rc.d/dhclient ue0 restart
Once again, right after I run the command I am able to ping www.google.com, but a few seconds later it goes back to not being able to resolve hostname.

The only thing that fixes this is a complete reboot.
Code:
# reboot


Here is the kill switch shell script in case it is relevant
Code:
while [ true ]; do # infinite loop
    var2="$(sockstat | grep openvpn)"
    leng=${#var2}
    if [ "$leng" != "0" ]
    then
        echo "OPENVPN IS [ON] | DO NOTHING"
    else
        echo "OPENVPN IS _OFF_ | KILL INTERNET"
        ifconfig ue0 down
    fi
    sleep 60
done


Edit:
The issue was that after I ran the kill switch I would ctrl-z and background job it. When openvpn lost connection and the script dropped ue0, it would disappear from the `$ jobs' list.
BUT when I logged in as root and ran `ps -e', the script was still running. So every time that I restarted the network, it kept killing it.
 
I made a very simple shell script (OpenVPN kill switch) that checks sockstat every 60 seconds to see if OpenVPN is running, if not, it runs the command `ifconfig ue0 down' (not tun0, the VPN device, but the ethernet connection).
Why are you killing your ethernet connection? Just stop/start/restart (whichever you need) the openvpn service.
 
Why are you killing your ethernet connection? Just stop/start/restart (whichever you need) the openvpn service.
I have programs running that are connecting through the VPN, so if I shut the VPN off, those programs will just switch to my regular internet and keep running (if tun0 drops, they will use ue0), which I don't want. I only want the programs to run while OpenVPN is running. The safest bet is to take the whole internet offline once OpenVPN goes offline so there are no `leaks'.
 
so if I shut the VPN off, those programs will just switch to my regular internet and keep running
Stop them with a script that's triggered with the down command of OpenVPN.

Code:
–down cmd
    Run command cmd after TUN/TAP device close (post –user UID change and/or –chroot ). cmd consists of a path to script (or executable program),
   optionally followed by arguments. The path and arguments may be single- or double-quoted and/or escaped using a backslash, and should be 
   separated by one or more spaces.Called with the same parameters and environmental variables as the –up option above.
 
What if the VPN connection loss happens during the 60 sec sleep? Wouldn't those programs leek in that period? There is a forum posting dealing with the same topic:


It might be worth having a look.
 
Stop them with a script that's triggered with the down command of OpenVPN.

Code:
–down cmd
    Run command cmd after TUN/TAP device close (post –user UID change and/or –chroot ). cmd consists of a path to script (or executable program),
   optionally followed by arguments. The path and arguments may be single- or double-quoted and/or escaped using a backslash, and should be
   separated by one or more spaces.Called with the same parameters and environmental variables as the –up option above.
I thought of that but some programs like rtorrent have no command line shutoff command, I would have to `pkill' the process. And I am worried if rtorrent is in the process of writing data to a hard drive, it might lock it or corrupt it and then I'll have to run fsck or something.
 
OMG I fixed the problem.
I was logged into my usual account, I run the kill switch as root (so it can drop ue0)
Code:
$ sudo sh killswitch.sh
then I ctrl-z and `$ bg %1' background it.
I check on it with `jobs', but once openvpn `dies' it disappears from jobs.
Well...
I logged in as root,
ran `ps -e'
and guess what was still running?
That is why everytime I reset the internet with netif, routing or dhclient it would work for a few seconds and then stop.
I killed the process and ran `dhclient restart ue0' and everything works again.
 
Back
Top