Showing posts with label grep. Show all posts
Showing posts with label grep. Show all posts

Wednesday, September 30, 2009

How to Change MAC Address

Changing MAC address of a machine is called spoofing a MAC address or faking a MAC address. In linux, you can change MAC address of your machine. This is how it is done.

First find the physical MAC address of your machine by running the following command :

$ ifconfig -a | grep HWaddr
eth0 Link encap:Ethernet HWaddr 00:1f:f3:cc:c2:f9

The hexadecimal numbers in blue denote my machine's MAC address. Yours will be different.

Next, login as root in Linux and enter the following commands -



# ifconfig eth0 down
# ifconfig eth0 hw ether 00:11:22:33:44:55
# ifconfig eth0 up
# ifconfig eth0 | grep HWaddr


Note above that I have changed the MAC address to a different number highlighted in blue. 00:11:22:33:44:55 is the new MAC address I have provided for my Linux machine. You can choose any 48 bits hexadecimal address as your MAC address.

Friday, August 14, 2009

Determining Bits of CPU and OS (32/64-bits)

In order to determine whether OS/CPU is 32-bit or 64-bit,
you can use this shell script.


#!/bin/bash
echo -n "Running "

RES=`uname -a | grep 64`
if [ $? -eq 0 ]; then

echo -n "64-bit "
else
echo -n "32-bit "
fi
echo -n "operating system on a "

RES=`cat /proc/cpuinfo | grep " lm "`
if [ $? -eq 0 ]; then

echo -n "64-bit "
else
echo -n "32-bit "
fi
echo "machine"




Sample Run:

$ ./os_cpu.sh
Running 64-bit operating system on a 64-bit machine

Friday, June 26, 2009

Finding Block Size of Filesystem

In order to, find block size of a filesystem on Linux,

$ sudo tune2fs -l /dev/sda1 | grep -i 'block size'
Block size: 4096

OR

$ echo "mitesh"> test && du test | awk '{print $1}' && rm -f test
4K

Tuesday, January 13, 2009

Playing Directory in Mplayer

In order to play media files inside a directory with mplayer, I have written a shell script. Please save it in a directory in your $PATH variable.

For example, ~/bin/mplayer_dir.sh

#!/bin/bash


# Plays files inside a directory with mplayer

playlist="/home/mitesh/.mplayer/playlist.txt"

dir="./"
if [[ $# > 0 ]]
then
dir=$1
fi

#rm -f $playlist
find "$dir" -iname "*" | egrep -i "\.(wma|wmv|flv|mp3|avi|vob|dat|mp4|m4v|ogg|divx|xvid|rmvb|rm|asf)$" | sort > $playlist
#cat $playlist
nlines=`wc -l $playlist | awk '{print $1}'`

if [[ $nlines > 0 ]]
then
mplayer -playlist $playlist
else
echo "$0: Unrecognized files in $dir"
echo "Please update this script if playable format is present."

fi

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

Sample Run:

$ mplayer_dir.sh [directory]

Monday, October 13, 2008

Terminating Processes Containing Given Pattern

Generally, if you want to terminate a process which contains the given pattern, you use following 4 steps:

  1. $ ps aux | grep pattern 
  2. Copy the pid of the process from output of above command.
  3. Paste this pid in the following command
  4. $ kill -9 pid

The above procedure is cumbersome and requires much typing.

Therefore I have written a program to terminate process(es), which contains given pattern. (I am assuming that ~/bin is in your $PATH).

$ vi ~/bin/kill_prog.sh

#!/bin/sh

# Kills the program given


if [ $# -lt 2 ]
then
  echo "Usage: $0 [1/0] signal program-name"
  echo "[0/1] Do not Kill / Do kill (optional)"
  echo "signal -TERM (Graceful Terminate), -KILL (Abrupt KIll)"
  exit
fi

donothing=1
signal=$1
program=$2
if [ $# -gt 2 ]
then
  donothing=$1
  signal=$2
  program=$3
fi

pids=$(ps auxww | awk "/ $program / { print \$2 }")


for pid in $pids
do
  if [ $donothing -eq 1 ] ; then
  echo "kill $signal $pid"
  else
  kill $signal $pid
  fi
done

#end kill_prog.sh

By default, this program does not kills the process. You have to

pass 0 as second parameter to the command to kill.

Sample runs of above programs:

$ kill_prog.sh
Usage: /home/mitesh/Programming/Shell/WCS/kill_prog.sh [1/0] signal program-name
[0/1] Do not Kill / Do kill (optional)
signal -TERM (Graceful Terminate), -KILL (Abrupt KIll)

$ kill_prog.sh -TERM ping
kill -TERM 16050
$ kill_prog.sh -KILL ping
kill -KILL 16050
$ kill_prog.sh 1 -KILL ping
kill -KILL 16050
$ kill_prog.sh 0 -KILL ping
Killed

The last command actually terminates(-TERM) or kills(-TERM).

Tuesday, June 10, 2008

Process Status of Any Process Containing given String

In order to get Process Status (ps) of any process containing given string
(say 'mitesh'), we use to type following commands, which are long and
tedious to type.

$ ps auxww | grep "mitesh" | grep -v grep

Instead, if we type

$ psg.sh mitesh

which is more convenient to type. So what this '
psg.sh'
contains (I am assuming, that
~/bin is in $PATH):

$ vi ~/bin/psg.sh

#!/bin/bash
function is ()
{
ps auxww | grep "$@" | grep -v "grep"
}

is $@

# END : psg.sh