Saturday, March 6, 2010

Method To Get Rid Of Ulcers

How to control your PC from Linux Twitter

wore long mulling over the issue of minimum control home server from within. First I thought of a series of thong through GMail, but then I thought it
using Twitter. All this came to me for several reasons: there are times that the router hangs and I do not hear, but the funny thing is that if you have access to the outside lately I
skips the automatic home too often and of course if I am out for I spoil what is in the fridge and my tank is also suffering a lot.

I looked to see if there was any program that would allow me more than anything to not reinvent the wheel (though this is usually fun), and of course I found this . The problem is that it works only on Windows (you have to be won in our times), and found nothing seems to work on my Ubuntu. I found this

python API, so get to work, as well python can not be very difficult.

First of all it is very important the Twitter account you use not be public because otherwise everyone will be able to control the PC. Right now I command you I've added are a table, one to get the IP (public and private) to manage the router number.

Add a command is as easy as defining a function called comando_command , and within this function call to the command you want.

Then add this script to cron to run it every X minutes and this, controlling the PC from outside the house with the tip command 'ping rc', 'ip rc', etc ...

Now we can send a private message to the account you'll use the form ' rc command' and await the outcome.

here is the script:

      
import twitter import os import socket

import urllib2
fcntl import struct import

cStringIO
import tokenize import string import



api
global

user = 'usuario_twitter'
passwd = 'password_twitter'
tmp_file = '/ tmp / twmylin.tmp'

old_id = 0 def

set_old_id (id) :
f = open (tmp_file, 'w')
f.write(str(id))

def get_old_id():
try:
f = open(tmp_file, 'r')
id = int(f.readline())
except Exception:
id = 0
return id



def get_interface_ip(ifname):

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

return socket.inet_ntoa(fcntl.ioctl(

s.fileno(),

0x8915, # SIOCGIFADDR

struct.pack('256s', ifname[:15])

)[20:24])

def get_lan_ip():

ip = socket.gethostbyname(socket.gethostname())

if ip.startswith("127.") and os.name != "nt":

interfaces = ["eth0","eth1","eth2","wlan0","wlan1","wifi0","ath0","ath1","ppp0"]

for ifname in interfaces:

try:

ip = get_interface_ip(ifname)

break;

except IOError:

pass

return ip
def send_twitter(m):
msg='#twmylin '+ m
status = api.PostUpdate( msg)

def ping_command(tokens):
send_twitter('Estoy vivo')

def ip_command(tokens):
pub_ip='not found'
try:
pub_ip = urllib2.urlopen("http://whatismyip.com/automation/n09230945.asp").read()
except Exception:
pass

send_twitter('int:' + get_lan_ip() + ', pub:' + pub_ip)


def top_command(tokens):
s = 'top: error'
try:
f = os.popen("top -b -n1")
for i in range(0, 5):
send_twitter (f.readline())
except Exception:
send_twitter(s)


def router_check_command(tokens):
s = 'router_check: error'
try:
f = os.popen("/data/bin/router_check.sh")
f = os.popen("tail -n2 /data/bin/router_stat.txt")
s=''
for i in range(0, 2):
s+= f.readline()
except Exception:
pass

send_twitter(s)

def router_sincro_command(tokens):
s = 'router_connect: error'
try:
s = "try router_sincro"
send_twitter(s)
f = os.popen("/data/bin/router_sincro.sh")
except Exception:
pass

def router_reboot_command(tokens):
s = 'router_reboot: error'
try:
s = "try router_reboot"
send_twitter(s)
f = os.popen("/data/bin/router_reboot.sh")
except Exception:
pass

# Main
api = twitter.Api(username=usuario, password=passwd)

old_id=get_old_id()

status=api.GetDirectMessages()


for s in status:
if s.id > old_id:
old_id=s.id
set_old_id(old_id)
tokens = list(tokenize.generate_tokens(cStringIO.StringIO(s.text).readline))
if tokens[0][0] ==1 and tokens[0][1] == 'rc':
globals()[tokens[1][1]+'_command'](tokens)


0 comments:

Post a Comment