bash&shell Serial articles:http://www.cnblogs.com/f-ck-need-u/p/7048359.html
<http://www.cnblogs.com/f-ck-need-u/p/7048359.html#blogshell>

expect The tool can realize automatic response, So as to achieve the goal of non interaction.

expect The specific usage is complex, Chinese manual I am translating, After the translation, I'll make up for it. There are only a few in this articlessh Related most likely examples.
yum -y install expect
1.1 scp Auto answer script

Below isscp Script for automatic Q & A.
[[email protected] ~]# cat autoscp.exp #!/usr/bin/expect
########################################################### # description: scp
without interactive # # author : Golden Dragon # # blog : http://
www.cnblogs.com/f-ck-need-u/ #
########################################################### set timeout10 set
user_hostname [lindex $argv0] set src_file [lindex $argv 1] set dest_file
[lindex $argv2] set password [lindex $argv 3] spawn scp $src_file
$user_hostname:$dest_file expect {"(yes/no)?" { send "yes\n" expect "*assword:"
{ send"$password\n"} } "*assword:" { send "$password\n" } } expect "100%"
expect eof
usage:autoscp.exp [[email protected]]hostname src_file dest_file [password]


The auto answer script can automatically complete host authentication and password authentication, Even a machine that already implements public key authentication is OK, Because public key authentication mechanism takes precedence over password authentication by default, And thepassword Item is optional, Of course, Without public key authentication,password It is a must. otherwiseexpect Achieving non interactive goals is meaningless.

Here are some examples:
[[email protected] ~]# ./autoscp.exp 172.16.10.6 /etc/fstab /tmp 123456 spawn scp
/etc/fstab172.16.10.6:/tmp The authenticity of host '172.16.10.6 (172.16.10.6)'
can't be established. RSA key fingerprint is f3:f8:e2:33:b4:b1:92:0d:5b:95:3b:97
:d9:3a:f0:cf. Are you sure you want to continue connecting (yes/no)? yes #
Ask whether to save when the host validateshost key, Automatic answeryes Warning: Permanently added'172.16.10.6' (RSA) to
the list of known hosts. [email protected]'s password: # Password authentication process, Automatically answer the specified password"123456"
fstab100% 805 0.8KB/s 00:00
You can also specify the completed user name and host name.
[[email protected] ~]# ./autoscp.exp [email protected] /etc/fstab /tmp 123456 spawn scp
/etc/fstab [email protected]:/tmp [email protected]'s password: fstab 100% 805 0
.8KB/s00:00
<>

1.2 ssh-copy-id Auto answer script

The following is when establishing the public key authentication mechanism,ssh-copy-id Automatic reply script of copying public key to server.
[[email protected] ~]# cat /tmp/autocopy.exp #!/usr/bin/expect
########################################################### # description:scp
without interactive # # author : Golden Dragon # # blog : http://
www.cnblogs.com/f-ck-need-u/ #
########################################################### set timeout10 set
user_hostname [lindex $argv0] set password [lindex $argv 1] spawn ssh-copy-id
$user_hostname expect {"(yes/no)?" { send "yes\n" expect "*assword:" { send "
$password\n"} } "*assword:" { send "$password\n" } } expect eof
usage:autocopy.exp [[email protected]]hostname password

Here is an example,
[[email protected] ~]# /tmp/autocopy.exp [email protected] 123456 spawn ssh-copy-id
[email protected] The authenticity of host '172.16.10.6 (172.16.10.6)' can't be
established. RSA key fingerprint is f3:f8:e2:33:b4:b1:92:0d:5b:95:3b:97
:d9:3a:f0:cf. Are you sure you want to continue connecting (yes/no)? yes #
Host authentication, Automatic answeryes Warning: Permanently added'172.16.10.6' (RSA) to the list of
known hosts. [email protected]'s password: # Enter password automatically during password authentication"123456" Now try logging
into the machine, with"ssh '[email protected]'", and check in: .ssh/
authorized_keys tomake sure we haven't added extra keys that you weren't
expecting.
1.3 batchssh-copy-id Automatic answer

If you want to achieve batch non interaction, You can write ashell The script calls thisexpect Script. for example:
[[email protected] ~]# cat /tmp/sci.sh #!/bin/bash
########################################################### # description:scp
without interactive # # author : Golden Dragon # # blog : http://
www.cnblogs.com/f-ck-need-u/ #
###########################################################passwd=123456 #
Specifies that the password to be passed is123456 user_host=`awk '{print $3}' ~/.ssh/id_rsa.pub` #
This variable is used to determine whether the local information has been added successfully in the remote hostfor i in [email protected] do /tmp/autocopy.exp $i $passwd >&/dev/null
ssh $i "grep "$user_host" ~/.ssh/authorized_keys" >&/dev/null # Judge whether to add native information successfully if
[ $? -eq0 ];then echo "$i is ok" else echo "$i is not ok" fi done
usage:/tmp/sci.sh [[email protected]]hostname


amonghostname Parts can be enumerated by expanding with curly braces. But there is one.bug, Bestssh-copy-id The target of is not the script's native machine, Local password may be forced, But batch scriptautocopy.exp There is no such thing.bug.

for example:
[[email protected] tmp]# /tmp/sci.sh 172.16.10.3 172.16.10.6 172.16.10.3 is ok 172.16.
10.6 is ok [[email protected] tmp]# /tmp/sci.sh 172.16.10.{3,6} 172.16.10.3 is ok
172.16.10.6 is ok [[email protected] tmp]# /tmp/sci.sh [email protected] 172.16.10.6
[email protected] is ok 172.16.10.6 is ok