expect 是一个用来与终端交互的一个命令, 必须先安装才能使用, 我的系统是ubuntu 的直接apt-get install expect 就行了,
expect 的关键字就四个
expect -- 期望输入
send -- 代码提交expect 的内容
spawn --单起一个进程, 比如ssh , scp,rsync等等
interact --这个允许用户交互
比如我要做一个ssh 登陆的简单脚本 login.expect
#!/usr/bin/expect
set timeout 30
set host [lindex $argv 0]
set username [lindex $argv 1]
set passwd [lindex $argv 2]
spawn ssh $username@$host
expect {
"yes/no" {send "yes\r"; exp_continue}
"*password*" {send "$passwd\r"}
}
interact
就能登陆到152.10的服务器
有了这个命令,对于多个服务器管理就有有很大帮助, 比如要分发文件到N个服务器 直接添加 发送地址和接收地址用scp 或者rsync 发送即可.
非常方便.