Wednesday, September 17, 2008

TRIMMING THE LOG

Various unix processes canproduce fast growing logs thatsometimes need to be trimmedinstead of deleted, for referenceor troubleshooting. And youlikely have no desire to edit thefiles. Here is a handy kshscript that will quickly trimthe log so it keeps recentinformation and lets you keep asmany lines as you think you mightneed.

I call it trimlog:

#! /bin/sh
# trimlog
filesize=`cat $1wc -l`
trim=`expr $filesize - $2`
if [ $trim -gt 0 ]
then
sed "1,$trim d" $1 > /tmp/$1
mv /tmp/$1 $1
echo $1 trimmed by $trim lines
else
echo "Invalid argument"
fi
Use it by feeding in the name of the log you want to trim followedby the number of lines you want to keep:

# trimlog oracle_listener.log 10000

Of course you can't use it in the /tmp directory.
There are probably newer and more efficient ways to do this, but this works.

kill really does not KILL

kill is not really meant only to kill a process. But mainintention is to send a signalto process. And the processhandles these signals to dovarious tasks.

kill -INT 1234

where 1234 is the Process ID is really, this set an interrupt signal, Ctrl-C is an example of interupt signal givento shell.

kill -HUP 1234

some UNIX daemons handle this, to do refreshing business.

kill -KILL 1234

This is the sure kill,which cannot be handled by any process. Also know as,

kill -9 1234

Just, check the in the file /usr/include/sys/signal.h for all signals on the system or you can use:

kill -l

to get the complete list of signals.

Note: More about this can be found onkill and signal man pages.

AUTOMATIC LOGIN TO REMOTE HOSTS w/o PASSWORD

Consider two hosts.host1.domain.comhost2.domain.com

>From hosts (user:deepak), you wish to logon tohost1 (user:paul).

Paul would create a .rhostsfile in his directory

chmod 744 .rhosts

(No write permission forothers)
The first line would contain

host1.domain.com deepak

Now, deepak should be able to logon to host2.domain.comas user paul by using rlogin.
Deepak will type

e.g.
rlogin hosts.domain.com -l paul

And he should be in.

ELIMINATING MULTIPLE SPACES

Here is a simple script whichhelps in eliminating multiplespaces when one wants toextract a particular columnin a set of records from afile.

Here is an example to provethe same:

ps -u $LOGNAME tr -s " " : cut -d: -f

The above script line can beused to extract a column fromthe output of a ps command for a particular user ($LOGNAME).

Monday, September 15, 2008

CRYPT AN ASCII FILE

An ascii file can be easily encrypted and decrypted.
To encrypt simply pipe the STDOUT ofthe file to "crypt" and redirect it toa new file name. Enter a passowrd whenprompted with "Enter key".

$ cat foo crypt > foo.e
Enter key:

To unencrypt simply pipe the STDOUT ofthe encrypted file to "crpyt" andredirect it to a new file name. Entera passowrd when prompted with"Enter key".
$ cat foo.e crypt > foo.new
Enter key:

Thursday, September 4, 2008

Getting access to your linux box without password

For BSD:
In order to recover your root password, you must boot FreeBSD in single user mode, mount the filesystems read/write and issue the passwd command. The passwd command will not actually recover your root password, but will allow you to change it to something new.

Boot in single user mode
As the operating system is starting, it will display the following message:Hit [Enter] to boot immediately, or any other key for command prompt.
Booting [kernel] in 10 seconds...
You should now press the space bar, and you will see the following message:
Type '?' for a list of commands, or 'help' for more detailed help.ok>
Now type boot -s and press the enter key to start FreeBSD in single user mode.
ok> boot -s
After the system boots, you should see the statement:
Enter full pathname of shell or RETURN for /bin/sh:
Press the enter key and you will have a # prompt and you can change the password using passwod command

