Xen + DCOP: automatic screenshots
eBox is growing a lot lately, and updating screenshots is always a cumbersome task, so, prompted by a workmate, I decided to do something to get automatic screenshots.
The first step was to get an automatic way to install eBox from scratch to have something to get screenshots from. That was quite easy using the Xen facilities available in Debian. I only had to write a role for xen-create-image and now I can get an eBox up and running in a couple of minutes executing a simple “xen-create-image –role ebox –hostname ebox-screenshots”.
Using eBox introspection capabilities and some Perl magic, it was quite easy to autogenerate a list of the URLs that we should take screenshots from. Once we had the URL list and a running eBox all we have to do was to have a browser logging in the eBox web interface and loop through the URLs taking screenshots.
To achieve that I resorted to Ruby, Konqueror, KSnapshot and DCOP, and it took less than 40 lines of code to do it. The first problem was spawning Konqueror and KSnapshot instances and getting a DCOP reference for them, but that was easily done using Ruby’s fork, which is quiet convenient:
konqpid = Kernel.fork { exec("konqueror --geometry 1024x768+0+0") }
ksnappid = Kernel.fork { exec("ksnapshot") }
konqwin = KDE::DCOPRef.new("konqueror-#{konqpid}","konqueror-mainwindow\#1")
ksnapshot = KDE::DCOPRef.new("ksnapshot-#{ksnappid}","interface")
The second problem I faced was logging in the eBox interface, I had to enter the password and submit the form. Fortunately Konqueror’s DCOP interface offers a really nice “evalJS” function:
konqwin.openURL("https://#{ip}/ebox/")
konqhtml = KDE::DCOPRef.new("konqueror-#{konqpid}","html-widget1")
konqhtml.evalJS("document.getElementById('credential_0').value = 'ebox'")
konqhtml.evalJS("document.login.submit()")
and we’re in!
Now we only have to loop through the urls taking the screenshots. I had a little problem because ksnapshot expects you to click the left mouse button before taking the screenshot. Fortunately that could be easily fixed using xte from the xautomation package to simulate the click.
urls.each { |url|
konqwin.openURL("https://#{ip}/ebox/#{url}")
name = "#{konqwin.caption.split(/-/)[1].strip.gsub(/ /,'_')}"
ksnapshot.slotGrab
system("xte 'mousemove 100 100' 'mouseclick 1'")
ksnapshot.save("#{outpath}/#{name}.png")
}
The code I pasted lacks a bit of initialization and some sleeps I put there to synchronize the stuff, but basically that’s all. I just love Ruby, KDE and DCOP ![]()
December 27th, 2006 at 11:30 am
It really turns me on to see all the new possibilities that there are with Xen
February 29th, 2008 at 1:09 am
Wow, that’s really cool. Elegant and insanely useful.
Although I understand Xen and Ruby, I’ve never used them (although I did intentionally write a ruby script to crash the interpreter once, can’t remember how I did it, took me aaages cause I was deliberately doing it without reading a single line of ruby code first, and had to guess the names of all the functions and the syntax, getting it right by error messages and a process of elimination)
A friend and myself are playing with automation involving windowing systems as we speak. I have one running on a cron job using imagemagick’s “import” – /usr/bin/import – which captures the whole screen rather nicely, but pushes cpu usage up through the roof in the process (unlike ksnapshot, which doesn’t seem to affect cpu time at all)
What I’m really interested in is being able to do it on demand from a remote machine, without a password. php looks like and obvious answer but I can’t figure out how to do it – I don’t particularly want a long series of snapshots, I just want each new one to replace the last. It’s easier said than done (possibly unless apache is run as the current user, no way man.)
…
I will probably put a dedicated webserver/router in front of the desktop machine though, and write a script to get the image from the desktop machine, which will be allowed to serve apache content to the local network as the currently logged-in user, or something liek that.
Maybe. I dunno.
But anyway, what do you think of this?:
p://docs.kde.org/stable/en/kdegraphics/ksnapshot/dcop.html
I’ve jsut written a humbe little php script which starts ksnapshot, grabs its dcop interface, and then takes a picture of the whoel screen (no mouse click)