This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Automating PPP0 Tunnels (kvpnc, ipredator, PPTP protocol)

Tags: None
(comma "," separated)
imported4-Sam
Registered Member
Posts
22
Karma
0
The attached script will automate a connection to the somewhat buggy ipredator service. And with a few minor changes any other tunnelling service / protocol you want to use.

To use the script you will first need to setup KVpnc to correctly connect to your tunnelling service:
- use chap secrets (or equivalent)
- un-check "Use KWallet" (Settings > Configure KVpnc > General)
- Add "/etc/sysconfig/network-scripts/ifup eth0" to Profile :: Command execution :: Before Connect
- Add "/etc/sysconfig/network-scripts/ifdown eth0" to Profile :: Command execution :: After Disconnect
- Google the rest of the setup instructions for linux

Once KVpnc works manually, configure KVpnc to Automatically connect at startup. (Settings > Configure KVpnc > General > Connect)

And then ignore ALL subsequent password requests (idiot program keeps asking for it even when it's already in chaps).

You will then need to add KVpnc and killall to /etc/sudoers (But you already needed to add KVpnc just to get KVpnc to run correctly, so do the same for killall)

You will also have to configure your eth interface to: "Allow users to manage the connection"

Optionally have KVpnc:
- Minimize after good connect
- Disable all notifications (popups)

The script [was] too spammy and I'll add a cleaned up version to http://www.inet-design.com/ktorrent-scr ... ation.html

There's probably a better way, there usually is,
Sam

PS:
See also viewtopic.php?p=16240#p16240

Edit:
11-07-02: Cleaned up and Prettified the script. If you want the spam, uncomment the 2nd echo.
11-07-16: More cleaning, now shows stats without spamming the terminal.
imported4-Sam
Registered Member
Posts
22
Karma
0
I'm tired of the file upload gizmo and this script has a few additional features that aren't relevant to just automating tunnels. The attached script in the first post is without the additions. This one is significantly 'prettier' and has better error handling. If I can get dbus to do what I want I'll update this script further.

Code: Select all
#!/bin/bash

####
# KTorrent 'helper' script for Automating PPP0 Tunnels (kvpnc, ipredator, PPTP protocol)
#   and controlling the Queue Manager's Maximum Seeds.
#
# Script=ktorup
# Author=Sam
# Email=sam_ktorrent_wiki at the commercial domain inet-design
# Website=http://www.inet-design.com/
# License=GPL
# Script repository=http://www.inet-design.com/ktorrent-scripts-and-information.html
# Last Edit Date : 11-07-22 08:00
#
##
#  Reason:
#   - ipredator service drops connections fairly frequently, so
#   - Want KT to only run when tunnel is up, and
#   - With ~2K torrents, KT crashes alot (cleaning up broken symlinks helps correct alot of this)
##
#  Does:
#   - Automates connecting to tunneling services using KVpnc
#   - Starts and stops KT based on if ppp0 is up
#   - Restarts KT if crashed
#   - Turns off KDE crash handler
#   - Captures / Eliminates crashes from old C++ error
#       See thread:  http://ubuntuforums.org/showthread.php?t=175050
#   - Calculates and sets Queue Manager's Maximum Seeds based on number of unused Downloads slots
##
#   Additionally:
#     You can add the below to your .profile to disable coredumps
#       "limit coredumpsize 0"
#     For Gnome:  also remove package "bug buddy"
##
#   Set:
LOGFILE="/your-dir/ktorup.log"
TOTALTORRENTS=17 # number of total torrents you want to have running
#
####

####
#   Prettify things:
INVT="\033[7m"; NORM="\033[0m"; BOLD="\033[1m"; BLINK="\033[5m"
BLACK_F="\033[30m"; BLACK_B="\033[40m"; RED_F="\033[31m"; RED_B="\033[41m"
GREEN_F="\033[32m"; GREEN_B="\033[42m"; YELLOW_F="\033[33m"; YELLOW_B="\033[43m"
BLUE_F="\033[34m"; BLUE_B="\033[44m"; MAGENTA_F="\033[35m"; MAGENTA_B="\033[45m"
CYAN_F="\033[36m"; CYAN_B="\033[46m"; WHITE_F="\033[37m"; WHITE_B="\033[47m"
####

## Leave these here (or add to your .bashrc)
# stops "*** glibc detected *** double free or corruption" crashes
export MALLOC_CHECK_=0
# stops KDE crash handler
export KDE_DEBUG=1
##

#### Let's Begin
D00=`date`
MESG="Start"
echo -e "             ## "$RED_F$BOLD$MESG$NORM" ##           " $D00 | tee -a "$LOGFILE"
newSeeds="Default Value"
oldSeeds=0

while [ 1 ]
do
   D00=`date +"%y-%m-%d %H:%M:%S"`
   TEST=`/sbin/ifconfig | grep ppp0 | awk '{print $1}'`
   if [ $TEST ]; then
#      TEST2=`ps -A | grep ktorrent | awk '{print $1}'` # hmph! occasionally returns multiple pids, breaks IF, use if your system doesn't do pidof
      TEST2=`pidof -s  ktorrent`
      if [ $TEST2 ]; then
         MESG="KT Running"
         numTorrentsRunning=`qdbus org.ktorrent.ktorrent /core numTorrentsRunning`
         if [[ "$numTorrentsRunning" =~ ^-?[0-9]+$ ]] ; then  # skip when dbus throws errors cause KT is starting up or shutting down
            if [ $TOTALTORRENTS -ne $numTorrentsRunning ]; then
               maxSeeds=`qdbus org.ktorrent.ktorrent /settings maxSeeds`
               let newSeeds=($TOTALTORRENTS-$numTorrentsRunning)+$maxSeeds
               captureTrash=`qdbus org.ktorrent.ktorrent /settings setMaxSeeds $newSeeds`
               captureTrash=`qdbus org.ktorrent.ktorrent /settings apply`
               newNumTorrentsRunning=`qdbus org.ktorrent.ktorrent /core numTorrentsRunning`
               echo -e $YELLOW_F$D00$NORM" Seed Change, Active: "$CYAN_F$numTorrentsRunning$NORM"->"$GREEN_F$newNumTorrentsRunning$NORM", Seeding: "$CYAN_F$oldSeeds$NORM"->"$GREEN_F$newSeeds$NORM"                 "
               oldSeeds=$newSeeds
               numTorrentsRunning=$newNumTorrentsRunning
            fi
            echo -en $YELLOW_F$D00$NORM" Found ppp0, "$CYAN_F$MESG$NORM", Active: "$GREEN_F$numTorrentsRunning$NORM", Seeding: "$GREEN_F$newSeeds$NORM
            for (( i=0 ; i < 80 ; i++ )) ; do echo -en "\b" ; done
         fi
      else
         MESG="Starting KT"
         echo -e $YELLOW_F$D00$NORM" Found ppp0                                 "$GREEN_F$MESG$NORM | tee -a "$LOGFILE"
         nohup ktorrent & # all good, start KT
#         use below to suppress all messages from KT
#         ktorrent  > /dev/null 2>&1
         sleep 30s # let KT finish forking
         D00=`date +"%y-%m-%d %H:%M:%S"`
         OLDIP=$NEWIP
         NEWIP=`/sbin/ifconfig ppp0|grep inet|awk {'print $2'}|cut -d":" -f2`
         OLDPID=$NEWPID
         NEWPID=`pidof -s  ktorrent`
         echo -e $YELLOW_F$D00$NORM" Stats - PID: "$CYAN_F$NEWPID$NORM", OIP: "$CYAN_F$OLDIP$NORM", NIP: "$CYAN_F$NEWIP$NORM | tee -a "$LOGFILE"
         sleep 300s # let KT finish loading
         maxSeeds=`qdbus org.ktorrent.ktorrent /settings maxSeeds`
         if [[ "$maxSeeds" =~ ^-?[0-9]+$ ]] ; then  # skip when dbus throws errors cause KT is starting up or shutting down
            oldSeeds=$maxSeeds # bit of a hack, no real way to get currently running seeds
            newSeeds=$maxSeeds
         fi
      fi
   else
      MESG="Resetting"
      echo -e $YELLOW_F$D00$NORM" No ppp0                                    "$RED_F$BOLD$MESG$NORM | tee -a "$LOGFILE"
      qdbus org.ktorrent.ktorrent /MainApplication quit # our IP address will change, let KT quit nicely
      sudo killall kvpnc # don't care if it goes down hard
      /etc/sysconfig/network-scripts/ifdown eth0 # shut down eth0, just in case
      sleep 30s # let everything cleanup
      nohup sudo kvpnc --nocrashhandler & # bring up the tunnel, fork to background (doesn't work)
      sleep 30s # wait an extra bit, don't want to spam the provider in case of failure(s) or slow connects
   fi
   sleep 30s # take a nap, do it again
done
theZenman
Registered Member
Posts
1
Karma
0
Hi,

I don't know if this achieves the same thing you want, but it works well enough for me. Its much simpler as I am really interested in is if the connection to the VPN ever drops, then run a command to pause KTorrent. When the connection goes back up again, un-pause KTorrent.

I wrote this bash script, and kept it in a directory with other scripts:
Code: Select all
#!/bin/bash

CONN_STAT=$1
KTORPID=`pidof -s  ktorrent`

if [ $KTORPID ]; then
    if [ $CONN_STAT == 0 ]; then # Lost connection
        # Suspend torrents if ktorrent was running
        DISPLAY=:0 sudo -i -u <YOURUSERNAME> -- qdbus org.ktorrent.ktorrent /core org.ktorrent.core.setSuspended 1
    else
        # Restore torrents
        DISPLAY=:0 sudo -i -u <YOURUSERNAME> -- qdbus org.ktorrent.ktorrent /core org.ktorrent.core.setSuspended 0
    fi
fi


Substitute <YOURUSERNAME> above for your username

Then, in kvpnc, I told it to run the following commands.

After disconnect:
/home/<YOURUSERNAME>/scripts/kvpnc_updown 0

After connect:
/home/<YOURUSERNAME>/scripts/kvpnc_updown 1

This works for me because as soon as kvpnc sees the connection drops, it tries to reconnect again. So after the connection terminating, it runs the "After disconnect" command, and then the "After connect" command when it reconnects.

I decided to post this here because maybe the dbus call my solution uses helps you out, if that is indeed what you are looking for. Otherwise, this can just be an alternative for some if it works out for them.
chensi
Registered Member
Posts
1
Karma
0
THANK YOU
i think i should thank you very much for you can waste lot of time to write this post .....thank you again for your sharing..


Bookmarks



Who is online

Registered users: Bing [Bot], daret, Google [Bot], sandyvee, Sogou [Bot]