For RH Linux
Booting Single-User Mode
You may be able to boot single-user mode directly. If your system boots, but does not allow you to log in when it has completed booting, try single-user mode.
If you are using GRUB, use the following steps to boot into single-user mode:
If you have a GRUB password configured, type p and enter the password.
Select Red Hat Linux with the version of the kernel that you wish to boot and type e for edit. You will be presented with a list of items in the configuration file for the title you just selected.
Select the line that starts with kernel and type e to edit the line.
Go to the end of the line and type single as a separate word (press the [Spacebar] and then type single). Press [Enter] to exit edit mode.
Back at the GRUB screen, type b to boot into single user mode.
After boot, you can change the password of root using passwd command

CASE TRANSLATION

If you have occasion whereyou need to translate uppercase to lower and viceversa, and happen to run aksh, enter the following functions into your .bash_profile:

trans ()
{
tr '[:upper:]' '[:lower:]' < $1 > $2
}
TRANS()
{
tr '[:lower:]' '[:upper:]' <$1 > $2
}
The first function trans will translate all uppercaseletters in the first file(argument) to lowercase in the second file.
The second function TRANS does the exact opposite.

Tip within unix

tip is a unix utility for establishing a terminal connection to a remote system via a modem

The tip command line options are as follows tip [-v] [-speed-entry] {hostname phone-number device}

A simple example is a script that establishes a terminal session:
spawn tip modem
expect "connected"
send "ATD$argc\r"
set timeout 30
expect "CONNECT"

Friday, August 1, 2008

Tracking User's activity using "script -a"

Unix System provides a very good method allows us to capture of all commands issued by the user “XYZ”. For this, we can user "script" command. This will print all what appears on the screen (commands and their output) to the filename

