Monday, July 30, 2012

Beginners Guide to know the network in Linux command line [Part 2]


Beginners Guide to know the network in Linux command line [Part 2]

Hey Folks , as we have already learnt the three most basic networking commands (Ifconfig, netstat and ping) in Linux in our last post we will now concentrate on the other most important networking commands in Linux.

1.traceroute :

Traceroute utilizes the IP protocol ‘time to live’ field and attempts to elicit an ICMP TIME_EXCEEDED response from each gateway along the path to some host. The only mandatory parameter is the destination host name or IP number. The default probe datagram length is 40 bytes, but this may be increased by specifying a packet length (in bytes) after the destination host name.So This command printa the route packets take to network host.Typically Ubuntu installs tracepath for IPv4 and traceroute6 for IPv6. One can install traceroute: apt-get install traceroute.
traceroute IP-address-of-server
traceroute domain-name-of-server
Examples
traceroute computerandyou.net - would display results similar to the below example.
traceroute to computerandyou.net (166.70.11.24), 60 hops max, 80 byte packets
1 198.60.22.1 (198.60.22.1) 2.303 ms 1.424 ms 2.346 ms
2 krunk3.xmission.com (198.60.22.6) 0.742 ms * 1.521 ms

Note: in this example because we are local to the address we are tracerouting the amount of hops is very minimal. However, when you traceroute computerhope.com you may hop more than we do.

2.W-displays :

It provides the information about the users currently on the machine, and their processes. The header shows, in this order, the current time, how long the system has been running, how many users are currently logged on, and the system load averages for the past 1, 5, and 15 minutes..
Syntax
w [-husfVo] [user] Examples
w
Would display information similar to the below example.
18:31:58 up 156 days, 5:16, 64 users, load average: 0.27, 0.27, 0.27
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT

3.ARP :

Arp manipulates the kernel’s ARP cache in various ways. The primary options are clearing an address mapping entry and manually setting up one. For debugging purposes, the arp program also allows a complete dump of the ARP cache.ARP displays the IP address assigned to particular ETH card and mac address.
arp [-vn] [-H type] [-i if] -a [hostname] arp [-v] [-i if] -d hostname [pub] arp [-v] [-H type] [-i if] -s hostname hw_addr [temp] arp [-v] [-H type] [-i if] -s hostname hw_addr [netmask nm] pub
arp [-v] [-H type] [-i if] -Ds hostname ifa [netmask nm] pub
arp [-vnD] [-H type] [-i if] -f [filename]

4.mtr :

This is a network diagnostic tool introduced in Fedora – Like traceroute except it gives more network quality and network diagnostic info. Leave running to get real time stats. Reports best and worst round trip times in milliseconds.
mtr IP-address-of-server
mtr domain-name-of-server
To start mtr just type command (default is GUI interface):$ mtr upstream.router.isp.com
$ mtr sl-gw9-nyc-8-0.sprintlink.net
$ mtr -n router-ip
$ mtr gsrmum.vsnl.net.in

5.whois :

This command looks up a domain name in the internic whois database.
whois [ -h host ] identifier
-h host Host which holds the identification information.
identifier Name or host you wish to identify

6.finger :

This command displays the information on a system user.i.e. finger user@host Uses $HOME/.plan and $HOME/.project user files.
Finger example :
adeetkds@celltadm:/home/celltadm $ finger
Login Name TTY Idle When Where
celltadm Pakulla;Joachim;TIDI pts/1 1 Tue 09:09 145.16.229.12
celltadm Pakulla;Joachim;TIDI pts/2 Tue 09:13 145.16.229.12
celltadm Pakulla;Joachim;TIDI pts/3 1:01 Mon 07:56 145.16.229.12
celltadm Pakulla;Joachim;TIDI pts/4 19: Mon 13:44 adeebkbs.mdv.mmo.de
vdwrk510 Wienen;Alfons;+49211 pts/5 1:33 Tue 07:43 145.16.229.12
celltadm Pakulla;Joachim;TIDI pts/6 19: Mon 09:27 145.16.229.12
celltadm Pakulla;Joachim;TIDI pts/7 Tue 09:13 145.16.229.12
adeetkds@celltadm:/home/celltadm $

