You are not logged in.

#1 2009-06-25 13:03:47

eldragon
Member
From: Buenos Aires
Registered: 2008-11-18
Posts: 1,029

help with a script

ive written this script to setup my gnome proxy based on netcfg's active profile, it runs from the PRE_UP and POST_DOWN entries in the profiles.

the issue is im ussing dbus to access my gnome preferences, but it only works if my user is not being logged it. is there a way to use dbus and have it work, from sudo, or from my account?

here's the script:

#!/bin/bash
PROXY_SET=$1
PROXY=$2
PORT=$3
ON_USER=$(cat /etc/passwd | grep :1000: | cut -d ':' -f 1)
#Assumes user directory is /home/$ON_USER
#export DBUS_SESSION=$(grep -v "^#" /home/$ON_USER/.dbus/session-bus/`cat /var/lib/dbus/machine-id`-0)

if sudo -u $ON_USER test -z "$DBUS_SESSION_BUS_ADDRESS" ; then
       eval `sudo -u $ON_USER dbus-launch --sh-syntax --exit-with-session`
fi

echo $ON_USER
echo $DBUS_SESSION
echo $DBUS_SESSION_BUS_ADDRESS
echo $DBUS_SESSION_BUS_WINDOWID
echo $DBUS_SESSION_BUS_PID



case "$PROXY_SET" in
    proxy-on)
        sudo -u $ON_USER "DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS gconftool-2 --type bool --set /system/http_proxy/use_http_proxy true
        sudo -u $ON_USER "DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS gconftool-2 --type bool --set /system/http_proxy/use_same_proxy true
        sudo -u $ON_USER "DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS gconftool-2 --type string --set /system/http_proxy/host "$PROXY"
        sudo -u $ON_USER "DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS gconftool-2 --type int --set /system/http_proxy/port $PORT
                sudo -u $ON_USER "DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS gconftool-2 --type string --set /system/proxy/mode "manual"
        ;;
    proxy-off)
                sudo -u $ON_USER "DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS "DBUS_SESSION_BUS_PID="$DBUS_SESSION_BUS_PID gconftool-2 --type string --set /system/proxy/mode "none"
        sudo -u $ON_USER "DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS gconftool-2 --type bool --set /system/http_proxy/use_http_proxy false
        ;;
    *)
        echo "no paso nada"
        ;;
esac

one of the ideas i got is NOT to use dbus if users | grep $ON_USER is not zero. but it seems quite hackish. is there a more elegant way to do this?


on a side note, if i run the script as myself, it does not fail. it only fails if run as root, and my user is already logged in,  (which is a problem, since netcfg runs as root)

Offline

#2 2009-06-25 13:46:08

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: help with a script

whenever i need to run a script as root but a particular command as user i always run it like this:

su -c "/some/command/to/run --with --options" user_to_run_as

worth a try, right?

Offline

#3 2009-06-25 14:48:24

eldragon
Member
From: Buenos Aires
Registered: 2008-11-18
Posts: 1,029

Re: help with a script

brisbin33 wrote:

whenever i need to run a script as root but a particular command as user i always run it like this:

su -c "/some/command/to/run --with --options" user_to_run_as

worth a try, right?

with the same principle, i did this with sudo before, but an update to gnome, makes it dependant on a dbus session. so if my user is not logged it, and init runs a script on my behalf, since there is no dbus session running for my user, the script fails. this is why i asked, i dont want to test if my user is logged in if can

Offline

#4 2009-06-30 00:58:14

eldragon
Member
From: Buenos Aires
Registered: 2008-11-18
Posts: 1,029

Re: help with a script

well, i checked the script and fixed the condition which should pickup the user's running dbus session... here it is

#!/bin/bash
PROXY_SET=$1
PROXY=$2
PORT=$3
ON_USER=$(cat /etc/passwd | grep :1000: | cut -d ':' -f 1)

#Assumes user directory is /home/$ON_USER
# Picks up the running dbus session if there is one
export $(grep -v "^#" /home/$ON_USER/.dbus/session-bus/`cat /var/lib/dbus/machine-id`-0)

# if there is no dbus session running, start one
if sudo -u $ON_USER test -z "$DBUS_SESSION_BUS_ADDRESS" ; then
       eval `sudo -u $ON_USER dbus-launch --sh-syntax --exit-with-session`
fi

# some debugging info here, feel free to comment these out
echo $ON_USER
echo $DBUS_SESSION
echo $DBUS_SESSION_BUS_ADDRESS
echo $DBUS_SESSION_BUS_WINDOWID
echo $DBUS_SESSION_BUS_PID



case "$PROXY_SET" in
    proxy-on)
        sudo -u $ON_USER "DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS gconftool-2 --type bool --set /system/http_proxy/use_http_proxy true
        sudo -u $ON_USER "DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS gconftool-2 --type bool --set /system/http_proxy/use_same_proxy true
        sudo -u $ON_USER "DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS gconftool-2 --type string --set /system/http_proxy/host "$PROXY"
        sudo -u $ON_USER "DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS gconftool-2 --type int --set /system/http_proxy/port $PORT
                sudo -u $ON_USER "DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS gconftool-2 --type string --set /system/proxy/mode "manual"
        ;;
    *)
                sudo -u $ON_USER "DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS "DBUS_SESSION_BUS_PID="$DBUS_SESSION_BUS_PID gconftool-2 --type string --set /system/proxy/mode "none"
        sudo -u $ON_USER "DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS gconftool-2 --type bool --set /system/http_proxy/use_http_proxy false
        ;;
esac

i use this script with netcfg to setup my proxy settings depending on the profile im using...

Offline

Board footer

Powered by FluxBB