I often find myself changing routes on my OS X system. Usually, however, I’m systematically adding the same routes over and over again, because they were automatically removed after a reboot, network change or whatever. Enter.
Posts tagged ‘script’
I regularly watch log files in real time using the highly appreciated tail -f command. But I usually find myself manually inserting newlines to give a visual clue of which log-lines happened together. Obviously the timestamps in the lines tell you the full story, but it’s not that visually appealing.
SSH is a wonderful tool. It allows you to run commands on a remote host, either manually, or from scripts. Obviously, since it’s a remote connection, you must authenticate yourself to the remote host. There are several ways of doing this.
When using SSH in a script, most pages tell you to use public keys. While this is an excellent idea, it’s sometimes just not possible due to policy. This Expect script fakes a regular username-password login
set target [lindex $argv 0]
set password [lindex $argv 1]
set command [lindex $argv 2]
spawn ssh $target $command
# Look for passwod prompt
# Send password aka $password
send — “$password\r”
# send blank line (\r) to make sure we get back to gui
send — “\r”
This script can be run like this:
./ssh-passwd.ex firstname.lastname@example.org password “ls /root”
Sometimes it’s really useful to prepend a timestamp to every output line of a command. This can be done fairly easily:
$command | \ perl -pe '@now=localtime();printf "%04d-%02d-%02dT%02d:%02d:%02d ",$now+1900,$now+1,$now,$now,$now,$now;'
The perl command reads in every line, prints the current time in the default format (or in whatever format you specify), followed by the read line.
ssh-to-puttyssh-to-puttyIn Windows you can register “url handlers”. These are programs that are run when you try to open a URL (via Start->Run for example). “http://” for example is registered to Internet Explorer by default. “telnet://” also works. This is especially useful in combination with the URL-field of KeePass. Double-clicking on this field tries to “open” the specified URL.
However, “ssh://” is not a standard registered protocol. I’d like Putty to handle this. Also, “telnet://” gets you the standard windows telnet client instead of putty. Putty can be called with command line arguments. Supplying the “telnet://” url as a parameter works, but “ssh://” does not.
Hence, I wrote a very small wrapper program to accept “ssh://” URL’s and convert them to Putty command line arguments:
- Source code in C: ssh-to-putty.c
- Compiled Windows executable:
ssh-to-putty.exe(some virus-scanners seem to think this is a virus, exe is no longer available, please compile it yourself)
- Registry commands to set putty as telnet-handler: putty telnet url handler.reg
- Registry commands to set the wrapper as ssh-handler: putty ssh url handler.reg
- The registry commands assume Putty and the wrapper are installed in C:\Progs\SSH. If this is not the case, you need to change the .reg-files accordingly
- The wrapper-program assumes putty.exe to be in the same directory as itself
I was looking for an easy way to parse a binary file. I know what the file contains (it’s an MPEG2 transport stream) and know the bit-field layout. It’s just a pain to figure the bits out manually in a hex editor.
That’s when I came across the Data::ParseBinary perl module, which is a true relief to use. It supports pretty much every thing you need to parse a binary file:
- Signed and unsigned integers
- Big and little endian
- 8, 16, 32 and 64 bit integers
- Enum-types to specify your own names for values
- If-constructs: Fields are present or not depending on the value of another field
In short, an incredible tool!
Besides the standard password authentication, ssh also supports public key authentication. This key-based authentication has the added bonus of having per-key options:
- you can restrict the source IP from which this key may be used
- you can force a command to be executed instead of allowing the connecting side to specify one
I got another toy to play with: A digital multimeter with RS232 interface and True RMS power measurement. Sadly, it comes with Windows-only software, which I interpreted as a challenge!