Tag Archive for 'bash'

Quick directory switching

At work, I do a lot of directory traversal: going from one location to another within the whole (global) filesystem structure. So, to conveniently go back to a previous directory, I use pushd and popd, aside from the usual cd:

$ pwd
/home/iandexter
$ pushd /etc/sysconfig
$ pwd
/etc/sysconfig
$ popd
$ pwd
/home/iandexter

You can even echo $DIRSTACK to list the current directories in the stack, and push multiple directories.

Twitter updates, via CLI

TwitterI’ve just started using Twitter, an up-and-coming web service that posts user status on the web, in IM and in SMS. The service also exposes its API, so it’s possible to build apps using it.

I found a nifty way of updating my Twitter status through the command line:

curl -u username:password -d status='status_goes_here' -s http://twitter.com/statuses/update.xml > /dev/null 2>&1

FizzBuzz

Hmmm

In Bash, I would have done:

$ for i in `seq 100` ; do if [ `expr $i % 15` -eq 0 ] ; then echo FizzBuzz; elif [ `expr $i % 3` -eq 0 ]; then echo Fizz; elif [ `expr $i % 5` -eq 0 ]; then echo Buzz; else echo $i; fi; done

Then again, it took me about 10 minutes to do that. (I couldn’t get the ternary operator to work, somehow.) In C: about five minutes (rusty — I last used C way back in college {ages ago!}).

In a recent phone interview, I was asked to programmatically (in shell) rename a set of files. I blundered for about two minutes, and gave up in the end, saying I could probably do that by experimentation. I could fairly say I passed that interview. The point? Answering “FizzBuzz” questions does not reflect real-world situations — it’s how you approached the problem, even if you didn’t get the answer, that matters.

If I were an interviewer, I’d concentrate on the steps rather than the solution.

SMTP testing automation

I know there are scripts out there that does full-blown SMTP testing. I’ve used smtp-sink and smtp-source in Postfix for this purpose:

$ smtp-source -s 100 -m 100 -f sender@domain.com -t \\
recipient@domain.com server.address:25

So this was what I recommended to a friend, a Unix engineer in Singapore. But she had a different requirement: she has to establish an SMTP connection through telnet to a remote server, and send a template mail to thousands of recipients. The telnet session goes something like:

[user@host ~]$ telnet remote.host 25
Trying 123.456.789.10...
Connected to remotehost.remotedomain (123.456.789.10).
Escape character is '^]'.
220 mail.remote.host ESMTP Postfix
EHLO some.domain
250-mail.remote.host
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250 8BITMIME
MAIL FROM:sender@this.domain
250 Ok
RCPT TO:recipient@another.domain
250 Ok
DATA
354 End data with >CR<>LF<.>CR<>LF<
Subject: This is a test
From: Sender

This is only a test.
.
250 Ok: queued as D81D7FA927
QUIT
221 Bye
Connection closed by foreign host.
[user@host ~]$

With thousands of recipients (no, her employer is not a spammer), this can be quite tedious. I thought of using an alias file at first, but the requirement was SMTP through telnet. So I told her to do the following:

  1. Create a recipients file, /tmp/recipients:
    recipient1@some.domain
    recipient2@another.domain
    ...
    recipientX@yet.another.domain
    
  2. Create a message file, tmp/message:
    Subject: This is a test
    From: Sender
    
    This is only a test.
    
  3. Create a script, smtp-telnet.sh:
    #!/bin/bash
    
    SENDER=sender@this.domain
    
    (echo "HELO some.domain";
    sleep 1;
    echo "MAIL FROM:" $SENDER;
    sleep 1;
    for i in `cat /tmp/recipients`; do 
      echo "RCPT TO:" $i; 
      sleep 1;
    done;
    echo "DATA";
    sleep 1;
    cat /tmp/message;
    sleep 1;
    echo ".";
    sleep 1;
    echo "QUIT") | telnet remote.host 25
    
  4. Do a chmod 744 smtp-telnet.sh and run the script:
    $ ./smtp-telnet.sh

The sleep parameter can be increased to compensate for delays. I tested it on 50 users in my local test environment, and it worked for me.