Tuesday, October 25, 2011

Bash & shell trick

This is a rehash of some old notes I had on the wiki.


Wiki tricks
Move heading in a level deeper
sed -e 's/\(=\+\)/=\1/g' infile > outfile

Wikipedia:Tools/Editing_tools
An HTML to Wiki syntax converter
Convert Word doc or Webpage to wiki
http://excel2wiki.net/

Sed tricks



Bash Tricks


RPM


Harvest host memory profile

gethostinfo.sh
/usr/sbin/dmidecode > /tmp/DMIDECODE
LLIST=`grep -n "Memory Device" /tmp/DMIDECODE | grep -v "Address" | sed 's/:.*//'`
TOTSIZE=0
for i in $LLIST; do
MEMSIZE=`tail -n +$i /tmp/DMIDECODE | head -13 | grep "Size:" | sed 's/No/0/' | awk '{print $2}'`

  TOTSIZE=$[ $TOTSIZE + $MEMSIZE ]
done
PNAME=`grep "Product Name" /tmp/DMIDECODE |  sed  -e 's/.*: //' -e 's/ /_/g' -e '1 q' `
HNAME=`hostname | sed 's/\.xyzcompany.*//'`

OSINFO=`uname -n -m -r`
#SPEEDINFO=`grep "Current Speed:" /tmp/DMIDECODE | sed  -e 's/.*: //' -e 's/000 MHz/ GHz/' -e '1 q' `
NUMCORES=`grep "^processor" /proc/cpuinfo |  sed  -e 's/.*: //'   | tail -1`

NUMCORES=$[ $NUMCORES + 1]
CPUINFO=`grep "model name" /proc/cpuinfo |  sed  -e 's/.*: *//' -e 's/\s\+/_/g' -e '1 q' `
RAMSIZE=`echo "$TOTSIZE MB" | sed -e 's/1024 MB/1 GB/' -e 's/2048 MB/2 GB/' -e 's/4096 MB/4 GB/' -e 's/6144 MB/6 GB/' -e 's/8192 MB/8 GB/' -e 's/12288 MB/12 GB/' -e 's/16384 MB/16 GB/' `

#echo "$OSINFO $PNAME $RAMSIZE $SPEEDINFO"
echo "$OSINFO, $PNAME, $RAMSIZE, $NUMCORES Core, $CPUINFO"



TCPDUMP

sudo tcpdump -i bond0:6 -s 0 -x host 10.1.224.147 and port 8080
sudo tcpdump -i bond0:6 -s 0
sudo tcpdump -i bond0:6 -s 0 -X port 8080 -w /tmp/tdump1
output file tdump1 can be read in using WireShark

rsync

Pulls files off remote and delete's then after verified transfer.
sudo rsync -auvlz  --remove-sent-files jsokol@xyzhost:tmp/xyx* .