7.iptables:

It shows -IP firewall administration (Linux kernel 2.6/2.4).
iptables -L
cat /etc/sysconfig/iptables
View the current firewall configuration
View/read the real firewall file
Edit the iptables file:
pico /etc/sysconfig/iptables Edit and Save the firewall iptables file
.# This firewall is an example of a Linux web, ftp, pop3 & smtp server
.# It also limits ssh access to a block of IP - you need to customize the IPs to match your allowed IPs for ssh access
*filter
:INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [131962:7397220] :inputf - [0:0] -A INPUT -j inputf
-A FORWARD -j inputf
-A inputf -i lo -j ACCEPT
-A inputf -m state --state RELATED,ESTABLISHED -j ACCEPT
-A inputf -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A inputf -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
-A inputf -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT
-A inputf -p tcp -m state --state NEW -m tcp --dport 25 -j ACCEPT
-A inputf -p tcp -m state --state NEW -m tcp --dport 110 -j ACCEPT
-A inputf -s 192.168.1.0/24 -p tcp -m tcp --dport 22 -j ACCEPT
-A inputf -p ipv6-crypt -j ACCEPT
-A inputf -p ipv6-auth -j ACCEPT
-A inputf -j REJECT --reject-with icmp-host-prohibited
COMMIT
shutdown -r now Restart the server
If you know these commands in Linux you are going to have a good time networking ;) .

Beginners Guide to know the network in Linux command line [Part 1]


Beginners Guide to know the network in Linux command line [Part 1]

Linux is the most powerful operating system and we often need to use certain commands to explore its power effectively and efficiently. So Today we summarized the most interesting and useful networking commands which every Linux user are supposed to be familiar with it so that we can do our work faster.But Before we get to know the commands let’s have a look at some Important files in our OS.

Important files in Linux TCP/IP Network Configuration:

1./etc/resolve.conf : This file Lists the DNS servers for internet domain name resolution.
2./etc/hosts : This file lists the hosts to be resolved locally and not by the DNS.
3./etc/nsswitch.conf :This file lists the order of host name search. Typically look at local files, then NIS server, then DNS server.
4.Red Hat/Fedora/CentOS might have a /etc/sysconfig/network file to specify the network configuration.
5.Red Hat/Fedora/CentOS might also have a /etc/sysconfig/network-scripts/ifcfg-device file which specifies TCP network information.
6./network/interfaces/:And our favourite Ubuntu/Debian must have a etc/network/interfaces file to specify network configuration and devices.For EXample Static IP and DHCP etc.

Some Useful Linux Networking Commands:

1.Ifconfig : We type if config into the terminal to determine whether the network been detected or not.Ifconfig is used to configure the network interfaces.Normally we use this command to check the IP address assigned to the system.It is used at boot time to set up interfaces as necessary. After that, it is usually only needed when debugging or when system tuning is needed.
Example 1. Display Details of All interfaces Including Disabled Interfaces
# ifconfig -a
Example 2.Disable an Interface
# ifconfig MIG down
Example 3.Enable an Interface
# ifconfig MIG up
Example 4.Assign ip-address to an Interface
Assign 192.168.2.1 as the IP address for the interface MIG.
# ifconfig MIG 192.168.2.1

