I don't wan write script ( cron job) on remote machine. Instead, I want to schedule a cron job on my local machine that run for the remote server. Both machine has same linux version Redhat 5.5 and I have ssh configured on both remote server and local machine. So I want to know is there any linux scripting or any command .
Thanks & Regards
I don't wan write script ( cron job) on remote machine. Instead, I want to schedule a cron job on my local machine that run for the remote server.
Why? What is the business problem you are trying to solve by doing it this way?
Both machine has same linux version Redhat 5.5 and I have ssh configured on both remote server and local machine. So I want to know is there any linux scripting or any command .
Thanks & Regards
To use ssh to execute commands on a remote machine is fairly simply and does not even require that your host and remote host use the same OS or user.
Saturn:~ bb$ uname -s -r
Saturn:~ bb$ ssh email@example.com "uname -sr"
If you want to do this in cron you will have to setup ssh user equivalence, so it does not prompt you for password.
To use the example above, where 10.0.2.5 is the remote machine, for instance:
Create public and private keys on the current host:
Saturn:~ bb$ mkdir -p ~/.ssh
Saturn:~ bb$ chmod 700 ~/.ssh
Saturn:~ bb$ rm -f ~/.ssh/id_dsa
Saturn:~ bb$ ssh-keygen -t dsa -N "" -f ~/.ssh/id_dsa
Generating public/private dsa key pair.
Your identification has been saved in /Users/bb/.ssh/id_dsa.
Your public key has been saved in /Users/bb/.ssh/id_dsa.pub.
The key fingerprint is:
The key's randomart image is:
+--[ DSA 1024]----+
| o.o ..o . |
| . o+. . = |
| . o.+. + = |
| ....Eo o . |
| .o .. S |
| .. . |
| . o |
| .o |
| .o. |
Create the necessary .ssh directory on the remote host and set required privileges:
Saturn:~ bb$ ssh firstname.lastname@example.org "mkdir -p .ssh; chmod 700 .ssh"
Copy the DSA public key to the remote host account and set required privileges:
Saturn:~ bb$ KEY=`cat ~/.ssh/id_dsa.pub`
Saturn:~ bb$ ssh email@example.com "echo "$KEY" >> .ssh/authorized_keys; chmod 644 .ssh/authorized_keys"
Saturn:.ssh bb$ ssh firstname.lastname@example.org "uname -sr"
Voila, this time it did not ask for a password anymore! So you could then use ssh commands in cron to execute them on the remote host. The DSA key-pair will also work with SFTP and SCP, which are both part of SSH. The older RSA key is no longer required, only for some older licensed versions of SSH.
How to use cron? There is lots off info on the web.
Message was edited by: Dude! I thought I would point out that is probably nothing wrong using RSA instead of DSA. Unfortunately some of the information on the web is already outdated. RSA apparently supports so called Perfect Forward Secrecy as well as DSA, which generate random public keys per session for the purposes of key agreement that is not based on any sort of deterministic algorithm. Historically, version 1 of the SSH protocol supported only RSA keys. When version 2 was defined, RSA was still patented, and DSA was added to support an opensource patent-free implementation. RSA patent expired more than 10 years ago.
I come to know that how i can copy a file by using ssh (to bypass password) while connecting with remote host from local host. But I am not getting your point that if I write .sh file on local machine (SERV1) then how i can run it on remote host(SERV2) ?
Thanks & Regards
Copying the DSA public key to the remote host is part of the password less login setup. If you want to copy a file to a remote machine you would use SCP. I did not make a point of copying a file to a remote machine, but was demonstrating how you can use SSH to execute commands on a remote machine, for which password less login is necessary, especially if you wish automate the task.
You can also execute a local script on a remote host using the previous setup, for example:
Saturn:~ bb$ cat script
who am i
Saturn:~ bb$ ssh -T email@example.com < script
drwx------. 3 dude dude 4096 Dec 9 16:40 .
drwxr-xr-x. 3 root root 4096 Dec 9 16:37 ..
-rw-------. 1 dude dude 5 Dec 9 16:38 .bash_history
-rw-r--r--. 1 dude dude 18 Jul 18 16:33 .bash_logout
-rw-r--r--. 1 dude dude 176 Jul 18 16:33 .bash_profile
-rw-r--r--. 1 dude dude 124 Jul 18 16:33 .bashrc
drwx------. 2 dude dude 4096 Dec 9 16:40 .ssh