Showing posts with label *NIX. Show all posts
Showing posts with label *NIX. Show all posts

*NIX utilities

1) You are in a directory lets say "/home/xyz" and you want to find some file lets say "abc.txt" and open it using vi. We normally do

# find /home/xyz -name abc.txt
# vi /abc.txt

Simply you can do :
# find /home/xyz -name abc.txt -exec vi {} +

You can make an alias of above command and it in your profile file.

But if you have more then one abc.txt file and you want to open only one of them, you can use the following script.

# vi fvi

################################################################
#small script that finds the file given at command line and vi it
#needs improvement
if [ $# -ne 1 ]
then
echo "Usage: $0 "
exit 1
fi
result=`find . -name $1`
file=`echo $result | awk '{print $1}'`
if [ -n "$file" ]
then
vi $file
else
echo "$file not found"
fi
################################################################

How to use it
1) Copy the script to some folder where everybody can access it, like "/usr/bin/"
2) chmod 755 /usr/bin/fvi

Let say you want to find and vi abc.txt from /home/xyz
# cd /home/xyz
# fvi abc.txt

date command in *NIX

How to get yesterday / past / future dates in *NIX

Ref

http://www.cyberciti.biz/tips/linux-unix-get-yesterdays-tomorrows-date.html

Get yesterday date

1)

YESTERDAY=`TZ=aaa24 date +%Y%m%d`

echo $YESTERDAY


2)

in GNU date:

date --date=yesterday

Shell Scripting

1) for loop
1.1) Simple for loop
# for i in `seq 1 3`
> do
> echo "i is $i"
> done
i is 1
i is 2
i is 3


# for i in 192.168.0.{1..5}
> do
> echo "i is $i"
> done
i is 192.168.0.1
i is 192.168.0.2
i is 192.168.0.3
i is 192.168.0.4
i is 192.168.0.5

# ls
dir1 dir2 file1 file2
# for i in `ls`
> do
> echo "i is $i"
> done
i is dir1
i is dir2
i is file1
i is file2

2) set command
# date
Sun Apr 12 22:41:37 IST 2009

# set `date`
# echo $#
6
# echo $1
Sun
# echo $2
Apr
# echo $3
12
# echo $4
22:41:37
# echo $5
IST
# echo $6
2009

Threads - Solaris

Counting number of threads a process has :
#ps -eLf | grep
or
#prstat
(it shows the NLWP, i.e. number of light weight processes)

*NIX Sites

http://www.unix-manuals.com/

Use gnome-terminal to automate (Fedora)

Suppose you have to run many applications on terminal (lets say gnome-terminal) and you want to automate this process, You can use command lines options of gnome-terminal.
Example
#gnome-terminal --geometry=80x20+700+0 --title=ping -x ping google.com &
where geometry will decide what will be length and width of your terminal and what will be the position of your terminal.
title sets the title of the terminal
-x is for the command to run
& to run this as background process.
(See man pages of gnome-terminal for more options, you can have tabs and many more options)

Now lets say we want to run following applications viz
1) my server lets say the name is /home/xyz/myServer.exe
2) tail -f /var/log/messages to see the log
3) ping google.com
4) ssh to some remote machine lets say xx.yy.zz.aa

So I will make a script say
automate.sh with following
#cat automate.sh
gnome-terminal --geometry=80x20+0+0 --title=myServer -x ./home/xyz/myServer.exe &
gnome-terminal --geometry=80x20+0+400 --title=logMessages -x tail -f /var/log/messages &
gnome-terminal --geometry=80x20+700+400 --title=ping -x ping google.com &
gnome-terminal --geometry=80x20+700+0 --title=remoteMachine -x ssh root@xx.yy.zz.aa &

Now you can run all the applications with a simple command
#sh automate.sh

Please remember that the opened window will close as soon as the command it is running stops executing, lets say the window/terminal running "tail -f /var/log/messages" will immediately go if you dont have permission to read /var/log/messages

Linux Desktop Shortcut Keys