2.netstat : Display connections, routing tables, stats etc.
List externally connected processes: netstat -punta
-a: Show both listening and non-listening sockets.
-p: Show PID of process owning socket
-u: Show UDP
-t: Show TCP
-n: Show IP addresses only. Don’t resolve host names
-g: Show multi-cast group membership info
-c: Continuous mode – update info every second
-v: Verbose
-e: Extended information
-o: show network timer information
examples :
netstat -nap : List all connected processes.
netstat -s : Show network statistics.
netstat -rn : Display routing table info.
Example 1: List all ports using netstat -a
# netstat -a | more 
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost:30035         *:*                     LISTEN
udp        0      0 *:bootpc                *:*                                

Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     6135     /tmp/.X11-unix/X0
unix  2      [ ACC ]     STREAM     LISTENING     5140     /var/run/acpid.socket
Example 2: List all tcp ports using netstat -at
# netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost:30037         *:*                     LISTEN
tcp        0      0 localhost:ipp           *:*                     LISTEN
tcp        0      0 *:smtp                  *:*                     LISTEN
tcp6       0      0 localhost:ipp           [::]:*                  LISTEN
Example 3: List all udp ports using netstat -au
# netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
udp        0      0 *:bootpc                *:*
udp        0      0 *:49115                 *:*
udp        0      0 *:mdns                  *:*
3.ping : This command sends the ICMP ECHO_REQUEST packets to the network hosts. We Use Cntl-C to stop the ping command. ping command is used to check the connectivity of a system to a network.Whenever there is a problem in network connectivity we use ping to ensure the system is connected to network.
Example:Test the connection to server yahoo.com server, enter:
ping yahoo.com
Sample outputs:
PING yahoo.com (209.191.93.53) 56(84) bytes of data.
64 bytes from b1.www.vip.mud.yahoo.com (209.191.93.53): icmp_seq=1 ttl=50 time=311 ms
64 bytes from b1.www.vip.mud.yahoo.com (209.191.93.53): icmp_seq=2 ttl=50 time=310 ms
64 bytes from b1.www.vip.mud.yahoo.com (209.191.93.53): icmp_seq=3 ttl=50 time=309 ms
64 bytes from b1.www.vip.mud.yahoo.com (209.191.93.53): icmp_seq=4 ttl=50 time=310 ms
^C
--- yahoo.com ping statistics ---
5 packets transmitted, 4 received, 20% packet loss, time 4007ms
rtt min/avg/max/mdev = 309.710/310.718/311.594/0.966 ms
Lets do networking at Linux with ease now.

Monday, July 23, 2012

File/Directory System in Ubuntu

File/Directory System in Ubuntu

File/Directory System in Ubuntu : In Ubuntu all filesystems are contained within one directory hierarchy. The root directory is the top level directory, and all its sub directories make up the directory hierarchy. This Directory differs to other operating systems such as MS-Windows or may be even Mac which applies a separate hierarchy for each device and partition. So lets Try to understand what are all the important Directories and File systems which you might get by default.This Directory is actually very important to know because if we know them , we can certainly be the quickest among our colleagues.

The Most Important Directories are as follows :

/lib : This Directory has the Libraries , These are the system libraries similar to Program Files commonly we call them libraries.
/bin : This Directory has the binary executable files what we usually call as binaries.
/boot : This Directory has the files required to boot such as the kernel etc.
/dev : This Directory has devices everything from drives to displays like Disc drivers.
/etc : This Directory has configuration files for your system.
/home : This Directory has locally stored user files and folders.

The usual Directories for Daily use are as follows :

/lost+found : This Directory has lost and found for lost files.
/media : This Directory has mounted or loaded devices such as cdroms, digital cameras, etc.
/mnt : This Directory has mounted file systems.
/opt : This Directory has location for “optionally” installed programs.
/proc : This Directory has dynamic directory including information about and listing of processes.
/root : This Directory has the “home” folder for the root user
/sbin : This Directory has system-only binaries.
/sys : This Directory contains information about the system
/tmp : This Directory has temporary files
/usr : This Directory has applications mainly for regular users.
/var : This Directory has mainly logs, databases, etc.

Some Important Files are as follows  :

/etc/profile.d : This Directory contains scripts that are run by /etc/profile upon login.You can put the scripts here which can be run at every startup.
/etc/rc.d : This Directory has contains a number of shell scripts that are run on boot up at different run levels. There is also typically an rc.inet1 script to set up networking , an rc.modules script to load modular device drivers, and an rc.local script that can be edited to run commands desired by the administrator, along the lines of autoexec.bat in DOS.Normally we have all the scripts which we want to run on the boot time like we want to run a health checkup scripts in our production servers just at the log in.
/etc/rc.d/init.d : This Directory contains most of the initialization scripts themselves on an rpm-based system.
/etc/rc.d/rc*.d : Now where “*” is a number corresponding to the default run level. Contains files for services to be started and stopped at that run level. On rpm-based systems, these files are symbolic links to the initialization scripts themselves, which are in /etc/rc.d/init.d.
/etc/skel : This Directory directory containing several example or skeleton initialization shells. Often contains sub directories and files used to populate a new user’s home directory.
/etc/X11 : This Directory configuration files for the X Window system
/usr/local/bin : This Directory the place to put your own programs. They will not be overwritten with upgrades.
/usr/share/doc : This Directory has all the documentation.
Enjoy Playing with Files and directories.

