Subscribe:

Ads 468x60px

Pages

Thursday, April 4, 2013

Autologin through SSH using bash script

Autologin using bash script

1. Create a new ssh RSA keygen:
[root@linuxtutors# ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (//.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in //.ssh/id_rsa.
Your public key has been saved in //.ssh/id_rsa.pub.
The key fingerprint is:
4a:0d:db:6a:06:c9:c8:62:e0:f3:cb:93:5f:de:d6:f4 root@-i

Keys are stored at /.ssh/ directory:
[root@linuxtutors# ~]# cd /.ssh/

List the files
[root@linuxtutors# .ssh]# ls -l; cd
total 6
-rw-------   1 root     root         883 Apr  4 17:50 id_rsa
-rw-r--r--   1 root     root         217 Apr  4 17:50 id_rsa.pub
-rw-r--r--   1 root     root         633 Apr  4 17:43 known_hosts

2. Create the Bash Script to copy public key to remote server:
[root@linuxtutors# ~]# vi autossh.sh
#!/bin/bash
ips=/tmp/List_Of_Machine_IPs.txt
for x  in $(cat $ips)
do

cat .ssh/id_rsa.pub | ssh root@$x 'cat >> .ssh/authorized_keys'
cat .ssh/id_rsa.pub | ssh root@$x 'cat >> .ssh/authorized_keys2'
ssh root@$x 'chmod 640 .ssh/authorized_keys'
ssh root@$x 'chmod 640 .ssh/authorized_keys2'
ssh root@$x 'chmod 700 .ssh'
done

Save and Exit:
:wq

3. Assign the execute permission to the script:
[root@linuxtutors# ~]# chmod a+x autossh.sh

4. List all the machine ip which you want to autologin:
[root@linuxtutors# ~]# vi /tmp/List_Of_Machine_IPs.txt
192.168.1.100
192.168.1.101
192.168.1.102

Save and Exit:
:wq

5. Execute the script
[root@linuxtutors# ~]# sh autossh.sh
The authenticity of host '192.168.1.100 (192.168.1.100)' can't be established.
RSA key fingerprint is ae:14:11:0f:cc:2a:28:03:b4:13:7f:35:ce:ab:f5:ee.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.100' (RSA) to the list of known hosts.
root@192.168.1.100's password:
root@192.168.1.100's password:
The authenticity of host '192.168.1.101 (192.168.1.101)' can't be established.
RSA key fingerprint is ae:14:11:0f:cc:2a:28:03:b4:13:7f:35:ce:ab:f5:ee.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.101' (RSA) to the list of known hosts.
root@192.168.1.101's password:
root@192.168.1.101's password:
The authenticity of host '192.168.1.102 (192.168.1.102)' can't be established.
RSA key fingerprint is ae:14:11:0f:cc:2a:28:03:b4:13:7f:35:ce:ab:f5:ee.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.102' (RSA) to the list of known hosts.
root@192.168.1.102's password:
root@192.168.1.102's password:
[root@linuxtutors# ~]# 

0 comments:

Post a Comment