General Shortcut Keys

Alt + F1 - Opens the Applicantions Menu
Alt + F2 - Displays the Run Application dialog
Print - Screen Takes a screenshot
Alt + Print - Screen Takes a screenshot of the window that has focus
Ctrl + Alt + right arrow - Switches to the workspace to the right of the current workspace
Ctrl + Alt + left arrow - Switches to the workspace to the left of the current workspace
Ctrl + Alt + up arrow - Switches to the workspace above the current workspace
Ctrl + Alt + down arrow - Switches to the workspace below the current workspace
Ctrl + Alt + d - Minimizes all windows, and gives focus to the desktop
F1 - Starts the online help browser, and displays appropriate online Help

find command

Using "find" command in *NIX systems

1) To find a file say "hello.c" in /usr/xyz directory, do
#find /usr/xyz -name "hello.c"

If you dont know the exact location, you can search in "/" dir, and if you dont know the exact file name, you can use "*" like
#find / -name "he*.c" or
#find / -name "he*.c*"

2) To find a string in some specific files, lets say you want to find "some function" in all C++ files (having extension ".cpp") in, lets say "/usr/xyz/prj1" directory,

#find /usr/xyz/prj1 -name "*.cpp" | xargs grep "some function"

3) You want to do something with the result of find command, eg you want to see listing in time order (ls -ltr) of the output of find command
#find . -name "a*.pdf" -exec ls -ltr {} +
Here we will be seeing all the pdf files that start with "a" in reverse time wise list (ls -ltr)

or lets say you want to delete all the files with .tmp extension in /mytmp dir, you can do so by
#find /mytmp -name "*.tmp" -exec rm {} +

What is free software

What is "Free software Movement" as conceived in 1983 by Richard Stallman.

To define it from their own language its free as in "free speech," not as in "free beer."

Meaning if you have got a program, lets say a game from some source, for free, but you dont have the freedom to see the code, modify the game and redistribute it your own way, its not "free software".

So in summary, Free software is a matter of freedom, not price.

Read on, to find more ....

Free software Definition

Remote Desktop in Linux

Ever need to access windows machine from your fedora or other Linux system, Use Remote Desktop

In Fedora

install it using

#yum install rdesktop

On other systems, find the rpm and install the same

and you are ready to connect to remote system, to do so use

#rdesktop (IP address of remote machine)

You can also use vnc for this

Linux tips for newbies

1) Dont remember the command
#apropos "list directory"

2) use tab to complete the type, dont type all the words

3) to goto last dir
#cd -

4) to goto home dir
#cd ~

5) want to find that file
#locate filename

6) for tips on VI editor, see VI post

7) report file system disk space usage
#df -kh

8) to get/set hard disk parameters
#hdparm -t /dev/sda5
replace /dev/sda5 with the name of your disk

9) Use seq command to generate sequences
# seq 1 5
output will be
1
2
3
4
5
This you can use in many places including scripts, eg
# for i in `seq 1 5`; do echo "i is $i" ; done
output will be
i is 1
i is 2
i is 3
i is 4
i is 5

10) Sending mail through linux command line
First check, whether sendmail is running

#/etc/init.d/sendmail status
if not run it via (you need to be root for this)
#/etc/init.d/sendmail start

then, send mails using either mail or mutt command as below:

# echo "body of the mail" | mail -s "subject of the mail" toAddress
Give recipient's mail id in place of toAddress

As for body of the mail, you can also redirect from a file, like
# mail -s "subject of the mail" toaddress < body_mail.txt

if you want to send file as attachment, you can use mutt instead
# echo "body of the mail" | mutt -s "subject of the mail" -a fileToAttach.txt toAddress
Give recipient's mail id in place of toAddress

Practical SNMP

Ref . http://net-snmp.sourceforge.net/
essential-snmp-second-edition

Quick Start SNMP using NET-SNMP on Linux

This document helps you to quick start snmp using NET-SNMP

1) Download net snmp