Saturday, July 21, 2012

Tutorial on Time Stamps in Unix


Tutorial on Time Stamps in Unix

Unix Tips By Rahul Miglani
Unix Tips By Rahul Miglani
As a beginner we must know that , we have 3 different time stamps in Unix. Each file has three time stamps, which record the last time that certain operations were performed on the file. You can search for files whose time stamps are within a certain age range or compare them to other time stamps.

1: Modify Time:

This is last time when the actual contents of the file were modified.
We can get the list of files modified n days using find command.
find . –type f –mtime +n -exec ls –l {} \;
Where:
-n : files modified between today to n-1 days.
n : files that was modified on that particular n day.
+n : files modified from n+1 days back .
Needless to say that we get modified time of file using ls –lt command 
Another example would be : Finding 15 Days old files
find *.* -mtime -15
You can play with these files now : Deleting these 15 Days old files
find *.* -mtime -15 -exec rm -rf {}\;

2: Change time:

Change time is something like altering the label of the package (file) whereas modify time is altering the content of the file. So in Unix we can say, change time is last time change in inode of the file and inodechanges when we update the file, change permission, rename file, change in owner etc.
So whenever mtime change, ctime also does (change in content of file also update inode) but ctime change some extra time as explained above.
We use –ctime in find to get list of files changed in particular days back. ls –lc command is used to get the change time of a particular file.
Here file was zipped (renamed), means inode information of the file has been modified.
username@servername:username> ls -lt TestScript.sh.gz
-rwxrwxr-x 1 username aimsys 581 Dec 16 14:18 TestScript.sh.gz
username@servername:username> ls -lc TestScript.sh.gz
-rwxrwxr-x 1 username aimsys 581 Jan 25 07:29 TestScript.sh.gz
Here ls –lc gives the time when the file was zipped.
Another example would be the use of find command with ctime
find *.* -ctime -15
or may be you want to delete these files.
find *.* -ctime -15 -exec rm -rf {}\;

3: Access Time:

It’s a time the file was last accessed means the file was read. Last time we read that file.
username@servername:username> ls -lu TestScript.sh.gz
-rwxrwxr-x 1 username aimsys 581 Jan 25 07:43 TestScript.sh.gz
Example: Finding files which were accessed with in 15 days.
find *.* -atime -15
You can play with these files now : Deleting these 15 Days old files which were accessed (Not Modified)
find *.* -atime -15 -exec rm -rf {}\;
NOTE: With find you can choose to type -print or not with the find command, because the basic principle of find is obviously to print what ever it is doing so you may choose to type -print .To know more about the find command you can type man find on unix, to know what has been written about the find in Unix.

Summary :

Access Time (atime ) : This is the time that the file was last accessed, read or written to.
Modify Time (mtime ) :This is the last time the actual contents of the file were last modified.
Change Time (ctime ):This is the time that the inode information (permissions, name, etc., the metadata, as it were) was last modified.
Enjoying Playing with the times in Unix.

Tutorial on Parameter Substitution at Runtime


Tutorial on Parameter Substitution at Runtime

