Skip to content

Twitter as an admin tool 2

January 9, 2011

When twitter removed the simple username password authentication options for applications the simple use of curl to post messages stopped working.

However following these simple instructions I wrote simple command line application to post using python:

#!/usr/bin/python
import sys
import os
import getopt
import errno
import webbrowser
sys.path.append(‘/usr/local/lib/python/tweepy’)
import tweepy

def read_consumer():
f = open(‘/etc/opt/local/tweet.key’).readlines()
for l in f:
sl=l.split(‘=’)
if sl[0].strip() == ‘CONSUMER_KEY’:
consumer_key=sl[1].strip()
elif sl[0].strip() == ‘CONSUMER_SECRET’:
consumer_secret=sl[1].strip()

return (consumer_key, consumer_secret)

def read_access():
f = open(os.environ[‘HOME’] + ‘/.tpw’).readlines()
for l in f:
sl=l.split(‘=’)
if sl[0].strip() == ‘ACCESS_KEY’:
access_key=sl[1].strip().strip(“‘”)
elif sl[0].strip() == ‘ACCESS_SECRET’:
access_secret=sl[1].strip().strip(“‘”)

return (access_key, access_secret)

def register(user, CONSUMER_KEY, CONSUMER_SECRET):
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth_url = auth.get_authorization_url()
print ‘Please authorize: ‘ + auth_url
try:
webbrowser.open(auth_url)
except webbrowser.Error:
pass
verifier = raw_input(‘PIN: ‘).strip()
auth.get_access_token(verifier)
os.umask(066)
f=open(os.path.expanduser(‘~/.tpw’), ‘w’)
print >> f, “ACCESS_KEY = ‘%s'” % auth.access_token.key
print >> f, “ACCESS_SECRET = ‘%s'” % auth.access_token.secret

if __name__ == ‘__main__’:
try:
(CONSUMER_KEY, CONSUMER_SECRET) = read_consumer()
except IOError, (err, strerr):
if err == errno.EACCES:
pfexec=’/usr/bin/pfexec’
sys.argv.insert(0, pfexec)
os.execv(sys.argv[0], sys.argv)
raise
os.setuid(os.getuid())
try:
optlist, args = getopt.getopt(sys.argv[1:], ‘R:’)
except getopt.GetoptError, err:
print >> sys.stderr, str(err)
sys.exit(2)

for o, a in optlist:
if o == ‘-R’:
sys.exit(register(a, CONSUMER_KEY, CONSUMER_SECRET))

if len(args) == 0:
print “Usage: tweet.py ”
sys.exit()
message = ‘ ‘.join(args).strip()[0:139]
(ACCESS_KEY, ACCESS_SECRET) = read_access()

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
# print ‘Tweeting “%s”‘ % message
api.update_status(message)


By letting this run with an effective uid of the role “twitter” so that the file /etc/opt/local/tweet.key can contain the the consumer keys while not revealing them to the user:

root@pearson:/root# ls -l /etc/opt/local/tweet.key
-r——– 1 twitter root 95 Jan 2 18:06 /etc/opt/local/tweet.key
root@pearson:/root# cat /etc/opt/local/tweet.key
CONSUMER_KEY=XXXXXXXX
CONSUMER_SECRET=YYYYYYYYY
root@pearson:/root# egrep tweet /etc/security/exec_attr
Basic Solaris User:solaris:cmd:::/tank/fs/local/bin/tweet.py:euid=150012
root@pearson:/root#

Clearly I have removed the actual key and secret here


By then using @chrisgerhard in each message any admin message does not get lost in my twitter stream

: pearson FSS 31 $; tweet.py @chrisgerhard test 4 blog
: pearson FSS 32 $;

Results in this message in twitter: https://twitter.com/#!/syslogathome/status/24128233037369345

Advertisements
2 Comments
  1. James Legg permalink

    That’s pretty nifty. I use http://code.google.com/p/python-twitter/ for my little twitter driven tools as it does a load of the hard Oauth stuff for you and exposes much of the Twitter API in a quick way. It’s amazing what you can get done in a very short amount of time with it.

Trackbacks & Pingbacks

  1. Halvard Halvorsen’s tumblelog » Daily Digest for January 10th

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: