Archive for November, 2009

Linux Network Setup/Monitor commands

Posted on November 30, 2009. Filed under: Linux | Tags: , , , , |

1. check devices status:

lspci  (dmidecode, lsusb, lshw , lspci)

reference: http://ubuntuforums.org/showthread.php?t=320995

2.  Check the network status:

mii-tool  (you should be root)

ip link

References:

http://www.linuxforums.org/forum/redhat-fedora-linux-help/129167-how-detect-hardware-add-new-one.html

3. NIC card change sequence:

nameif

(hwinfo)

/lib/udev/rename_netiface <old> <new>

References:

http://www.centos.org/modules/newbb/viewtopic.php?topic_id=10434

http://www.cyberciti.biz/faq/rhel-fedora-centos-howto-swap-ethernet-aliases/

http://www.linuxforums.org/forum/suse-linux-help/92280-how-change-ethx-ethy.html

4. iftop

a good console bandwidth visualization tool that shows you active
connections, where they are going to/from and how much of your precious bandwidth
they are using.

iftop is a command-line system monitor tool that produces a frequently-updated list of network connections. By default, the connections are ordered by bandwidth usage, with only the “top” bandwidth consumers shown.

Reference:

http://en.wikipedia.org/wiki/Iftop

5. iptraf

IPTraf is a console-based network statistics utility for Linux. It gathers a variety of figures such as TCP connection packet and byte counts, interface statistics and activity indicators, TCP/UDP traffic breakdowns, and LAN station packet and byte counts.

http://iptraf.seul.org/

Read Full Post | Make a Comment ( 1 so far )

BASH Shell: For Loop File Names With Spaces

Posted on November 20, 2009. Filed under: Linux, Mac, Shell | Tags: , , , , , , |

BASH for loop works nicely under UNIX / Linux / Windows and OS X while working on set of files. However, if you try to process a for loop on file name with spaces in them you are going to have some problem. for loop uses $IFS variable to determine what the field separators are. By default $IFS is set to the space character. There are multiple solutions to this problem.

Set $IFS variable

Try it as follows:

#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for f in *
do
  echo "$f"
done
IFS=$SAVEIFS

OR

