You are not logged in.

#1 2015-03-31 06:50:31

UncleSlug
Member
Registered: 2013-10-14
Posts: 13

[Solved] Bash weirdness

I'm trying to write a script to start and stop a vpn connection but i'm running into some weird results when it runs. The connection itself works fine and it stops and starts as it should, but things like text that should be echoed aren't. Openvpn is started as a daemon so it doesn't hang/block and returns to the command prompt. notify-send doesn't like how it's formatted with $msg ("Invalid number of options.").

I borrowed the notify stuff from the script that comes with aarchup.

#!/usr/bin/bash

script_name="openvpn-us.sh"

pid_location="/var/run/openvpn"
pid_name="openvpn-us.pid"

config_location="/etc/openvpn"
config_name="vpnbook-us1-tcp443.ovpn"

exe="/usr/bin/openvpn"
exe_args="--daemon --writepid $pid_location/$pid_name --config $config_location/$config_name"

send_notify() {
	dbus=$(grep -z DBUS_SESSION_BUS_ADDRESS $1 | sed 's/DBUS_SESSION_BUS_ADDRESS=//')
	user=$(grep -m 1 -z USER $1 | sed 's/USER=//')
	dply=$(grep -z DISPLAY $1 | sed 's/DISPLAY=//')
	msg=${@:2}
	echo "send_notify start $msg"
	sudo -u $user sh -c "DBUS_SESSION_BUS_ADDRESS=\"$dbus\" DISPLAY=\"$dply\" /usr/bin/notify-send $msg"
	echo "send_notify finish"
}

notify_wrapper() {
	echo "notify_wrapper"
	called=false
	for p in $(pgrep gconf-helper); do
		send_notify /proc/$p/environ $@
		called=true
	done
	if [ $called = false ]; then
		for p in $(pgrep dconf-service); do
			send_notify /proc/$p/environ $@
			called=true
		done
	fi
	if [ $called = false ]; then
		send_notify /proc/self/environ $@
	fi
}


# Let the fun commence
start() {
	echo "Starting OpenVPN Tunnel"
	
	if [ -e "$pid_location/$pid_name" ]; then
		echo "error daemon appears to be already running"
		exit 1
	fi

	#touch $pid_location/$pid_name
	exec $exe $exe_args

	echo "start notify"
	notify_wrapper "OpenVPN Tunnel $config_name enabled"
	echo "end of start"
}

stop() {
	echo "Stopping OpenVPN Tunnel"

	if [ ! -e "$pid_location/$pid_name" ]; then
		echo "error no daemon appears to be already"
		exit 1
	fi

	kill -s SIGTERM $(cat $pid_location/$pid_name)
	rm "$pid_location/$pid_name"
	echo "stop notify"
	notify_wrapper "OpenVPN Tunnel $config_name disabled"
	echo "end of stop"
}

# Might as well go hard [reset]
restart() {
	echo "Restarting OpenVPN Tunnel"

	if [ ! -e "$pid_location/$pid_name" ]; then
		echo "error daemon appears to be already running"
		exit 1
	fi

	kill -s SIGHUP $(cat $pid_location/$pid_name)
	notify_wrapper "OpenVPN Tunnel $config_name restarted"
}

# Make sure only one instance of this script is running at a time
if [ $(pidof -x "$script_name"| wc -w) -gt 2 ]; then
	echo "error this script is already running" 
	exit 1
fi

if [ ! -d "$pid_location" ]; then
	mkdir -p "$pid_location"
fi

case "$1" in 
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	*)
		exit 1
esac

echo "Success"

Running it looks like:

[person@machine ~]$ sudo /etc/openvpn/openvpn-us.sh start
Starting OpenVPN Tunnel
[person@machine ~]$ sudo /etc/openvpn/openvpn-us.sh stop
Stopping OpenVPN Tunnel
stop notify
notify_wrapper
send_notify start OpenVPN Tunnel vpnbook-us1-tcp443.ovpn disabled
Invalid number of options.
send_notify finish
end of stop
Success

Yes that is some dodgey (and horrendously slow) free vpn but it's only for testing lol. Changing $@ to $* didnt seem to make a difference. I've probably done something dumb but i can't work out what it is.

Last edited by UncleSlug (2015-04-01 04:06:13)

Offline

#2 2015-03-31 07:24:09

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [Solved] Bash weirdness


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2015-04-01 04:05:57

UncleSlug
Member
Registered: 2013-10-14
Posts: 13

Re: [Solved] Bash weirdness

Figured it out.

Changed

send_notify /proc/$p/environ $@

to

send_notify /proc/$p/environ "$*"

because $* and $@ are special and must always be quoted. Used $* so i can use $2 instead of $msg in the next function.

Changed

sudo -u $user sh -c "DBUS_SESSION_BUS_ADDRESS=\"$dbus\" DISPLAY=\"$dply\" /usr/bin/notify-send $msg"

to

sudo -u $user sh -c "DBUS_SESSION_BUS_ADDRESS=\"$dbus\" DISPLAY=\"$dply\" /usr/bin/notify-send \"$2\""

because it turns out notify-send needs quotes around the string on the commandline.

Removed exec because i was completely using it wrong and cutting the script off. It was this giving me the "weirdness" of missing output.

Now i guess i just have to find a nice way of confirming the vpn is connected (probably using --status and the file it makes).

Last edited by UncleSlug (2015-04-01 04:29:44)

Offline

Board footer

Powered by FluxBB