Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Saturday, June 21, 2008

Non-interactive SSH in Linux

There are two machines, A and B in a network. Let the IP of A be 192.168.1.2, and IP of B be 192.168.1.8. You can access remotely from A to B using the command: ssh guest@192.168.1.8. It will ask you the password of the user guest in machine B. Now let me tell you a simple trick so that you don't need to enter the password any more. It's called non-interactive ssh.

Suppose your username in machine A is pypy. Open the terminal. Go to the directory /home/pypy/.ssh (cd /home/pypy/.ssh). Now enter the follwoing command:
ssh-keygen -t dsa
First it will ask you for a filename. Just press enter to keep it default (id_dsa) or you can enter any other name. Then it will ask you for a passphrase. Enter empty there (just press enter twice). Now you will see two files there named id_dsa (that has the private key) and id_dsa.pub (that contains the public key).

pypy@pypy-laptop:~/.ssh$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/pypy/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/pypy/.ssh/id_dsa.
Your public key has been saved in /home/pypy/.ssh/id_dsa.pub.
The key fingerprint is:
fc:37:69:9e:bb:15:e7:55:cd:b9:52:12:e6:49:b1:6c pypy@pypy-laptop

Key generation complete. Check the directory

pypy@pypy-laptop:~/.ssh$ ls -lt
total 12
-rw------- 1 pypy pypy 668 2008-06-20 23:54 id_dsa
-rw-r--r-- 1 pypy pypy 610 2008-06-20 23:54 id_dsa.pub

Now append the content of id_dsa.pub file to the file named authorized_keys2 in /home/guest/.ssh directory (if the file doesn't exist first create it) of machine B. You can do this using this command also:
cat /home/pypy/.ssh/id_dsa.pub | ssh guest@192.168.1.8 'cat - >> ~/.ssh/authorized_keys2'

(it will ask you for the password of user guest of machine B).

Now done. Try to access machine B from machine A
pypy@pypy-laptop:~/.ssh$ ssh guest@192.168.1.8

Hopefully it won't ask you for a password :-)

Wednesday, June 4, 2008

execute linux command using Perl, Python, PHP

Those who program in Linux often need to execute/run linux commands from programs/scripts.

Using python you can execute linux command is the following way:

import os
os.system('ls -lt')



If you use Perl, you can do it in the following ways:
1. system(mkdir $folder);
2. qw(mkdir $folder);
3. $temp = `mkdir $folder`; #back ticks
4. exec($command);

If you use back ticks or qw, $? will be set if any error occurs. So you can use it like this:

qw(rm $filename);
if($?) {
print STDERR "error deleting file\n";
}


There are also several options available to run linux commands using PHP. Take a look at here for detals: http://us2.php.net/manual/en/ref.exec.php

Sunday, March 30, 2008

UnxUtil - Run Unix / Linux command in Windows

Life sucks when you are asked to make your script run in Windows, that you have written for Linux and that uses lots of Unix or Linux specific system commands :(

But there is a nice tool name UnxUtil that you can install on your windows and run Unix / Linux commands in the command line. UnxUtil will speed up your development by saving hours.

You can download it from here. After download, just extract it in a directory and add the PATH in your ENVIRONMENT VARIABLE (PATH) (Start -> Settings -> Control Panel -> System -> Advanced -> Environment Variables).

Thats all!

Friday, March 28, 2008

Perl equivalent of UNIX / Linux commands

One afternoon I was asked by my boss to write a Perl script that does the same task as the 'egrep' command of Linux. I studied the manual, to understand what 'egrep' does and planned a solution. But next morning when I came to office, I searched google and found a the Perl equivalent of 'egrep' command. It saved me at least two hours. You can click this link for details.