You are not logged in.
Pages: 1
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
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
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
Pages: 1