Jump to content
Not connected, Your IP: 3.145.93.227

Recommended Posts

I want to auto start airvpn cli at boot time (with network lock enabled) and have it connect to server using a specified configuration file on a rasbian system.

 

I've spent 2 full days trying to figure it out myself but I'm at my wits end now!! 

 

I'd really appreciate if someone could explain to me in detail how I can do this. I'm new to linux and struggling with the complexity of this task!

 

Thanks!

Share this post


Link to post

For the benefit of anyone else who might be trying to do this, here is how I managed to do it.

 

Create script file (see notes for template):

 
     sudo nano /etc/init.d/airvpn
 
Make script file executable:
 
     sudo chmod +x /etc/init.d/airvpn
 
Add service: 
 
     sudo update-rc.d airvpn defaults
 
Remove Service:
 
     sudo update-rc.d -f airvpn remove
 
Notes:
 
 
Below is my modification of the template above which restores the iptables to what they were previously after airvpn changes them with the network lock. There is probably a better way to do this but it's the best I could come up with!
 
#!/bin/sh
### BEGIN INIT INFO
# Provides:          airvpn
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO

dir="/usr/lib/AirVPN"
cmd="airvpn -cli -login=irishtechnomonster -password=mypass -netl$
user=""

name=`basename $0`
pid_file="/var/run/$name.pid"
stdout_log="/var/log/$name.log"
stderr_log="/var/log/$name.err"

get_pid() {
    cat "$pid_file"
}

is_running() {
    [ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1
}

restore_iptables() {
    ipv4="/root/.airvpn/iptables.dat"
    if [ -f $ipv4 ]; then
        iptables -F
        iptables -t nat -F
        iptables -t mangle -F
        iptables-restore <$ipv4
        echo "Default IPv4 tables restored"
    else
        echo "Default IPv4 tables not found!"
    fi
}

reset_iptables() {
    if is_running; then
        echo "AirVPN must be stopped to reset iptables."
    else
        iptables --policy INPUT   ACCEPT;
        iptables --policy OUTPUT  ACCEPT;
        iptables --policy FORWARD ACCEPT;
        iptables -Z;
        iptables -F;
        iptables -X;
        echo "iptables reset"
    fi
}

case "$1" in
    start)
    if is_running; then
        echo "Already started"
    else
        echo "Starting $name"
        cd "$dir"
        if [ -z "$user" ]; then
            sudo $cmd >> "$stdout_log" 2>> "$stderr_log" &
        else
            sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" &
        fi
        echo $! > "$pid_file"
        if ! is_running; then
            echo "Unable to start, see $stdout_log and $stderr_log"
            exit 1
        fi
    fi
    ;;
    stop)
    if is_running; then
        echo -n "Stopping $name.."
        kill `get_pid`
        for i in {1..10}
        do
            if ! is_running; then
                break
            fi

            echo -n "."
            sleep 1
        done
        echo

        if is_running; then
            echo "Not stopped; may still be shutting down or shutdown may have $
            exit 1
        else
            echo "Stopped"
            restore_iptables
            if [ -f "$pid_file" ]; then
                rm "$pid_file"
            fi
        fi
    else
        echo "Not running"
    fi
    ;;
    restart)
    $0 stop
    if is_running; then
        echo "Unable to stop, will not attempt to start"
        exit 1
    fi
    $0 start
    ;;
    status)
    if is_running; then
        echo "Running"
    else
        echo "Stopped"
        exit 1
    fi
    ;;
    resetiptables)
        reset_iptables
    ;;
    *)
    echo "Usage: $0 {start|stop|restart|status|resetiptables}"
    exit 1
    ;;
esac

exit 0

 

Hope this helps someone!

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Security Check
    Play CAPTCHA Audio
    Refresh Image

×
×
  • Create New...