Remote Commands over SSH that require DBUS
So, I started a simple script that randomly selects a wallpaper from a directory and sets it as the background on my Fedora 17 laptop. That part was simple enough, and it turned out that a coworker had already written such a script in perl.
#!/usr/bin/perl -w
use strict;
use warnings;
my $searchPath = '~/Pictures/Wallpapers/'; # Set to the directory you want to have searched for p
my $switchTime = 300; # Edit to the number of seconds between photo switches
my @photos = `find $searchPath -type f | grep [pP][nN][gG]`;
chomp(@photos);
my $photo;
while(1)
{
$photo = $photos[rand($#photos)];
`gsettings set org.gnome.desktop.background picture-uri "file:///$photo"`;
sleep($switchTime);
}
The next part was a bit more challenging. I wanted to find a way to also set the same wallpaper via ssh on my second laptop to keep them in sync. First weapon was perl....I failed miserably at trying to get Net::SSH::Perl to work...so I gave up and moved to what I know. Bash.
path="path/to/wallpapers"
time_="300"
#-------------------------------------------------------------------------------
# Array of images
#-------------------------------------------------------------------------------
N=0
for i in $(ls $path | grep .png | sort -R) ; do
wallpaper[$N]="$i"
let "N= $N + 1"
done
#-------------------------------------------------------------------------------
# while loop
#-------------------------------------------------------------------------------
for i in "${wallpaper[@]}"
do
gsettings set org.gnome.desktop.background picture-options 'stretched'
ssh user@ipAddress "gsettings set org.gnome.desktop.background picture-options 'stretched' && exit"
ssh user@ipAddress "gsettings set org.gnome.desktop.background picture-uri 'file://'$path'/'$i'' && exit"
sleep .3
gsettings set org.gnome.desktop.background picture-uri 'file://'$path'/'$i''
sleep $time_
done
This would connect and attempt to change the setting, but would fail and complain about some DBUS nonsense. After some target googling about changing gconf settings over ssh, I broadended my search and included the term DBUS. Jackpot!!
# This goes in the .bashrc of the remote machine that you wish to change settings on
if [[ -n $SSH_CLIENT ]]
then
export DBUS_SESSION_BUS_ADDRESS=`cat /proc/$(/usr/sbin/pidof i3)/environ | tr '\0' '\n' | grep DBUS_SESSION_BUS_ADDRESS | cut -d '=' -f2-`
fi
Putting it all together, I updated the .bashrc file on my client (second laptop) with the above code and added the the wallpaper script on the server (first laptop) to autostart. (gnome-session-properties)
Now my wallpapers are in sync!!