Thursday, July 17, 2014

To replace text in vim with line number

e.g.:
mv file_abc.jpg pic.XX.jpg
mv random.jpg   pic.XX.jpg
mv other.jpg    pic.XX.jpg
mv name.jpg     pic.XX.jpg
If you do this:
:%s/XX/\=line(".")/

Tuesday, July 01, 2014

Make SSH ignore SSH key

Make SSH ignore your SSH key, not prompt for a passphrase, only use password:

$ ssh -o PreferredAuthentications=keyboard-interactive -o PubkeyAuthentication=no destination.host

Wednesday, May 28, 2014

find things...

Find files that are not world readable and make them readable:

sudo find . -type f \! -perm -4 -exec chmod o+r {} +

Find files that are not group readable

find . -type f \! -perm -40 -ls

Find files that are group writable

find . -type f -perm -20 -ls

Find files that are world writable

find . -type f -perm -2 -ls

Wednesday, May 14, 2014

To find if a Solaris host is running on an LDom

If the file /usr/sbin/virtinfo exists, run:

$ /usr/sbin/virtinfo -a

If you run it as root it will also tell you the hostname of the Control domain and the Chassis serial#.

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: http://nmap.org/book/osdetect-other-methods.html )

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

Try this:

$ nmblookup -A 12.34.56.78
Looking up status of 12.34.56.78
        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}'
20236


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; }
    close($smaps);

    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!