Today, we are presenting a very simple & useful thing i.e. Parameter Substitution in Unix at run-time.We must know that mainly there are four types of parameter substitutions which are commonly used in Unix Scripts.The Bourne shell has a handy set of operators for testing and setting shell variables.These are presented below with some quick examples.This will run in almost all the shells but as we all know exceptions are always there , You can use parameter substitution operators in any command line. You’ll see them used with the colon ( : ) operator , checking or setting default valuesLet us know see what are the types of parameter substations
1.${parameter:-value}
This Type of parameter substitution is used if the value is defined already for the parameter,So now it will use that value otherwise use the above one.Means if Variable is undefined or assigned a null string it must get the default value :For Example lets have a scenario where we have ${EDITOR:-/bin/vi}
In this case it will use the default EDITOR value if it defined otherwise it will use vi.We can use this feature in a program which prompts for a file name and use default value if the user simply press enter
echo “Enter file Name : \c”
read filename
f_name=${filename:-emp.lst}
2.${parameter:+ value}
This one will substitute the value if parameter is defined otherwise it will substitute nothing. (just opposite of – option).It simply override the value of parameter, if parameter is null, it will never override.
OPT_MODE=P
echo “Mode : ${ OPT_MODE:+T}”
output : Mode : T
As it was defined it will override its value.This feature can be used to set a variable to the output of a command and echo a message if the variable is no null.
found_file=`ls *log`
echo ${found:+”There are log”}
3.${parameter:=value}
It works similarly for (- option) except that it goes further and makes the assignment to the variable that is evaluated.With = option , we can use parameter substitution with a command without making intermediate assignment.
echo “enter file name : \c”
read filename
grep $pattern ${filename:=emp.lst} # filename is now assigned
X=1; while [ $x –le 10 ] can be combined with while [ ${x:=1} –le 10 ]
4.${parameter : ?value}
If the parameter is not assigned and null , it echoes value and kill the shell.This is quite useful in terminating script if user fails to respond properly to shell directive.
echo “Enter the filename: \c”
read filename
grep <pattern> ${filename:? “No file entered”}
If no file name is entered then message will be displayed and script is also aborted the use of explicit exit command.
NOTE : If you omit the colon ( : ) from the expressions , the shell doesn’t check for an empty parameter. In other words, the substitution will happen whenever the parameter is set. (That’s how some early Bourne shells work: they don’t understand a colon in parameter substitution.).The first substitution ( ${Value=default} ) will leave $Value empty because the variable has been set. The second substitution will set $Value to default because the variable has been set but is empty. The third substitution will leave $Value2set to stuff :
+Value=
Value2=stuff
: ${Value=default}
: ${Value:=default}
: ${Value2:=default}

Tutorial on External System Calls from PERL or TCL/TK Program


Tutorial on External System Calls from PERL or TCL/TK Program

