Archive for the ‘ software ’ Category

Fun with GMail, Google Voice, & Python

Lately I have been getting lots of Google Voice and GMail messages, and it can be a royal pain to go through and mark them all read on the web interfaces, so I threw together a couple of script that help to solve this problem from the shell for me.

gmail_mark_as_read:

#!/usr/bin/env python

import imaplib
obj = imaplib.IMAP4_SSL('imap.gmail.com', '993')
obj.login('charlie@charliemeyer.net', 'my_password')
obj.select('Inbox')
typ ,data = obj.search(None,'UnSeen')
try:
    obj.store(data[0].replace(' ',','),'+FLAGS','\Seen')
    print "Messages marked as read"
except:
    print "No messages to mark"

voice_mark_as_read:

#!/usr/bin/env python

from googlevoice import Voice,util

voice = Voice()
voice.login('charlie@charliemeyer.net', 'my_password')

while True :
    folder = voice.search('is:unread')
    if folder.totalSize <= 0 :
        break
    util.print_(folder.totalSize)
    for message in folder.messages:
        if not message.id:
            continue
        util.print_(message)
        message.mark(1)

Now, to tie it all together, a bash script:

mark_all_as_read:

#!/bin/bash

voice_file=/tmp/gvoice.$$.out
gmail_file=/tmp/gmail.$$.out

touch $voice_file
touch $gmail_file

echo "Marking text messages read"
~/bin/voice_mark_as_read &> $voice_file &
echo "Marking gmail messages read"
~/bin/gmail_mark_as_read &> $gmail_file &

wait

echo "GVoice:"
cat $voice_file
echo "GMail:"
cat $gmail_file

rm $gmail_file $voice_file

Now, I do not claim to be the original author of the first 2 scripts here, they were taken from various stack exchange posts and what not (I lost the URLs and I have modified the scripts, so unfortunately I cannot give due credit, but I will not take it for myself). The last bash script was my little work, and it does its job perfectly. I hope this helps someone else to fix the problem that I was also facing.

Auto installation of ssh public keys

Over the past few months, I’ve been writing little shell scripts here and there that make it just a bit easier to get my day to day work done. I thought I would post one of them here in case it might be beneficial to others as well.

I work a daily job where I am constantly using ssh to access many different remote machines all across the world. Since different machines can have different passwords and the like, I prefer to use public key authentication when possible to avoid this headache. The issue is, for each new machine I access, I used to have to manually install my pubkey on it to use public key authentication in the future. Even though Ubuntu has a shell script that ships with it called ssh-copy-id which handles the work for me, I knew that I could do better. Here is what I came up with:

~/bin/ssh

#!/bin/bash -x

/usr/bin/ssh -o PreferredAuthentications=publickey -o PasswordAuthentication=no -n $@ &> /dev/null
if [ $? -ne 0 ] 
then
    ssh-copy-id $@
fi  
exec /usr/bin/ssh $@

What this script does is first check if my pubkey is installed on the remote machine. If not, it runs the ssh-copy-id script to install it. This will prompt for the password, then install my public key. After that, a normal ssh session is started which, if everything worked properly, will use pubkey authentication to log me in. Perfect! The exec was necessary so that konsole (my preferred terminal) would realize I was in a remote session and update the tab label properly. I also had to change the shell script /usr/local/bin/ssh-copy-id near the end to have it use /usr/bin/ssh rather than just “ssh” otherwise it would recursively infinitely loop.

Now in Chi-town

The server that hosts this blog has been relocated from Scranton, PA to Chicago, IL. Things might be unstable for a few days as the new configuration is finalized.