#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
# set me
FILES=/data/*
for f in $FILES
do
  echo "$f"
done
# restore $IFS
IFS=$SAVEIFS

More examples using $IFS and while loop

Now you know that if the field delimiters are not whitespace, you can set IFS. For example, while loop can be used to get all fields from /etc/passwd file:

....
while IFS=: read userName passWord userID groupID geCos homeDir userShell
do
      echo "$userName -> $homeDir"
done < /etc/passwd

Using old good find command to process file names

To process the output of find with a command, try as follows:

find . -print0 | while read -d $'' file
do
  echo -v "$file"
done

Try to copy files to /tmp with spaces in a filename using find command and shell pipes:

find . -print0 | while read -d $'' file; do cp -v "$file" /tmp; done

Processing filenames using an array

Sometimes you need read a file into an array as one array element per line. Following script will read file names into an array and you can process each file using for loop. This is useful for complex tasks:

#!/bin/bash
DIR="$1"

# failsafe - fall back to current directory
[ "$DIR" == "" ] && DIR="." 

# save and change IFS
OLDIFS=$IFS
IFS=$'\n' 

# read all file name into an array
fileArray=($(find $DIR -type f))

# restore it
IFS=$OLDIFS

# get length of an array
tLen=${#fileArray[@]}

# use for loop read all filenames
for (( i=0; i<${tLen}; i++ ));
do
  echo "${fileArray[$i]}"
done

Playing mp3s with spaces in file names

Place following code in your ~/.bashrc file:

mp3(){
	local o=$IFS
	IFS=$(echo -en "\n\b")
	/usr/bin/beep-media-player "$(cat  $@)" &
	IFS=o
}

Keep list of all mp3s in a text file such as follows (~/eng.mp3.txt):

/nas/english/Adriano Celentano - Susanna.mp3
/nas/english/Nick Cave & Kylie Minogue - Where The Wild Roses Grow.mp3
/nas/english/Roberta Flack - Kiling Me Softly With This Song.mp3
/nas/english/The Beatles - Girl.mp3
/nas/english/John Lennon - Stand By Me.mp3
/nas/english/The Seatbelts, Cowboy Bebop - 01-Tank.mp3

To play just type:
$ mp3 eng.mp3.txt

Another example about IFS:

#!/bin/bash

IFS=$’\n’
set $(cat my.file)

# Now the lines are stored in $1, $2, $3, …

echo $1
echo $2
echo $3
echo $4

Reference:

http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html

Read Full Post | Make a Comment ( None so far )

Install Chromium (Google chrome) web browser in Ubuntu

Posted on November 10, 2009. Filed under: Linux |

Chromium is an open-source browser project that aims to build a safer, faster, and more stable way for all Internet users to experience the web. The Chromium codebase is the basis for Google’s Chrome browser.

First you need to edit the /etc/apt/sources.list file

sudo gedit /etc/apt/sources.list

Add one of the following

For ubuntu 9.10 (armic) Users add the following two lines

deb http://ppa.launchpad.net/chromium-daily/ppa/ubuntu karmic main

deb-src http://ppa.launchpad.net/chromium-daily/ppa/ubuntu karmic main

For ubuntu 9.04 (Jaunty) Users add the following two lines

deb http://ppa.launchpad.net/chromium-daily/ppa/ubuntu jaunty main

deb-src http://ppa.launchpad.net/chromium-daily/ppa/ubuntu jaunty main

For ubuntu 8.10 (Intrepid) Users add the following two lines

deb http://ppa.launchpad.net/chromium-daily/ppa/ubuntu intrepid main

deb-src http://ppa.launchpad.net/chromium-daily/ppa/ubuntu intrepid main

Save and exit the file Now add the GPG key using the following command

sudo apt-key adv –recv-keys –keyserver keyserver.ubuntu.com 0xfbef0d696de1c72ba5a835fe5a9bf3bb4e5e17b5

Update the source list using the following command

sudo apt-get update

Install chromium using the following command

sudo apt-get install chromium-browser

Using Chromium You can open chromium from Applications—>Internet—>Chromium Web Browser

 

References:

http://www.ubuntugeek.com/install-chromium-google-chrome-web-browser-in-ubuntu.html

http://www.ubuntugeek.com/how-to-install-chromium-google-chrome-in-ubuntu-using-deb-package.html

http://www.linuxscrew.com/2009/06/22/install-ubuntu-chromium-browser-google-chrome-for-linux/

http://www.google.com/chrome/intl/en/linux.html

Read Full Post | Make a Comment ( None so far )

Bash small tricks

Posted on November 2, 2009. Filed under: Linux, Shell |

1. How to get the output of the command? (Not the return/exit status of the command)

ret=$(echo 123)

or

ret=`echo 123`

Read Full Post | Make a Comment ( None so far )

Useful linux commands: null,zero,full,lsof

Posted on November 1, 2009. Filed under: Linux | Tags: , , , |

Linux / Unix Command: null

NAME

null, zero – data sink

DESCRIPTION

Data written on a null or zero special file is discarded.

Reads from the null special file always return end of file, whereas reads from zero always return characters.

null and zero are typically created by:

mknod -m 666 /dev/null c 1 3
mknod -m 666 /dev/zero c 1 5
chown root:mem /dev/null /dev/zero
chmod 666 /dev/null /dev/zero

References:

http://linux.about.com/library/cmd/blcmdl4_null.htm

DESCRIPTION
File /dev/full has major device number 1 and minor device number 7.

Writes  to  the  /dev/full device will fail with an ENOSPC error.  This
can be used to test how a program handles disk-full errors.

Reads from the /dev/full device will return characters.

Seeks on /dev/full will always succeed.

CONFIGURING
If your system does not have /dev/full created already, it can be  cre-
ated with the following commands:

mknod -m 666 /dev/full c 1 7
chown root:root /dev/full

Reference:

man full

lsof

lsof is a command meaning “list open files”, which is used in many Unix-like systems to report a list of all open files and the processes that opened them. This open source utility was developed and supported by Vic Abell, the retired Associate Director of the Purdue University Computing Center. It works in and supports several UNIX flavors.[2]

Open files in the system include disk files, pipes, network sockets and devices opened by all processes. One use for this command is when a disk cannot be unmounted because (unspecified) files are in use. The listing of open files can be consulted (suitably filtered if necessary) to identify the process that is using the files.

# lsof /var
COMMAND     PID     USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME
syslogd     350     root    5w  VREG  222,5        0 440818 /var/adm/messages
syslogd     350     root    6w  VREG  222,5   339098   6248 /var/log/syslog
cron        353     root  cwd   VDIR  222,5      512 254550 /var -- atjobs

To view the Port associated with a daemon :

 # lsof -i -n -P | grep sendmail
 sendmail  31649    root    4u  IPv4 521738       TCP *:25 (LISTEN)

From the above we can see that “sendmail” is listening on its standard port of “25”.

  • -i Lists IP sockets.
  • -n Do not resolve hostnames (no DNS).
  • -P Do not resolve port names (list port number instead of its name).

Reference:

http://en.wikipedia.org/wiki/Lsof

man lsof

Read Full Post | Make a Comment ( None so far )

Liked it here?
Why not try sites on the blogroll...