conn

A script repository to manage connections in Linux.
git clone git://r-36.net/conn
Log | Files | Refs | README | LICENSE

run.sh (2536B)


      1 #!/bin/sh
      2 
      3 . ./common.sh
      4 
      5 mkpaths
      6 
      7 usage() {
      8 	echo "usage: `basename $1` [-hfnwlo] [-[s|k|u|r] [connection [args ...]]"
      9 }
     10 
     11 dostart=0
     12 dokill=0
     13 dohup=0
     14 dorestart=0
     15 dolist=0
     16 dolistopen=0
     17 dosuspend=0
     18 dowakeup=0
     19 doforce=0
     20 
     21 cmdarg=""
     22 while getopts "fwnskurlo" opt;
     23 do
     24 	case $opt in
     25 		s)
     26 			dostart=1
     27 			cmdarg="-s"
     28 			;;
     29 		k)
     30 			dokill=1
     31 			cmdarg="-k"
     32 			;;
     33 		u)
     34 			dohup=1
     35 			cmdarg="-u"
     36 			;;
     37 		r)
     38 			dorestart=1
     39 			;;
     40 		l)
     41 			dolist=1
     42 			;;
     43 		o)
     44 			dolistopen=1
     45 			;;
     46 		n)
     47 			dosuspend=1
     48 			;;
     49 		w)
     50 			dowakeup=1
     51 			;;
     52 		f)
     53 			doforce=1
     54 			;;
     55 		*)
     56 			usage $0
     57 			exit 1
     58 			;;
     59 	esac
     60 done
     61 shift `expr $OPTIND - 1`
     62 
     63 if [ $dolist -eq 1 ];
     64 then
     65 	echo "Available network types:"
     66 	for i in `ls -d */ | sed 's~/~~g'`;
     67 	do
     68 		echo $i
     69 	done
     70 
     71 	exit 0
     72 fi
     73 
     74 if [ $dolistopen -eq 1 ];
     75 then
     76 	conns=`getstate "open"`
     77 	if [ -z "$conns" ];
     78 	then
     79 		echo "There are no connections open."
     80 		exit 1
     81 	else
     82 		echo "Open connections:"
     83 		for conn in $conns;
     84 		do
     85 			echo $conn
     86 		done
     87 	fi
     88 
     89 	exit 0
     90 fi
     91 
     92 if [ $dowakeup -eq 1 ];
     93 then
     94 	[ $LOGGING -eq 1 ] && \
     95 		logger -s -t "conn" "waking up"
     96 	cmdarg="-f -s"
     97 fi
     98 
     99 if [ $dosuspend -eq 1 ];
    100 then
    101 	[ $LOGGING -eq 1 ] && \
    102 		logger -s -t "conn" "suspending"
    103 	$0 -f -k $*
    104 	exit 0
    105 fi
    106 
    107 if [ $dorestart -eq 1 ];
    108 then
    109 	$0 -k $*
    110 	$0 -s $*
    111 	exit $? 
    112 fi
    113 
    114 if [ -z "$cmdarg" ];
    115 then
    116 	usage $0
    117 	exit 1
    118 fi
    119 
    120 connection=$1
    121 profile=$2
    122 if [ -z "$1" ];
    123 then
    124 	if [ $dostart -eq 1 ] && [ ! $dowakeup -eq 1 ];
    125 	then
    126 		conns=`getdefaultstate "open"`
    127 	else
    128 		conns=`getstate "open"`
    129 	fi
    130 
    131 	[ -z "$conns" ] && exit 0
    132 	[ $doforce -eq 1 ] && cmdarg="-f $cmdarg"
    133 
    134 	for conn in $conns;
    135 	do
    136 		narg0=`echo $conn | awk -F'|' '{print $1}'`
    137 		[ -z "$narg0" ] && continue;
    138 		narg1=`echo $conn | awk -F'|' '{print $2}'`
    139 
    140 		$0 $cmdarg $narg0 $nargs1
    141 		exit 0
    142 	done
    143 
    144 	exit $?
    145 fi
    146 
    147 if [ $doforce -eq 0 ];
    148 then
    149 	if [ $dostart -eq 1 ];
    150 	then
    151 		if isset "open" $connection $profile;
    152 		then
    153 			echo "Connection $connection $profile is already open."
    154 			echo "Use -f for forcing the opening."
    155 			exit 1 
    156 		fi
    157 	fi
    158 
    159 	if [ $dokill -eq 1 ];
    160 	then
    161 		if ! isset "open" $connection $profile;
    162 		then
    163 			echo "Connection $connection $profile is already" \
    164 				"closed."
    165 			echo "Use -f for forcing the closing."
    166 			exit 1
    167 		fi
    168 		delstate "open" $connection $profile
    169 	fi
    170 fi
    171 
    172 if [ $# -lt 2 ];
    173 then
    174 	shift $#
    175 else
    176 	shift 2
    177 fi
    178 
    179 [ $LOGGING -eq 1 ] && \
    180 	logger -s -t "conn" "running: $connection $cmdarg $profile $*"
    181 
    182 runconnection $connection $cmdarg $profile $*
    183 if [ $? -eq 0 ] && [ $dostart -eq 1 ] && [ ! $doforce -eq 1 ];
    184 then
    185 	addstate "open" $connection $profile
    186 fi
    187 
    188 exit $?
    189