cisco twitter TCL -> API
This TCL Script create new twitter messages on www.twitter.com by using the twitter API.
 
ciscotwitter.tcl
########################################################################################
#
# ciscotwitter.tcl
# 
# twitters a status update from a Cisco Router to the Twitter Page 
# 
#
########################################################################################
#
# (c) 2009 by packetlevel.ch 
# 
# version 0.5
# date 06.09.2009
#
########################################################################################
# 
# ios installation:
#
#     download the file ciscotwitter.tcl into flash:ciscotwitter.tcl
#     configure a alias:  alias exec ctw tclsh flash:ciscotwitter.tcl     
#     or over tftp
#     configure a alias:  alias exec ctw tclsh tftp://IP_of_TFTP_SERVER/tcl/ciscotwitter.tcl     
#
#     execute with: ctw Message 
#                   ctw "Message with space"
#
########################################################################################
# 
# only for some demonstrations and proof of concept.
# see www.twitter.com for limits and terms of use from twitter.com and the API's
#
########################################################################################
#
# insert your twitter name and password in the main section for correct work !!!
#
########################################################################################
#
# base64
# ------
#
namespace eval base64 {
  set charset "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  
# ----------------------------------------
# encode the given text
proc encode {text} {
  set encoded ""
  set y 0
  for {set i 0} {$i < [string length $text] } {incr i} {
    binary scan [string index $text $i] c x
    if { $x < 0 } {
      set x [expr $x + 256 ]
    }
    set y [expr ( $y << 8 ) + $x]
    if { [expr $i % 3 ] == 2}  {
      append  encoded [string index $base64::charset [expr ( $y & 0xfc0000 ) >> 18 ]]
      append  encoded [string index $base64::charset [expr ( $y & 0x03f000 ) >> 12 ]]
      append  encoded [string index $base64::charset [expr ( $y & 0x000fc0 ) >> 6 ]]
      append  encoded [string index $base64::charset [expr ( $y & 0x00003f ) ]]
      set y 0
    }
  }
  if { [expr $i % 3 ] == 1 } {
    set y [ expr $y << 4 ]
    append encoded [string index $base64::charset [ expr ( $y & 0x000fc0 ) >> 6]]
    append encoded [string index $base64::charset [ expr ( $y & 0x00003f ) ]]
    append encoded "=="
  }
  if { [expr $i % 3 ] == 2 } {
    set y [ expr $y << 2 ] 
    append  encoded [string index $base64::charset [expr ( $y & 0x03f000 ) >> 12 ]]
    append  encoded [string index $base64::charset [expr ( $y & 0x000fc0 ) >> 6 ]]
    append  encoded [string index $base64::charset [expr ( $y & 0x00003f ) ]]
    append  encoded "="
  }
  return $encoded
 }
}

########################################################################################
#
# encode_userpwd
# --------------
# (set a : between usernbame and password)
#
proc encode_userpwd { username password } {
  set text "$username:$password"
  return [base64::encode $text]
}

########################################################################################
#
# GetBase64String
# ---------------
# returns a base 64 encoding of username and password
# for basic http authentication.
#
proc GetBase64String { username password } {
  return [encode_userpwd $username $password]
}

########################################################################################
#
# strlen
# ------
#
proc strlen string {
    regsub -all . $string +1 string
    expr 0$string
}

########################################################################################
#
# twittermsg
# ----------
# server auth text length
#
proc twittermsg { tw_server tw_auth tw_msg tw_len } {
#
# add 7 to tw:len (size of "status="
#
set tw_totlen [expr $tw_len+7]
#
# open socket and send
#
set sh [socket $tw_server 80]
puts -nonewline $sh "POST /statuses/update.xml HTTP/1.1\n"
puts -nonewline $sh "Authorization: Basic "
puts -nonewline $sh $tw_auth
puts -nonewline $sh "\n"
puts -nonewline $sh "User-Agent: ciscotwitter.tcl 0.3\n"
puts -nonewline $sh "Host: twitter.com\n"
puts -nonewline $sh "Accept: */*\n"
puts -nonewline $sh "Content-Length: "
puts -nonewline $sh $tw_totlen
puts -nonewline $sh "\n"
puts -nonewline $sh "Content-Type: application/x-www-form-urlencoded\n"
puts -nonewline $sh "\n"
puts -nonewline $sh "status="
puts -nonewline $sh $tw_msg
flush $sh
close $sh
return
}

########################################################################################
#
# main
# ----
# 
# user twitter API for send Status Updates 
# 
#
set twitterserver "www.twitter.com"
# 
# set here your twitter-username and twitter-password
#
set username "my-twitter-user-name"
set password "my-twitter-password"
#
# create basicauth string for login
#
set basicauth [GetBase64String $username $password]
#
# 
#
set twittertext [lindex $argv 0]
set twitterlen [strlen $twittertext]
#
# send message , if Message have the right size (1 - 140)
#
if { $twitterlen > 0 && $twitterlen < 141 } then {
    twittermsg $twitterserver $basicauth $twittertext $twitterlen
} else { 
    puts "Message to short or to long (1-140)" 
}
#
# end
#

Download Script here
ciscotwitter.tcl

Cisco Twitter
Cisco is presenting his own twitter tcl script (Nov 2009)

tweet-policy.tcl (local)

Install of the script:

Prepare IP + login + password (on the linux box)
		      
    $ dig +short twitter.com
    128.121.146.100
    $ echo twitterlogin:twitterpassword | base64 -
    dHdpdHRlcmxvZ2luOnR3aXR0ZXJwYXNzd29yZAo=

Cisco Device:

    evil-router# copy tftp://192.168.0.1/tweet-policy.tcl flash:/tweet-policy.tcl
    evil-router(config)# event manager directory user policy "flash:/"
    evil-router(config)# event manager policy tweet-policy.tcl type user


    evil-router(config)# event manager environment _tweet_ip 128.121.146.100
    evil-router(config)# event manager environment _tweet_b64 dHdpdHRlcmxvZ2luOnR3aXR0ZXJwYXNzd29yZAo=

    evilrouter# event manager run tweet-policy.tcl

    evil-router(config)#event manager applet tweetme
    evil-router(config-applet)# event timer watchdog time 300                   
    evil-router(config-applet)# action 1 policy tweet-policy.tcl	

Links
www.twitter.com
http://apiwiki.twitter.com/
http://www.twitter.com/myciscorouter / My Cisco Router Test

(c) 2009 by packetlevel.ch / last update: 29.11.2009