Thursday, November 14, 2013

list to csv...

ls | paste -s -d'%' - | sed 's/\(^\|$\)/"/g;s/%/","/g'

Friday, October 11, 2013

web client in pure bash

( printf "GET /path/to/file HTTP/1.0\n\n" 1>&0 ; cat ) 0<> /dev/tcp/<HOSTNAME>/80
Thanks David!

Tuesday, August 13, 2013

Get Windows hostname from IP address via Samba

You can tell it's likely to be a Windows box by what ports it has open...

"The target host OS can often be guessed simply by looking at the ports which are open. Microsoft Windows machines often have TCP ports 135 and 139 open. Windows 2000 and newer also listen on port 445." ( from: )

Once you've identified it's a Windows host, how can you tell which one?

Try this:

$ nmblookup -A
Looking up status of
        HOSTNAME        <00> -         M <ACTIVE>
        DOMAINNAME      <00> - <GROUP> M <ACTIVE>
        HOSTNAME        <1f> -         M <ACTIVE>
        HOSTNAME        <20> -         M <ACTIVE>
        DOMAINNAME      <1e> - <GROUP> M <ACTIVE>

        MAC Address = 00-11-22-33-44-55

Tuesday, August 06, 2013

swap usage...

To find out how much swap processes are using...

On Solaris:

ps -eo vsz,rss,pid,args | sort -n top -b -o size

Total swap used by pid

pmap -S $PID | tail -1 | awk '{print $4}'

Don't really want to use these:

prstat -s rss
prstat -s size

Size: total virtual memory size of the process, including all mapped files and devices.
RSS: should be the resident set size, but is completely unreliable.

On Linux:

#!/usr/bin/perl -s

use warnings;
use strict;

if ( $< != 0 ) {
    die "ERROR: $0 Needs to be run as root\n";
our ($c,$m,$n,$t);

opendir(my $proc,"/proc");

my $pids;
my $total;

for my $pid (grep {/^[0-9]+$/} readdir($proc)) {

    # If the process has terminated already, skip on to the next one.
    open(my $smaps, "/proc/$pid/smaps") || next;
    while(<$smaps>) { next if !/^Swap:\s+(\d+) kB$/; $pids->{$pid}->{swap} += $1; }

    if ( $pids->{$pid}->{swap} ) {

        open(my $cmdline, "/proc/$pid/cmdline");
        while(<$cmdline>) { s/\0/ /g; $pids->{$pid}->{cmdline} = $_; }
        $total += $pids->{$pid}->{swap};

    } else {

        delete $pids->{$pid};



if ( $c && ! $n ) {
    print "  PID   Swap Cmd\n";
for my $pid ( sort { $pids->{$b}{swap} <=> $pids->{$a}{swap} } keys %$pids ) {

    # Only report procs > 1MB
    next if $pids->{$pid}->{swap} < 1024;
    # Specify minimum size (MB) to report
    # print "m [$m] swap[$pids->{$pid}->{swap}]\n";
    next if ( $m && $pids->{$pid}->{swap} < $m * 1024 );
    if ( $c ) {
        printf "%5d %4dMB %s\n", $pid, $pids->{$pid}->{swap} / 1024, $pids->{$pid}->{cmdline};
    } else {
        printf "%5d %4dMB\n", $pid, $pids->{$pid}->{swap} / 1024;


if ( $t ) {
    printf "Total: %dMB\n", ${total} / 1024;

Monday, July 08, 2013

Copy history from one git repo to another

cd repository
git log --pretty=email --patch-with-stat --reverse -- path/to/file_or_folder > ../patch
cd ../other_repository
git am < ../patch

Thursday, May 02, 2013

Portable, super efficient alternative to seq

Ever missed seq on a non-gnu system (like older Solaris)?

Here's an extremely portable alternative which turns out to be much more efficient too!

yes '' | head -100000 | cat -n

Phil explained that the efficiency is to do with the way that cat does its line numbering (by incrementing the ASCII characters rather than counting numbers and converting them to characters) Thanks Phil!

Wednesday, March 20, 2013

Trash the hard disk on Solaris VM

If you want to wipe out the boot sector on a Solaris x86 VM so a reboot takes you straight into a net boot to rebuild the host... (because often VMWare takes so long for the console to come up that you don't have a chance to hit F12 before it boots the grub loader)

# dd if=/dev/zero of=/dev/dsk/c1t0d0 count=1 bs=512

Linux: Which processes are using the most RAM?

An easy clear way to see which processes are gobbling all the memory:

$ ps -eo pid,vsz,args | sort -k2n

I got the idea from:

Wednesday, December 19, 2012

Sometimes when you want to do something on the web, they insist you give an email address to sign up.

Many of them won't accept addresses from

But they MAY accept addresses from:

easy... just point a mailexpire address at a guerrillamail account...

Job done! :-)

Friday, December 14, 2012

VBS script to launch multiple PuTTY sessions

This is to launch parallel PuTTY sessions to a list of hosts, as by the name of their saved PuTTY sessions.

Set objShell = CreateObject("WScript.Shell") dim hosts hosts = "host1 host2 host3 host4" dim host for each host in split(hosts) objShell.Exec "C:\Program Files\PuTTY\PUTTY.EXE -load " + host next

NB: you could use

objShell.Exec "C:\Program Files\PuTTY\PUTTY.EXE username@" + host

instead of

objShell.Exec "C:\Program Files\PuTTY\PUTTY.EXE -load " + host

If you want to log straight into the box without using a saved session