Archive for the ‘cli’ tag
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 ~]$</lf></cr></lf></cr>
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:
- Create a recipients file,
/tmp/recipients:recipient1@some.domain recipient2@another.domain ... recipientX@yet.another.domain
- Create a message file,
/tmp/message:Subject: This is a test From: Sender This is only a test. - 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
- Do a
chmod 744 smtp-telnet.shand 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.
Backing up Wordpress database
I’m an O.C. and a paranoid (bad combination, I know), and I’ve learned from experience that there’s nothing like a good backup strategy to cover your bases.
So I regularly back everything up, but unlike Linus, I don’t have a readily available FTP server to do that. I do have a GMail account, though, so that’s the next best thing. Below is my script to back up Coredump’s SQL database:
#!/bin/sh DBNAME=database_name DBUSER=username DBPASS=password DBHOST=database_host EMAIL="my_email@ddr.ess" SUBJECT="SQLbackup" mysqldump --opt -u $DBUSER -p$DBPASS -h $DBHOST $DBNAME > backup.sql gzip -9 backup.sql DATE=`date +%Y%m%d`; mv backup.sql.gz backup-$DATE.sql.gz echo 'SQL backup is attached.' | mutt $EMAIL -s $SUBJECT -a backup-$DATE.sql.gz rm backup-$DATE.sql.gz
Change the user information above (from wp-config.php), chmod 711 the script, and plug it in crontab like so:
@weekly $HOME/path/to/script >/dev/null 2>&1
In GMail, create a filter to archive the incoming mail from above.
Using non-interactive FTP
We have a secret (therefore, widely known and very popular) stash of films and TV series episodes somewhere in the office. This clandestine repository is an FTP server, which probably gets more traffic than the various issue tracking and CRM systems we use around here.
Sometimes, though, it gets tedious logging on to the server, checking for new additions, before finding out what new files to download. Good thing the pirates were sane enough to come up with a “What’s new” page in the root of the FTP server’s public directory. So, I whipped up a very simple script that would fetch this file:
#!/bin/sh ftp pirate.ftp < <EOT user username password get whatsnew.txt quit EOT
I placed the above in a crontab, executed daily at early dawn. Then, it’s just a matter of grepping for my faves and downloading them promptly. The download could be automated as well, but it’s a bit tedious and requires more braincells than I can dedicate for this sort of work — much easier to just eyeballing what files I particularly like from the text file.
CLI shortcuts, 6
Here’s a nifty SSH ’shortcut’. This requires that you have an existing SSH key on the remote box.
Use this script, named ssh-to:
#!/bin/sh ssh `basename $0` $*
Add to the bin directory, then add symlinks to your remote box(es):
$cd /bin $ln -s ssh-to remote-server-name
Issue a remote command:
remote-server-name freeSource: O’Reilly Linux Server Hacks
CLI keyboard shortcuts
Got this from CJ Pangilinan, through the PLUG mailing lists:
- Ctrl+S. Scroll lock.
- Ctrl+Q. Release scroll lock.
- Home or Ctrl+A. Move the cursor to the beginning of the current line.
- End or Ctrl+E. Move the cursor to the end of the current line.
- Alt+B. Move the cursor to the beginning of the current or previous word.
- Alt+F. Move the cursor to the end of the next word.
- Ctrl+U. Erase the current line.
- Ctrl+K. Delete the line from the position of the cursor to the end of the line.
- Ctrl+W. Delete the word before the cursor.
- Shift+PageUp. Scroll terminal output up.
- Shift+PageDown. Scroll terminal output down.
- Ctrl+L. Does exactly the same as typing the clear command.
- ArrowUp or Ctrl+P. Scroll up in the history and edit the previously executed commands. To execute them, press Enter like you normally do.
- ArrowDown or Ctrl+N. Scroll down in the history and edit the next commands.
- Ctrl+R. Find the last command that contained the letters you’re typing.
- Ctrl+C. Kill the current process.
- Ctrl+Z. Send the current process to background. Type the command
fgto get the process back. - Ctrl+D. Log out from the current terminal.
Thanks, CJ!
