Automated configuration ssh connection
When other host connection from the host using ssh often require a number of different response information.
For example, when the first visit:
#ssh root@localhost
The authenticity of host 'localhost (::1)' can't be established.
ECDSA key fingerprint is 03:cc:13:fe:6c:85:0a:af:df:67:de:18:19:ae:d8:1f.
Are you sure you want to continue connecting (yes/no)?
You enter yes
the next step you will find under the following output:
root@localhost's password:
At this point it asks you to enter the root password. After you enter the correct password, you can see the following output:
Last login: Tue Jun 14 20:45:07 2016 from 192.168.12.1
[root@LINUXTEST ~]#
If we want to achieve ssh automatic operation, and perform an operation on a remote host, such as:
root@LINUXTEST .ssh]# ssh root@localhost hostname
The authenticity of host 'localhost (::1)' can't be established.
ECDSA key fingerprint is 03:cc:13:fe:6c:85:0a:af:df:67:de:18:19:ae:d8:1f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
root@localhost's password:
LINUXTEST
In this process, you need to enter yes
, password
these two values.
Here alone shell
itself is not complete, we need a expect
tool to help us.
installation
expect
#yum install expect -y Installed: expect.x86_64 0:5.45-14.el7_1 Dependency Installed: tcl.x86_64 1:8.5.13-8.el7 Complete!
Create the following script
autossh.sh
# vi autossh.sh #!/usr/bin/expect set ipaddr "localhost" set password "root1234" spawn ssh root@$ipaddr expect { "yes/no" { send "yes\r";exp_continue } "password:" { send "\r" } } expect "]#" send "pwd;hostname \r" expect eof exit # chmod u+x autossh.sh
Execute this script, we get the following output
# ./autossh.sh spawn ssh root@localhost The authenticity of host 'localhost (::1)' can't be established. ECDSA key fingerprint is 03:cc:13:fe:6c:85:0a:af:df:67:de:18:19:ae:d8:1f. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts. root@localhost's password: Last login: Tue Jun 14 20:56:38 2016 from 192.168.12.1 [root@LINUXTEST ~]# pwd;hostname /root LINUXTEST
Next we all just need to create a user name and password hosts file
h-u-p.txt
, which contains the following elements#vi h-u-p.txt hostname1:username1:password1 hostname2:username2:password2 hostname4:username3:password3 hostname4:username4:password4 .......
Write another script
batchssh.sh
to handle these parameters and callsautossh.sh
#vi batchssh.sh #!/usr/bin/bash for line in $(cat h-u-p.txt) do arr=(${line//:/ }) ./autossh.sh ${arr[0]} ${arr[1]} ${arr[2]} done
Modified
autossh.sh
to#>autossh.sh #vi autossh.sh #!/usr/bin/expect set ipaddr [lindex $argv 0] set username [lindex $argv 1] set password [lindex $argv 2] spawn ssh $username@$ipaddr expect { "yes/no" { send "yes\r";exp_continue } "password:" { send "$password\r" } } expect "]#" send "pwd;hostname;exit \r" expect eof exit
So now you can edit the file
h-u-p.txt
to the batch execution ssh connection. Files are listed below:autossh.sh batchssh.sh h-u-p.txt