Remote Clipboard

Posted on July 3, 2008. Filed under: Fedora, Software | Tags: , , , , , , , , |

When I was using my GNOME desktop from inside a guest running on top of KVM, suddenly an idea struck me, about a couple of hours ago – “To allow copy and paste between the guest and the host”. It would be a very valuable feature to have, I thought.

Deciding to hack a quick solution, I came up with the following in around two hours. It works only one way for the moment. The guest(virtual machine) is the server and the host is the client i.e, you will only be able to copy from the host and paste it into the guest. And it works only with text now.

In the guest, ( please change the IP address appropriately )


#!/usr/bin/python
#Run this in the guest
import socket,gtk

clip = gtk.Clipboard(display=gtk.gdk.display_get_default(), selection="CLIPBOARD" )

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(("192.168.1.2", 7070))
serversocket.listen(1)

while (True):
	(clientsocket, address) = serversocket.accept()
	clip.set_text(clientsocket.recv(100));
	clip.store()
	clientsocket.close();

serversocket.close()

And in the host,


#!/usr/bin/python
import gtk,time,socket

clip = gtk.Clipboard(display=gtk.gdk.display_get_default(), selection="CLIPBOARD" )
while (True):
	prev = clip.wait_for_text()
	while (prev == (clip.wait_for_text())):
		time.sleep(1)

	prev = clip.wait_for_text()
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.connect(("192.168.1.2", 7070))
	s.send(prev)
	s.close()
s.close()

With this program, I’ve copy-pasted across two machines in a network! Very powerful isn’t it ? Its really surprising too see what around 35 lines of python can do. Though I had studied a bit of python a few years ago, this one small hack has made me fall into love with python again. Its an awesome language. Extremely powerful.

This program uses something called pygtk, which are python extensions for GTK. You can write GTK programs using python.

I wish to develop this further to the extent of copying files, images and other rich content across machine boundaries. Though it probably will be criticised for being bad from a security point of view, it will definitely be useful for users of KVM, XEN and other users of virtual machine monitors.

Happy hacking!

Make a Comment

Make A Comment: ( 7 so far )

blockquote and a tags work here.

7 Responses to “Remote Clipboard”

RSS Feed for Brain Dump Comments RSS Feed

how did u get the code formatting into the blog post?

@Sanaulla

[sourcode language='python']…[/sourcecode]

Thanks. can i use the same style for adding java code? replacing python with Java

@Sanaulla

Yes, ofcourse you can!

Thanks!!! It seems to be working. But need to work with the indenting stuff.

@Sanaulla

Try making your code preformatted and then enclose it within the [sourccode] markup. It should work. Even I had faced the indentation problem.

nice. very nifty feature.

in fact, the other day i was using VNC and copy-paste across computers didn’t work. maybe a badly configured client/server. but damn useful!


Where's The Comment Form?

  • Feed

  • Ohloh

  • Bookmarks

  • Categories

  • Visitors


  • Hits

    • 16,934 hits

Liked it here?
Why not try sites on the blogroll...