One thing that was frustrating me for a while was being unable to remotely control my running desktop applications over SSH. This was something I had got accustomed to with DCOP, and the advent of DBUS appeared to take the functionality away from me.
The script below allows you to attach to an existing DBUS session when connecting to a machine remotely via SSH. This gives the ability to control DBUS-enabled applications remotely using utilities like qdbus, similar to how DCOP used to work.
Instructions for use
1) Login to the target machine locally first. Start some applications that you want to remote control (e.g. Rhythmbox).
2) Run the script below onto the target machine, and run it from a remote SSH session by typing "source ./name_of_script.sh". Doing this allows the script to export the variable correctly.
Code:
#!/bin/bash
# Remember to run this script using the command "source ./filename.sh"
# Search these processes for the session variable
# (they are run as the current user and have the DBUS session variable set)
compatiblePrograms=( nautilus kdeinit kded4 pulseaudio trackerd )
# Attempt to get a program pid
for index in ${compatiblePrograms[@]}; do
PID=$(pidof -s ${index})
if [[ "${PID}" != "" ]]; then
break
fi
done
if [[ "${PID}" == "" ]]; then
echo "Could not detect active login session"
return 1
fi
QUERY_ENVIRON="$(tr '\0' '\n' < /proc/${PID}/environ | grep "DBUS_SESSION_BUS_ADDRESS" | cut -d "=" -f 2-)"
if [[ "${QUERY_ENVIRON}" != "" ]]; then
export DBUS_SESSION_BUS_ADDRESS="${QUERY_ENVIRON}"
echo "Connected to session:"
echo "DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS}"
else
echo "Could not find dbus session ID in user environment."
return 1
fi
return 0
3) Once the script has been run successfully, you will be able to remotely control your applications using qdbus:
Code:
qdbus org.gnome.Rhythmbox /org/gnome/Rhythmbox/Player playPause false
Consult the "qdbus" man page for information on how to use it.
If you want to add this to your .bashrc, see this post.
Caveat
This script was written to detect the first session it finds, if there are multiple logins on the target machine it may break the script or connect to the wrong session. If you have some suggestions for this, please let me know.
Constructive criticism/feedback is welcome I've also uploaded the script to the "League of Scripters" repository, feel free to commit fixes directly if you would prefer.
Bookmarks