search and kill processes
This was for finding and killing XYZ processes on servers, (it's assumed) they will restart themselves.

[jsokol]$ more bouncehosts
if [ -s $1 ] ; then
 awk '{ print $1 }' $1 > /tmp/hostlist1

        for h in $(cat /tmp/hostlist1 );do
        echo ID $h
TMP1=`ps ax | grep xyz | grep Builds | grep "com_$h "`

#         ps ax | grep xyz | grep Builds | grep "com_$h "
        if [ "$?" -eq 1 ] ; then

            TMP2=`grep "^$h " $1`
            echo No XYZ Running for $TMP2
        else

            TMP2=`echo $TMP1 | awk '{print $1}'`
            echo Killing Pid $TMP2
            kill $TMP2
            sleep 30
        fi

#        grep "$h " x2 | awk '{print "'$h' " $1}' | sed 's/XYZ=/ /' | sed 's/%2C/  /g' | sed 's/.xyzco.com/ /g'


        done

else
        echo "file $1 doesn't exist, or you didn't enter a filename"
fi

if [ "$UID" -ne "0" ];then echo "please sUd0 this.."; exit
ssh -q "$3"@hostdb.corp "sudo mysql -e \"select hostname,productname from host where productname like '%$1' and hostname like '%$2%' \" noc";
for i in $(cat XYZ.all); do echo "edit" | ssh sokol@$i 'sudo sed s/34/37/g -i /etc/app/configfile' ;done >> xyz.log

[jsokol@xyz.sjc ~]$ ZZ=6
[jsokol@xyz.sjc ~]$ echo $[4 + $ZZ]
10

echo xyzhost(3..6}.sjc,|sed s/", "/","/g
xyzhost3.sjc,xyzhost4.sjc,xyzhost5.sjc,xyzhost6.sjc,


running average

#!/bin/bash
# example of how to get running average.
if [ ! -s "runaveragedata-last" ] ; then
  echo 0 > runaveragedata-last
fi

NEWVAL=`cat newdata`
RUNAVR=`cat runaveragedata-last`

# doing work in fixpoint numbers  
NEWAVR=$[ $NEWVAL * 1000 + $[ $RUNAVR * 9 ] ]
RUNAVR=$[ $NEWAVR / 10 ]
echo $RUNAVR > runaveragedata-last
echo $[ RUNAVR / 1000 ]

Bash loops

for h in 07 08 09 10 11 12 13 18; do scp host:/path /xyc$h.log .;done

for h in $(cat file ); do echo $h |tee -a outputlog; rsync -auvlz host:$h $h; done

rpm -qa | egrep -i 'egw|logfs' | xargs sudo rpm -e


------------
more dupesniff.sh
#!/bin/bash

for h in $(grep -vE ^# $1  | grep -vE ^$ | awk {'print $1'} | sort | uniq -d )
do
        grep -n $h $1
done

------------

[jsokol]$ cat portprobe.pl

#!/usr/bin/perl -w
#
#  tiny tool meant to mimic a device profile.  It should take ports, or service names,
#  and mimic the open ports epected for that type of machine
#

use IO::Socket;
use Sys::Hostname;


my $proto = "udp";
my $port = "8080";
my $hostname = hostname;
my $persist = 0;
my $server = "localhost";

my $socket = IO::Socket::INET->new(PeerAddr     => $server,

                                PeerPort        => $port,
                                Proto           => $proto,
                                Type            => SOCK_STREAM)
                        or die "Couldn't talk to $server on $port \n\n";




print $socket "GET  \n";
@answer = <$socket>;
close($socket);
print "@answer \n\n";

------------
[jsokol]$ cat portlisten.pl

#!/usr/bin/perl -w
#
#  tiny tool meant to mimic a device profile.  It should take ports, or service names,
#  and mimic the open ports epected for that type of machine
#
#

use IO::Socket;
use Sys::Hostname;


my $proto = "udp";
my $port = "8080";
my $hostname = hostname;
my $persist = 0;

my $server = IO::Socket::INET->new(     LocalPort       => $port,

                #                       Type            => SOCK_STREAM,
                                        Proto           => $proto,
                #                       Reuse           => 1,
                #                       Listen          => 10

                                        ) or die
                "\tCouldn't generate new server socket on port $port\n\n";

print "starting server\n\n";
$server->accept();
while ($server->recv($msg, 255)) {

        print "Got message $msg from $server->peername \n";
        $server->send("Thanks");
}

close($server);
------------

#!/bin/sh

# comm-hack.  Use this to send multiple commands to multiple hosts.  
# Usage: ./scriptname [hostlist filename] [user]


x=$1 #host list
y=$2 #username

#begin

echo
echo -n "Enter password: "
read -s PASS
echo
echo -n "Enter commands: "

read COMMANDS

#for i in $(cat $x ); do echo -e "$PASS" | ssh '$y'@$i $COMMANDS; done

for i in $(cat $x ); do echo "$PASS"| ssh "$y"@$i $COMMANDS;
echo "Executed on host: $i @ `date '+%m/%d/%y%t'` "

done
fi

No comments: