Tuesday, December 04, 2007

SSH Google Talk Bot

PLEASE: This is the most visited post in my blog, so I think people finds this micro script to be interesting, please take minute to leave a comment telling me if you would like me to further develop this script into a full blown google gtalk bot. I appreciate your time, Thanks for your visit!

Gabriel Medina

SSH Google Talk Bot

What?

Reading about how to interface to Google Talk I got the idea to build a bot to remotely control my computer from Google Talk, I needed two different things, a way to communicate easily with Google Talk and a way to run commands on my computer and get the results back.

Command Execution

The latter was easy. I had already done that in a quick and dirty way using IO.popen, but it didn't fell easy enough, luckily for me I came across the session gem, which is a fast way to run commands on a single session (this means, that you can issue a -cd- command, and expect to actually have your working directory to be changed in the next command you run) without having to open and close sessions for each command run.

Connecting to Google Talk

For the former, connecting to Google Talk I read about various libraries to interact with xmpp (the protocol behind jabber, on which Google Talk is based), but all of them implied tremendous work just to get a simple one-on-one conversation going. The most appealing was apparently xmpp4r and while searching for documentation on this, I came across xmpp4r-simple, which is really, really simple, specially since it detects you're connecting to Google Talk and configures itself properly, Automatically!

You Need

To run the following code you'll need 2 different Google Talk accounts, one for the bot's use, and your own account, just run it on a console (DOS Prompt on Windows), make sure you install both gems before running it:

sudo gem install session --include-dependencies
sudo gem install xmpp4r --include-dependencies
sudo gem install xmpp4r-simple --include-dependencies

The Actual Code

And the save the following code as shellbot.rb or anything else you want:

#!/usr/bin/env ruby
require 'rubygems'
require 'xmpp4r-simple'
require 'session'

strBOTUserName = '[botusername]'
strBOTPassword = '[botpassword]'
strHumanUserName = '[humanusername]'

puts "Connecting ..."
jabber = Jabber::Simple.new(strBOTUserName+'@gmail.com', strBOTPassword)
puts "Connected."
jabber.deliver(strHumanUserName+"@gmail.com", "Online...")

bash = Session::Bash.new
while (true) do
       jabber.received_messages do |msg|
               puts "="*80
               exit if msg.body.downcase.strip == 'exit'
               puts msg.body
               puts "-"*80
               stdout, stderr = bash.execute msg.body
               jabber.deliver(strHumanUserName+"@gmail.com", stdout)
               puts stdout
               jabber.deliver(strHumanUserName+"@gmail.com", 'ERROR: '+stderr) if stderr.strip.length > 0 if stderr
               puts 'ERROR: '+stderr if stderr.strip.length > 0 if stderr
               puts "="*80
       end
       sleep 2
       stdout, stderr = bash.execute 'echo'
end

Of course, substitute [botusername], [humanusername] and [yourpassword], with your corresponding real values

How to run it

Then finally just run it from your console:

ruby shellbot.rb

Or, if you're on linux, chmod-it:

chmod +x shellbot.rb

Then run it:

./shellbot.rb

SECURITY! SECURITY!

THIS IS NOT SECURE, I repeat NOT SECURE. This code is just for illustrative purposes, and is not protected in anyway, it is highly insecure, and should not be considered ready for production or as a finished product, although it could be secured in some ways I doubt this would ever be safe for real world public internet use. Consider yourself WARNED.

Thanks for reading!

I hope you find this interesting...

Good day!