Tutorial on External System Calls from PERL or TCL/TK Program
There are several ways to call external system calls from inside the PERL or TCL/TK Program, we use many of these in several of our Integration Line Areas such as SCHAPI Software, Perl Programs etc.
1: Using System function : We use system function when we don’t want to capture the output of the command run, we can only get to know that whether the command succeeded or not.Ex: From our JOB where it just triggers the external program and doesn’t capture the output of PROGRAM.
system ‘ls -l $HOME’;
if ($RUN_TYPE eq “I” || $RUN_TYPE eq “S” ) {
if ( $RUN_TYPE eq “S” ){
system(“perl D:\\scmwrk82\\J2EEServer\\config\\CRM\\Poller_Libra\\ShutDownPoller.pl ‘E’”);
}
else {
system(“perl D:\\scmwrk82\\J2EEServer\\config\\CRM\\Poller_Libra\\ShutDownPoller.pl ‘I’”);
}
}
2: exec
It is same as system function except that it does not start child process, The exec function causes the Perl process itself to perform the requested action.Ex: From our Script to call our external script of Unix.
button .frame_bottom.ok \
-relief raised \
-text “Yes” -font $MediumFont -fg red \
-borderwidth 2 \
-command { exec /opt/gemini/prj/oper/bin/cleaning_maestro.sh &
exit }
3: backticks
We want to capture the output of the command,
Like : my $now = `date`; # grab the output of date
print “The time is now $now”;
Backquotes in list context , If the output from a command has multiple lines, the scalar use of backquotes returns it as a single long string containing newline characters. However, using the same backquoted string in a list context yields a list containing one line of output per element.
my $who_text = `who`; ( stores the output in singe long string)
my @who_lines = `who`; (stores the output in list context)
4: Using open function ( process as Filehandles)
When we want to pipe the command (as input or output) to the script.There are 2 ways to do it :
a : capture the data of a command (syntax: open(“command |”))
b: feed an external command with data generated from the Perl script (syntax: open(“| command”))
open DATE, “date|” or die “cannot pipe from date: $!”;
here we are trying to run the date command and piping its output to DATE file handle
now we can read the output : my $now = <DATE>;
open MAIL, “|mail merlyn” or die “cannot pipe to mail: $!”;
Here we are trying to send data to the mail process,
print MAIL “The time is now $now”; # presume $now ends in newline;
close MAIL;
die “mail: nonzero exit of $?” if $?;
Now one small question arises ,why do we use processes as filehandles? (when the same task can be done using backquotes ? , Well, it’s the only easy way to write to a process, based on the results of a computation. If you’re only reading, backquotes can be easier , to manage unless you want to have the results as they come in.Example :
open F, “find / -atime +90 -size +1000 -print|” or die “fork: $!”;
while (<F>) {
chomp;
printf “%s size %dK last accessed on %s\n”,
$_, (1023 + -s $_)/1024, -A $_;
}
The find command is looking for all the files not accessed within the past 90 days and larger than 1,000 blocks. While find is searching, Perl can wait. As each file is found, Perl responds to the incoming name and displays some information about that file for further research.Had this been written with backquotes, we wouldn’t have seen any output until the find command had finished. It’s comforting to see that it’s actually doing the job before it’s done.

Tutorial on TRAP in Unix


Tutorial on TRAP in Unix

Some times in Unix we want to prevent our script from abnormal conditions and interrupts, this can be handled using the trap command in Unix.When you press the Ctrl+C or Break key at your terminal during execution of a shell program, normally that program is immediately terminated, and your command prompt returned. This may not always be desirable. For instance, you may end up leaving a bunch of temporary files that won’t get cleaned up.
Trapping these signals is quite easy, and the trap command has the following syntax:
Format: trap command signal
Trap enables signals handling ,signal are sent to running process to indicate that an event (external to the process) has occurred that the process must respond,Now, trap catches a signal sent to the process, and take action on the signal using the action defined in the trap command, Instead of letting the signal take it’s default effect on the process.Command would be one or more command that will be executed (separated by colon) when any of specified signal received.
Signal: The signal we want to trap , we can specify more than one signal here, it can be both numeric or string (signal name) format as described below.
0 EXIT Exit
1 HUP Hang-up
2 INT Interrupt (like ctrl +c)
15 TERM Software termination signal
While scripting we might get confuse where we put our trap command, trap command executed whenever one of the traps on its list occurs,This means that our shell needs to know in advance what to do about a signal before a signal occurs.This advice that we have our trap command before any otherexecutable command, In this case the shell will be able to know which signal to send our trap command.
Like : trap 1………
Command 1
Command 2
Command 3
trap 2 ………………
Command 4
Command 5
Command 6
Here, trap 1 will be executed for command 1,2,3, and trap 2 will be executed for command 4,5,6.
Now we can define multiple traps in our program 
Usage of trap : Suppose we want to prevent our program from hang-up and interrupt signals.trap ‘echo “you program interrupted”;rm /tmp/daemons1.flg;exit 1’ 1 2 .Here it will echo the msg, remove some log or flag file according to our requirement and then exit, once it receive 1 or 2 signal,Sometimes we might want to ignore certain signals while performing some operation that we don’t want interrupted,
trap ‘ ‘ 1 2
Here we are ignoring 1 and 2 signal by providing empty command in trap.We can also defined function in trap, if we have much command to execute,trap ‘function_name;exit1’ INT HUP
Change the trap result back for signals We can change the default action of a signal, like trap 1 2
NOTE : If you ignore a signal, all subshells also ignore that signal. However, if you specify an action to be taken on receipt of a signal, all subshells will still take the default action on receipt of that signal.The commands specified to trap must be enclosed in quotes if they contain more than one command. Also note that the shell scans the command line at the time that the trap command gets executed and also again when one of the listed signals is received.