LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Xscreensaver VPN Network Disconnect/Reconnect Script (https://www.linuxquestions.org/questions/linux-newbie-8/xscreensaver-vpn-network-disconnect-reconnect-script-4175506696/)

abourke 06-01-2014 04:09 PM

Xscreensaver VPN Network Disconnect/Reconnect Script
 
Hi, having trouble with a script I made to disconnect/reconnect my network connection and vpn. Everything works except for the reconnection of my vpn using nmcli con up.

It behaves differently depending how it is invoked. If I use the terminal to start the script it works. But if I start the script as an /etc/xdg/autostart desktop entry it fails on reconnection of the vpn. And there is no output for me to determine what is wrong.

The command alone runs fine in the terminal. (nmcli con up blah blah) But fails as part of the script.
I guess it is either a permission problem or the preceding sleep statement doesnt work as intended.

Cany somone help me fix this? Im very close!

Code:

#!/bin/bash

process() {
while read input; do
  case "$input" in
    UNBLANK*)        notifySendUp.sh && echo NET_UP && date && sudo dbus-send --system --dest=org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager.Sleep boolean:false && sleep 10 && sudo -u aubrey nmcli con up uuid 5d7a4c02-2934-44e9-8f58-5d5010ec36c7;;
    LOCK*)      nmcli con down uuid 5d7a4c02-2934-44e9-8f58-5d5010ec36c7 && sleep 4 && notifySendDown.sh && echo NET_DOWN && date && sudo dbus-send --system --dest=org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager.Sleep boolean:true ;;
  esac
done
}

/usr/bin/xscreensaver-command -watch | process


jpollard 06-03-2014 06:58 AM

Might have a problem with the "echo NET_DOWN" as an autostart doesn't have anywhere to send it...

nit: It would make it easier to read if those long lines were broken up. No need for the && syntax either - just put them on a new line with suitable indentation. It might also make it easier to debug.

abourke 06-07-2014 03:35 PM

Progress
 
Okay, I did this and now the nmcli commands work!

Code:

#!/bin/bash

process() {
while read input; do
  case "$input" in
    UNBLANK*)       
                notifySendUp.sh
                sudo dbus-send --system --dest=org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager.Sleep boolean:false
                sleep 5s
                nmcli con up uuid 5d7a4c02-2934-44e9-8f58-5d5010ec36c7
                ;;
    LOCK*)             
                notifySendDown.sh
                nmcli con down uuid 5d7a4c02-2934-44e9-8f58-5d5010ec36c7
                sleep 4s
                sudo dbus-send --system --dest=org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager.Sleep boolean:true
                ;;
  esac
done
}

/usr/bin/xscreensaver-command -watch | process

And again it fully works when launched from the terminal.
However, if I autostart it by adding this line to the end of my .bash_profile, it all works except for the dbus commands
Code:

/usr/local/bin/lockedNet.sh &
I also have the following in my sudeors:
Code:

aubrey ALL= NOPASSWD: /usr/bin/dbus-send
root ALL= NOPASSWD: /usr/bin/dbus-send
aubrey ALL= NOPASSWD: /usr/bin/nmcli
root ALL= NOPASSWD: /usr/bin/nmcli
aubrey ALL= NOPASSWD: /usr/local/bin/lockedNet.sh
root ALL= NOPASSWD: /usr/local/bin/lockedNet.sh

What am I doing wrong here? Appreciate any help.
Regards
Aubrey.

abourke 06-07-2014 04:05 PM

more progress...
 
HI,

I tried using another command instead of the dbus commands
Code:

#!/bin/bash

process() {
while read input; do
  case "$input" in
    UNBLANK*)    notifySendUp.sh
                #sudo dbus-send --system --dest=org.freedesktop.NetworkManager$
                sudo ifconfig p32p1 up
                sleep 5s
                nmcli con up uuid 5d7a4c02-2934-44e9-8f58-5d5010ec36c7        $
                ;;
    LOCK*)        notifySendDown.sh
                nmcli con down uuid 5d7a4c02-2934-44e9-8f58-5d5010ec36c7
                sleep 4s
                #sudo dbus-send --system --dest=org.freedesktop.NetworkManager$
                sudo ifconfig p32p1 down
                ;;
  esac
done

Here I'm using sudo ifconfig interface up/down

Again it doesnt work from autostart (.bash_profile or gnome-session-properties) but it does from the terminal!!

I guess I am close here if anyone knows??
Regards
Aubrey.

jpollard 06-08-2014 02:29 AM

sudo requires a terminal for a password check... There is no terminal from autostart.

abourke 06-08-2014 08:56 AM

Solved
 
Yes, thats it. I had to use
Code:

!requiretty
in my sudeors file.
So Im thrilled now my desktop is a clam-shell now! It locks up the network when idle. Thanks!
:-)

Regards
Aubrey.


All times are GMT -5. The time now is 04:16 AM.