2) untar the file using
# tar -xvf net-snmp-5.4.1.tar.gz

3) #cd
here
#cd net-snmp-5.4.1

4) configure and make the package with, read INSTALL and README file for more details
#./configure // you have options for configure, read INSTALL, README file
#make
#make install

5) configure your agent by changing /usr/local/share/snmp/snmpd.conf
( Plz. note that the path can be different, as configured by you)

6) Thats it, check your configuration by running snmp agent and manager by
#snmpd
#snmpget -v1 -Cf -c public localhost system.sysContact.0
( note : v1 for version 1, see man pages for ohter options)



*NIX Networking Tools

Ref : wikipedia, essential-snmp-second-edition
These commands are just an overview, see man pages for more options.

traceroute
to determine the route taken by packets across an IP network
#traceroute www.elitecore.com

nslookup/dig
to get IP address information on a host, and vice versa. dig stands for Domain Internet Groper, nslookup is now deprecated, dig –x is used to get reverse lookup
# nslookup www.google.com
#dig -x ip address
gets u reverse name lookup

whois
to obtain domain name registrar information
#whois www.elitecore.com

Ethereal
Besides GUI tool, we can use the same as command line utility also. It is used to capture network traffic e.g, to get network traffic on some port say 161 (SNMP Port) :
#tethereal -i lo -V -F libpcap -f "port 161"

ifconfig
To get machine's network configuration
#ifconfig


arp
Address Resolution Protocol - method for finding a host's hardware address when only its network layer address is known
#arp –a

ping
for network troubleshooting – uses ICMP packets
#ping google.com
Unknown Host - DNS Problem
network unreachable - networking problems
Timeout - May be the remote machine is not turned on

*If you want to find out Mac address of any machine, just ping to that machine and see the arp table using arp –a command

arping
use when ping doesnt work, sometimes firewall settings disable ICMP packets, to verify the reachability
we can use arping also
#arping ip-address
(ref. - http://www.linux.com/feature/50596)

netstat
Obtains NETwork STATistics from kernel, can be used to find problems in the network and determine the amount of traffic on the network. It displays network connections, routing tables and network interface statistics
#netstat –rn
(-rn for seeing routing table)
to see all connections and listening ports, use
#netstat –a
(see man page for more info)


using vi's .exrc file

These useful commands are taken from various sources including net, books. Hopefully it will be useful:
(Note : ~/.exrc file : is the file for making permanent settings to your vi editor, if you place the following commands in this file, these commands will be available to all your vi sessions.)
Commands
1) :abb - for abbreviation
eg.

#:abb etl Elitecore Technologies Limited, Ahmedabad
so whenever u type etl and press enter, space or tab, "etl" will be replaced by "Elitecore Technologies Limited, Ahmedabad"

I use it to create template C and C++ programs, like
#:abb CPP #include<iostream>^M using namespace std;^M int main()^M {^M return0;^M }

(here ^M is for new line, NOTE : make sure u dont type ^M, for ENTER u have to
a) ctrl+v
b) press ENTER
it will display like ^M as above but it means ENTER/new line)
So, now every time I need a C++ template, I just type CPP and press enter, space or TAB,
It gives me a nice C++ program template to work on.
You can use the same for similar purpose.


2) :map - is for mapping some command to some shortcut
eg
the vi command :set number is used to show numbers along the lines of the file, lets say we want to give it a short cut, say F2, meaning when we type F2, it should run the command :set number, To map this command to shortcut key, the vi setting will be
#:map #2 :set number^M
(Note : remember ^M comment in above command)
same for :set nonumber (lets say to F3)
#:map #3 :set nonumber^M

3) set tabstop=2
this is for TAB's setting, I want TAB to be equal to 2 spaces, so I set it to 2.


vi quick reference

Books I like

  • Inside C++ Object Model
  • Unix Network Programming - Stevens
  • Professional-c++-programmer-to-programmer - wrox
  • Beautiful-code-leading-programmers-explain-how-they-think-theory-in-practice