Friday, June 15, 2012

pidof and /proc/pid/environ values and ps and ...

All this time and I was not aware
/sbin/pidof java | sed 's/\s/\n/g'

cat /proc/%/environ | tr \\0 \\n
combined.... 
/sbin/pidof java | sed 's/\s/\n/g' | 

xargs -i% cat /proc/%/environ | tr \\0 \\n
xargs -i% -t ps -o uid,pid,start_time,ucomm -p % w 2> /dev/null  
xargs -i% -t ps -o uid,pid,start_time,comm,args -p % wwe ' 


/sbin/pidof java | sed 's/\s/\n/g' |xargs -i% -t ps -o uid,pid,start_time,comm,args -p % wwe ' 

Wednesday, June 13, 2012

Where Did My Office Computer Go? Scan IP ranges for a service

So I lost my computer, since the network MAC->IP cache was flushed, however I know the subnet and the machine is on. But its a headless piece, no monitor. Where oh, where did my computer go??!?!?! Wireshark is more for recording what comes in, other network utils are on a IP basis... but lets curl ourselves out of here... I want to validate the existence of a twiki site:
curl -f -m 2 http://10.10.10.[100-200]/twiki/bin/view
Yeah its that easy.... and easy to forget...

Tuesday, June 12, 2012

grab that oneliner for sledgehammering with xargs and remote execute

Finally found the command on one node.... now how to grab that and execute it on milions of hosts (with a passwordless setup)
   66  ps auxww| grep Server | grep -v grep | awk '{ print $2 }' | xargs -i{} -t /usr/sbin/lsof -p {}  | grep wls10
Here you go
   77  history | grep Server | grep -v history | cut -c8- | grep wls10 | head -1 > ~/wls10.txt
   78  cat list | xargs -i{} -t ssh {} "$(<wls10.txt)"

xargs and using the stdin reference more than once

Multiple tar extractions in different directories based on the tar name.
computer:x usr$ ls -rtl
total 426552
-rw-r--r--  1 usr  usr  73451520 Jun 12 17:59 l01-2.tar
-rw-r--r--  1 usr  usr  72622080 Jun 12 17:59 l02-2.tar
-rw-r--r--  1 usr  usr  72284160 Jun 12 17:59 l03-2.tar
The only way I was able to do it is to push it into a small script, louzy.... I'll keep you posted on the alternatives.
computer:x usr$ cat test.txt 
mkdir $1 
cd $1 && tar xfv ../$1.tar && cd ..
computer:x usr$ ls -1 *.tar | sed 's/\.tar//' | xargs -I{} -t /bin/bash test.txt {} 

Monday, June 4, 2012

Perl Object Creation, Inheritance, overloading.... in one page?

Keep looking for a small sample with all the bells and whistles that I can shelf for all my object structures in perl. Loads of docs, but I could not find a nice complete one that I could stamp as usable. So here we go. Example is the object relationships between Vehicle, Vehicle::Car and Vehicle::Car::RaceCar all vehicles have a maxspeed (number), car all has wheels (number), racecar has doortype (winged, standard), So here we go run.pl
#!/usr/bin/perl

use strict;
use lib("../lib");
use Vehicle;
use Vehicle::Car;
use Vehicle::Car::RaceCar;

my $v = Vehicle->new(name=>'Ferarri');
printf "The default max speed of any vehicle is %d, %s\n",$v->maxspeed, $v->name;
$v->print;
$v->name('Volvo');
$v->print;
print "----------------------\n";
$v = Vehicle::Car->new(name=>'Beetle', wheels=>4, maxspeed=>100, andre=>43);
$v->print;
$v->wheels(10);
$v->print;

print "----------------------\n";
$v = Vehicle::Car::RaceCar->new(name=>'Ferrari', wheels=>4, maxspeed=>200, doortype=>'Winged');
$v->print;
The following package structure is recommended Vehicle.pm
package Vehicle;
use strict;
use Carp;
use Data::Dumper;
use vars (qw($VERSION @ISA @EXPORT @EXPORT_OK ));

BEGIN {
 # initializer for static variables and methods
 use Exporter;
 use vars qw ( $VERSION @ISA @EXPORT);
 $VERSION = 1.0;
 @ISA     = qw ( Exporter );
 @EXPORT  = qw (static_method_string);
}

#constructor
sub new {
    my ($class) = shift;
    my %x = @_;
    my @override = ();
    foreach my $var (keys %x) {
     my $var_name = $var =~ /^_/ ? $var : sprintf "_%s", $var;
     push @override, ($var_name, $x{$var});
    }
    #print Dumper(\@override, @_);
    my $self = {
        _maxspeed => 0,
        _name => 'N/A',
        @override, #override
    };
    bless $self, $class;
    return $self;
}

#accessor method for name
sub name {
    my ( $self, $name ) = @_;
    $self->{'_name'} = $name if defined($name);
    return ( $self->{'_name'} );
}

#accessor method for maxspeed
sub maxspeed {
    my ( $self, $maxspeed ) = @_;
    $self->{'_maxspeed'} = $maxspeed if defined($maxspeed);
    return ( $self->{'_maxspeed'} );
}


sub print {
    my ($self) = @_;
    printf( "Name:'%-50s' Maxspeed:%s\n", $self->name, $self->maxspeed);
}

