Archive for the ‘Linux’ Category

How to exlude grep from a ps pipe result

Posted: October 3, 2012 in DIY, Linux, script
Tags: , , ,

How to exlude grep from a ps pipe result

Hello guys,

This is a short post of something I find very usefull if you have to do “ps -ef” often.

When you do a ” server#ps -ef | grep ‘something’ ” on any server you can se a line that also includes the grep process.

root@yourdiy.net:/# ps -ef | grep something
something      1084     1  0 Oct02 tty4     00:00:00 /sbin/getty 38400 tty4
something      1085     1  0 Oct02 tty5     00:00:00 /sbin/getty 38400 tty5
something      1086     1  0 Oct02 tty6     00:00:00 /sbin/getty 38400 tty6
something     20064 19246  0 09:44 pts/0    00:00:00 su
something     20109 20064  0 09:46 pts/0    00:01:21 bash
something     20748   883  0 17:28 ?        00:00:00 sleep 60
root     20750 20109  0 17:28 pts/0    00:00:00 grep something
root@yourdiy.net:/# 

You can see the  last line that includes the commands we just used, to avoid this you can simply use a negative form of grep.

 

root@yourdiy.net:/# ps -ef | grep something | grep -v "grep something"
something      1084     1  0 Oct02 tty4     00:00:00 /sbin/getty 38400 tty4
something      1085     1  0 Oct02 tty5     00:00:00 /sbin/getty 38400 tty5
something      1086     1  0 Oct02 tty6     00:00:00 /sbin/getty 38400 tty6
something     20064 19246  0 09:44 pts/0    00:00:00 su
something     20109 20064  0 09:46 pts/0    00:01:21 bash
something     20748   883  0 17:28 ?        00:00:00 sleep 60
root@yourdiy.net:/#

 

Like this you can see that the grep process is not longer listed, this is very usefull if you are using the output in a script.
I hope this little info help you.

Regards

File Growth Check Script

Posted: July 7, 2011 in CLI, Linux

Hello Guys,

This little script is for cecking on a file growth, and letting us know when the file has finished growing, more updates soon of course but here is the first release. ENJOY


#!/bin/bash
# File Growth Check Script
# Created by wsanriv 7/7/2011
# First input is file Path
# Example: "fgc.sh /tmp/file1"

#Input File path
if [ $# -lt 1 ]
then
    echo "Please give file"
    exit 1
fi
FILE_IN=$1

#Check for empty path or non file path
if [ ! -f $FILE_IN ]
then
    echo "Not a valid file"
    exit 1
fi

#Sample initialization
sample_a=0
tic=0

#Loop for Growth check
while true
    do
    clear
    sample_b=$sample_a
    ls -alh $FILE_IN | grep $FILE_IN
    sleep 1
#---Check if growth stoped
    sample_a=$(stat -c%s ${FILE_IN})
    if [ $sample_a -eq $sample_b ]
    then
    echo "SAME SIZE"
    $tic = ${tic} + 1
    fi
    done

#END

Hello Guys,

I just finished a very funny script to get the current bandwidth utilization of a Linux/Unix ethernet card.
This is just the first draft I have to make tons of changes, and options. but this is the main body the first working copy.

As always any comments are appreciated.

#!/bin/bash

#Function to get bit sample
get_sample()
{
/sbin/ifconfig eth0 | grep 'RX bytes:' | awk ' { split($2, RX, ":"); print RX[2]}'
}

#Function to calculate the difference between 2 samples
calc_sample()
{
A=$(get_sample)
sleep 1
B=$(get_sample)
echo $(($B - $A))
}

#Function to make the output more human readable
humanize()
{
if [ $((($RESULT * 8)/1000)) -gt 1000 ]
then
echo 0
else
echo 1
fi
}

#Main script
while true; do
RESULT=$(calc_sample)
#This checks if the output is readable or not
if [ $(humanize) -eq 0 ]
then
echo $(((($RESULT * 8)/1000)/10000))
echo "Megabits per second"
else
echo $((($RESULT * 8)/1000))
echo "Kilobits per second"
fi
#Sleep 5 secs before asking for new sample
sleep 5
done

Network Speed

Network Speed

Linux/Unix Ping State Script

Posted: April 21, 2011 in CLI, DIY, Linux

Hello Guys,

After a good afternoon of work, I bring you a small script to check on server network status.
You know “Keep Alive”  style.
This small script is intended to review on how many responses do you get from a ping test.
and you can use the out put for what ever you like, like an php report.

This is the script code:

root@bobafett:/# cat /pingok.sh
#!/bin/bash
HOST=$1
COUNT=4
if [ $# -eq 0 ]; then
        HOST=localhost
fi
count=$(ping -c $COUNT $HOST 2> /dev/null | grep 'received' | awk -F',' '{print $2 }' | awk '{ print $1 }')
if [ -z $count ]; then
        echo "Invalid Output"
        exit 1
fi
#echo $HOST #THIS IS DEBUGING
if [ $count -eq 4 ]; then
        # 100% Worked
        echo "UP"
fi
if [ $count -eq 3 ]; then
        # 25% failed
        echo "H/LOAD"
fi
if [ $count -eq 2 ]; then
        # 50% failed
        echo "INTERMIT"
fi
if [ $count -eq 1 ]; then
        # 75% failed
        echo "DEPRECATED"
fi
if [ $count -eq 0 ]; then
        # 100% failed
        echo "DOWN"
fi

This is a screenshot of how it looks in a PHP report.

I cant copy the entire PHP code, but I will be more than glad to share if you want it.

PHP Report Example

Ping Connection Test with Sound

Posted: February 8, 2011 in CLI, Linux, Windows

Hello Guys,

I just finished a draft for a Ping Connection Test that emits a sound when its Disconnected, I like this to monitor my internet even if I am not using it, also more actions can be done not just the sound, this is just a start, this is the first draft of the code, I will be posting a step by step details later.

@echo off
:A
set destination=%1
ping %destination% -n 1 |FIND /C “TTL” > c:/temp.log
set /p result=< c:/temp.log
IF %result% EQU 0 (
echo “Connection Inactive”
type beep.txt
ping 0.0.0.0 -w 1000 > NUL
GOTO A
)
IF %result% EQU 1 (
echo “Connection Active”
ping 0.0.0.0 -w 100000 > NUL
)
GOTO A

As I said this is just a basic draft later on I will post how to create you own and how to run it.