But remember, Its not a User History.... It stores all commands entered by all users (.e. its the shell history .

Add the following line to your BASH profile

script -a ~/.termlog`date +%m%d%y`

This enable to capture all the terminal activities to a file ending with current date

Monday, July 28, 2008

Cut and paste text files by columns

How many times have you wanted to edit those results files columnwise?

Here’s a generic UNIX way: (startcol and endcol refer to character columns, including whitespace. Tab counts as 1.)

cut -cstartcol-endcol file {>receiving file} paste sourcefile1 sourcefile2 {>final file}

I like to preview and fine-tune my commands by looking at the result on standard out (the shell window) and then redirecting output to a (temporary) file when things look right. Those feeling really adventurous can combine things into a single command line…

Great Unix Tips & Tricks: Vol 1

Whether you are a newbie to Linux or you are a seasoned guru, you are bound to find some scripts or programs which just make your life on the computer exponentially easier. The following is a list of them and explains what they do.

1. tac

tac is a command similar to cat, except that it reverses the output of the specified file.

tac

2. Multiple X-Sessions

Using multiple X-Sessions allows a user to have open more than just one instance of XFree, so multiple desktop environments can be used at a time.

startx -- :

terminal name is simply which terminal X should start on. By default it starts on

0:0. If you want two open, you may want it open on 0:1, which can be run by:

startx -- :1

The resulting desktops will exist on F7 - F12.

3. links

links is a text based browser which has full support for tables

links

is simply the hostname or IP address of the computer to connect to.

sparse files

Due to the Unix concept of sparse files, you can create seemingly enormous files that in reality take next to no disk space. The following program will create a 305,419,897 byte file called 'core' that may result in you receiving a 'cleanup' email message from a less-than-seasoned Unix sysadmin even though it occupies virtually no real disk space.

    $ cat bigcore.c
#include
#include

int main(void) {
int fd = open("core", O_CREAT|O_WRONLY|O_TRUNC, 0600);
lseek(fd, 0x12345678, SEEK_SET);
write(fd, "1", 1);
close(fd);
return 0;
}

$ cc -o bigcore bigcore.c
$ ./bigcore
$ ls -l core
-rw------- 1 dmr staff 305419897 May 1 03:50 core
$ du -k core
48 core
$

Tuesday, July 15, 2008

Ctrl+D or exit: Which one is better?

In BASH, to exit from shell we can use Ctrl+D or exit ? which one is better?

-Ctrl+D conveniently exits Bash
-Sometimes too conveniently bcoz session may be terminated if pressed accidentaly.

To avoid the accidental exit, we can specify the no of "Ctrl+D" pressed before exit.

$export IGNOREEOF=1

$ Ctrl+D
$ Use "exit" to leave the shell.
$ Ctrl+D
$ exit

Wednesday, July 9, 2008

Games

If you like to use Linux and only Linux, and you also like to play games on your computer, the best choice is to buy native Linux games (games that run without any intermediate layer on your Linux system). You can find lots of information about available games and even order them through the links below.

Naturally there are also lots of free games, many simple but fun games are included with Linux distributions, but some are not, so I have included links about those too.

Linux Game Publishing
Tux Games
Linux Gaming
Linuxgames
The Linux Game
Mandrake eXPerience FREE GAMES section
Article on Gaming and Linux in 2003

Tuesday, July 8, 2008

Command based Calculator in BASH

Bash can perform simple arithmetic operations at the command prompt.

To use this feature, simply type in the arithmetic expression you wish to evaluate at the prompt within double parentheses, as illustrated below. Bash will attempt to perform the calculation and return the answer.


bash> echo $((16/2))
8


bash> echo $((16*2))
32

Open source I use and recommend

I'm always happy to put in a plug for the tools I use every day. With the exception of the Anti-Spyware category, all of the following software is not only free, but open source as well. You might have heard of, or even use, most of these programs already, but give the list a scan for any hidden gems you might not have encountered before.

Virtual Desktop
Organize your applications over several virtual Windows desktops. In this "once you've tried it, you can't live without it" category, we have two strong contenders: VirtuaWin and Virtual Dimension. The latter application has some extra features, but it's a virtual (sorry) tie: you can't lose trying either one of them. [Windows Only]

File Archiver
7-Zip can create ZIP, GZIP, BZIP2 and TAR archives, and can unpack RAR, CAB, even your old LZH and ARJ files. And the Windows version integrates into Windows Explorer. What's not to like?

Server Operating System
I am always happy to put in a word for my favourite Unix server operating system, FreeBSD. While GNU/Linux may have better support for bleeding-edge hardware, I find FreeBSD more stable, scalable, secure and usable. When you consider all the various Linux distributions out there, the fact that there is one FreeBSD is, frankly, less confusing. The excellent ports system and the fact that FreeBSD can run Linux binaries are icing on the cake.

X Server
If you need to run a particular X application on Windows, or if you prefer xterm to putty, you need Cygwin/X. It's a large download, and there are decent commercial alternatives in this category, but this is my pick. [Windows Only]

Web Browser and Email
Yes, Firefox is slowly but surely winning the browser war, so I don't need to plug it a whole lot. Thunderbird will probably do the same thing to Outlook eventually, once more people try this full-featured (spell-checking, address book, spam filter, etc.) email client that provides the same slick email interface whether you're running Linux, Windows or OS X. Highly recommended.

Database
Which one is best: MySQL or PostgreSQL? The debate rages on, but the two have recently come much closer in terms of capabilities, with MySQL playing catch-up in terms of features (stored procedures, triggers, views, etc.). It's getting tougher and tougher to pick a winner, but I think they each have their strengths and appropriate place, and quite honestly, I'm as happy as a clam using either one.

SFTP Client
If you need to securely transfer files between a Windows client and a remote computer running SSH, and you want a GUI interface, FileZilla will make your life easier. [Windows Only]

Text Editor
If you know what a text editor is, you probably already have a favourite, and if past experience is any guide, you probably will use it exclusively the rest of your days. The one they will pry from my cold, dead fingers is Vim.

Terminal Emulator
If you work on Windows, but need to access remote Unix accounts, how can you not already love PuTTY? If only they would finish the Mac OS X port, I could die happy. Until that day, I'll use iTerm on OS X, if only for the tabbed windows. In conjunction with either of these terminal emulators, I could not work without GNU Screen, a console window manager that allows you to run interactive shells in multiple virtual terminals.

Argument of the previous command

To use the last argument of the previous command line as an argument, use $_.

For example:
$ ls -l /home/sanju/somefile
$ vi $_

It will open /usr/home/somefile using vi editor.

Core dump information

To determine what program dumped an anonymous core file, use gdb:
----------
$ gdb -core core
Core was generated by `a.out'.
Program terminated with signal 6, Abort trap.
----------

If you are on a system without gdb, try the file command:
----------
$ file core
core: ELF 32-bit LSB core file of 'a.out' (signal 6)
$

----------

Wednesday, July 2, 2008

Limiting File Size in UNIX

C shell
limit filesize 2m

Korn/Bash shell

ulimit -f max-size
ex: Korn/Bash shell limit 2 MB.
ulimit
-f 2000

Removing non-consecutive duplicate lines from a file

The uniq command will "Discard all but one of successive identical lines" from a file or input stream.

In order to remove non-consecutive duplicate lines, use awk:

awk '!x[$0]++' FILE

A list of directories

To see a list of subdirectories of the current directory:

printf "%s\n" */

With a Bourne shell:

echo */.

The Easy PATH

To format one's PATH variable for easy viewing, try this function:
==============

path()
{
oldIFS=$IFS
IFS=:
printf "%s\n" $PATH
IFS=$oldIFS
}

==============

A typical run of the function:
$ path
/bin
/usr/bin
/usr/bin/X11
/usr/X11R6/bin
/usr/local/bin
/home/sanju/bin
/home/sanju/scripts

Linux: How to Encrypt and decrypt files with a password

To encrypt and decrypt files with a password, use gpg command. It is an encryption and signing tool for Linux/UNIX like operating system such as FreeBSD/Solaris.


GnuPG stands for GNU Privacy Guard and is GNU's tool for secure communication and data storage. It can be used to encrypt data and to create digital signatures. It includes an advanced key management facility.

Encrypting a file in linux
To encrypt single file, use command gpg as follows:

$ gpg -c filename


To encrypt myfinancial.info file, type the command:$ gpg -c myfinancial.info

Output:

Enter passphrase:
Repeat passphrase:


This will create a myfinancial.info.gpg file.

Option:
-c : Encrypt with symmetric cipher

Caution if you ever forgot your password aka passphrase, you cannot recover the data as it use very strong encryption.

Friday, June 27, 2008

Shell Scripting: Using date within report/log file names

When you write a shell scripts you need to create filename with date in it. For example instead of log file name "traces.log", you can create a filename called "traces-Jun-27-8.log".

The date in file will make it easy to find out all logs or reports. You can display the current date and time in the given FORMAT using date command. If you just type date command it will display in standard FORMAT:

$ date
Output:
Fri Jun 27 11:43:25 IST 2008

To display date in MONTH-DAY-YEAR format you need to use date command as follows:

$ date +"%b-%d-%y"

Jun-27-08

As you can see I have used FORMAT as follows

date +"FORMAT"

Where, FORMAT can be any one of the following:

  • %a : Abbreviated weekday name (Sun..Sat)
  • %b : Abbreviated month name (Jan..Dec)
  • %B : Full month name, variable length (January..December)
  • %d : day of month (01..31)
  • %e : day of month, blank padded ( 1..31)
  • %H : 24 hour format (00..23)
  • %I : 12 hour format (01..12)
  • %j : day of year (001..366)

First obtained date:
$ NOW=$(date +"%b-%d-%y")

Create a file with date in filename
$ LOGFILE="log-$NOW.log"

Display filename:
$ echo $LOGFILE

You can use first two commands in script.

Thursday, June 26, 2008

BASH TIP: Script variables

Bash has some useful builtin variables. Here is a list of a few.

$$ : The PID of the script
$? : The exit status of the last command
$# : The number of command line arguments passed to script
$! : The PID of the command that has just been executed (and backgrounded)

Local variable in BASH (Setting a variables for one command only)

If you want to set a variable for one command only, you can always write the variable setting first, immediately followed by the simplecommand (don't write any ; after the assignment). This will result in the variable being set for that command only, but keeping the old value (if it had one) after the command has been executed.

As an example, if you execute the code

MANPATH=/usr/man:/usr/local/man man test

will only look for the manual for test in directories /usr/man and /usr/local/man. This regardless of what value MANPATH had before the call.

Super Bash Tricks

I'll start off this blog with some good bash shell tricks. You'll be the envy of your friends with these up your sleeve.

For the uninitiated, bash is the default shell in many Linux distros, including Fedora, Ubuntu, Redhat etc. If you use a Linux based OS, then chances are that you are using bash. For this reason, I outline below a few common annoyances, and the simple ways to overcome them.

1. Lost bash history

If you have a terminal open and are typing commands, then open another one and use that for a while, the new terminal won’t remember any of the commands typed in the first one. In addition, closing the first terminal, and then the second will overwrite any of the commands typed in the first terminal. Doubly annoying! This happens because the bash history is only saved when you close the terminal, not after each command. To fix it:


Edit your .bashrc (for beginners, any file starting with a . is hidden - they contain user preferences.)

nano ~/.bashrc

No need for a sudo here Ubuntuers, this is your own file, not a system setting. I like nano, but it’s up to you, choose gedit, kate, mousepad, vi or emacs as you wish.add the lines

shopt -s histappend

PROMPT_COMMAND=history -a;

And save. (control - O to write out. ^ means control in nano and other software, so the bottom of the editor does actually make sense to beginners! ^X to exit.) This makes bash append history instead of overwriting it, and makes it so that each time the prompt is shown it writes out all the history.

2. Stupid spelling mistakes

Add


shopt -s cdspell


to your .bashrc again. This will make sure that spelling mistakes such as ect instead of etc are ignored.

3. Duplicate entries in bash history


I often type cd .. multiple times in a row, when I then press UP to go back to earlier commands I don’t want to be reminded of my earlier inelegant excursions around the file system.
Add


export HISTCONTROL="ignoredups"


to .bashrc again.
Even better, add


export HISTIGNORE="&:ls:[bf]g:exit”


This will ignore duplicates, as well as ls, bg, fg and exit as well, making for a cleaner bash history.

4. Multiple line commands split up in history


Add


shopt -s cmdhist


to .bashrc, this will change multiple line commands into single lines for easy editing.


5. A couple of neat extras suggested by commenters


Press control + R in bash, then start typing and you can search through your past commands much easier than just pressing UP 300 times…
Alternatively, use


history grep "foo"


to search through your history - “foo” is the thing you are searching for. (Thanks to Ally)


cd -


goes to the last directory you were in - useful if you want to go somewhere to change something, then need to quickly flip back again.

Conclusion So, there are a few tips to get your bash history more manageable - if you have any extra tips, add them to the comments and I’ll add them to the main article!

Windows / Mac / or Linux?

Windows, Mac, or Linux is the question that comes to mind when people go to buy new computers. Now, which one should you use? If you want a bitten apple, an easy-to-use OS with no viruses, or want high-tech graphics editing, GET A MAC!!!!! If you want a semi-buggy OS with good network connectivity and a great range of software (but with 144,000 virus out to get it), get Windows. If you want to get the most secure OS in the world, get Windows Vista. It's more secure than any other OS because its restrictions are so great that nothing goes in and nothing goes out. If you're an internet user, Vista is not right for you. If you are an avid developer, want a FREE OS or if you just want a REALLY REALLY REALLY wide range of choices for an OS (there are over 500 different distributions of this OS), then Linux is right for you.

Personally, I prefer Linux. I'm into telecom software development and they're right for me. However, for daily use, I use Windows XP because we have to do a lots of documentation also

My First Post!

(Just setting this thing up)

I ...
-am happy in my Family
-live in Delhi
-work as a Test Manager
-love to work in UNIX enviorment