sub methodonlyinvehicles {
 print static_method_string("in all vehicles");
}

sub static_method_string {
 sprintf "%s %s\n", 'method call in ', shift;
}

$|=1;
1;
Vehicle/Car.pm
package Vehicle::Car;
use Vehicle;
use strict 'vars';
our @ISA    = qw(Vehicle);
our @EXPORT = qw(wheel);


#constructor
sub new {
    my ($class) = shift;
    my %x = @_;
    my @override = ();
    foreach my $var (keys %x) {
     my $var_name = $var =~ /^_/ ? $var : sprintf "_%s", $var;
     push @override, ($var_name, $x{$var});
    }
    #call the constructor of the parent class, Car.
    my $self = $class->SUPER::new(@override);
    bless $self, $class;
    return $self;
}

#accessor method for wheels
sub wheels {
    my ( $self, $wheels ) = @_;
    $self->{'_wheels'} = $wheels if defined($wheels);
    return ( $self->{'_wheels'} );
}

sub print {
    my ($self) = @_;
    # we will call the print method of the parent class
    $self->SUPER::print;
    printf " has %d wheels\n", $self->wheels;
}

sub methodonlyincars {
 print static_method_string('only in vehicles and cars');
}
1;
Vehicle/Car/RaceCar.pm
package Vehicle::Car::RaceCar;

use strict 'vars';
use Vehicle; # to import the static method in the parent class
use vars qw($VERSION @ISA @EXPORT);
our @ISA    = qw(Vehicle::Car);
our @EXPORT = qw(doortype);

my $CLASS = __PACKAGE__;

sub new {
    my ($class) = shift;
    my %x = @_;
    my @override = ();
    foreach my $var (keys %x) {
     my $var_name = $var =~ /^_/ ? $var : sprintf "_%s", $var;
     push @override, ($var_name, $x{$var});
    }
    #call the constructor of the parent class, Car.
    my $self = $class->SUPER::new(@override);
    bless $self, $class;
    return $self;
}


#accessor method for doortype
sub doortype {
    my ( $self, $doortype ) = @_;
    $self->{'_doortype'} = $doortype if defined($doortype);
    return ( $self->{'_doortype'} );
}

sub print {
    my ($self) = @_;
    # we will call the print method of the parent class
    $self->SUPER::print;
    printf " has %s doors\n", $self->doortype;
}

sub methodonlyinracecar {
 print static_method_string('only in racecar');
}


1;

Adding size Column to Saved Searches

Add Size and other columns to search results ... stolen and plagiarized for my own selfish need. I was very disappointed that I couldn't add the Size column to the list view in search results. Only Date Created and Date Modified were added as options in 10.6; the others are grayed out. Fortunately, I found a way to force the Size column to appear, as well as Comments, Version and Label columns. You'll have to edit the com.apple.finder.plist file found in
~/Library/Preferences
, using the Property List Editor app (included with Xcode) or PlistEdit Pro. Open the Root node, then navigate down into
SearchViewSettings » ListViewSettings » columns
. Then choose the column you want to activate in the column list (ie. Size), and set the visible key to yes. Save the .plist and relaunch the Finder (Option-Control-click on the Finder icon in the Dock). Note: you must have done at least one search, and selected one of the additional columns in the View Options panel for the search results (Date Created, for example), or else your .plist file may not contain the SearchViewSettings node. Warning: Apple probably disabled these columns for a good reason, as there may be some bugs that could cause problems on your system. I didn't have any problems with the size column, but be warned.

Friday, June 1, 2012

Vanguard exports and Google OFX imports

Okay wanted to straighten out my finances and have one overview in Google Finance.... lets export and import. The Vanguard website will allow you to download them in a csv format which contains all the valid data according to OFX. However they forgot the symbols and put the shorthand description there.... so totally useless.... so lets parse this baby into the correct format... I only had to do 5 different sources .... but feel free to adjust or add...
:Downloads amiga$ cat ofxdownload.csv | perl -pe 's/Total Bond Mkt Index Inst/VBTIX/;s/Windsor Fund Investor/VWNDX/;s/Total Intl Stock Ix Inv/VGTSX/;s/PRIMECAP Fund Investor/VPMCX/;s/500 Index Fund Signal/VFIAX/;'

remote shell qoutes and double qoutes in complex operations

I prefer the sledgehammer when managing multiple servers. but what about quoting and double quoting and double double qouting.... escaping in on thing.... however how about this.... Here a oneliner for printing the statistics on TCP state of a box.
[dswown@pghpviswl01 ~]$ cat cmd.txt 
netstat | perl -MData::Dumper -ne 'BEGIN {%h=();} chomp;$_=~s/\s+/ /g;@x=reverse split/[\s+:]/ if /tcp|udp/; if (@x) {$x[2]=~s/\.c?\w?\w?$//g;print Dumper(\@x), $_ if $x[2] eq '8471';$h{count}{$x[0]}++;$h{$x[0]}{$x[2]}++;
} END { print Dumper(\%h)}'

[dsw@pviswl01 ~]$ cat list | xargs -i{} -t ssh {} $(<cmd.txt)

Where the file 'list' contains the following:
user1@hostA
user2@hostB