[Denny] Expect: 执行远端命令或登录时,避免输入命令
Auto-updated 2013-03-23 12:06.
[#B] Expect: 执行远端命令或登录时,避免输入命令
ROPERTIES:
:type: KnowledgeBase_Linux_Automate
:END:
| Item | Comment |
|---|---|
| send | 将expect脚本中需要的信息发送给spawn启动的那个进程 |
| send_user | 回显用户发出的信息,类似于shell中的echo而已。 |
| timeout变量 | 内置变量,控制交互的等待时间 |
Table Of Content
- 1 ssh到一组server上执行一个命令, 同时避免人工输入密码
- 2 expect兼容server可能需要输入密码,也可能不需要输入密码的两种情况
- 3 在多server上执行某个费时命令,但不要串行执行,以提高速度
- 4 ssh在server上执行一个命令
- 5 expect模块化编程,来复用代码
- 6 concrete example: 相对完善的示例
- 7 # –8<————————– separator ————————>8–
- 8 [question] 登录ssh时,不需要人工输入密码,并保持session不断开,以继续使用terminal
- 9 [question] expect ssh到一组server上执行一个命令,如果任何一台失败,都返回失败
- 10 web page: Now Code » 用Expect脚本在多台服务器上执行命令
- 11 web page: expect学习笔记及实例详解 – 点滴在线 – ifis – 和讯博客
- 12 useful link
1 DONE ssh到一组server上执行一个命令, 同时避免人工输入密码 IMPORTANT
#!/usr/bin/expect set timeout 10 set command "cat /etc/hosts" set user "denny" set password "password" set servers "r610-1 r610-2" foreach ip $servers { spawn ssh -o StrictHostKeyChecking=no $user@$ip "$command" expect "*password:*" send "$password\r" expect eof; }
2 DONE expect兼容server可能需要输入密码,也可能不需要输入密码的两种情况
#!/usr/bin/expect set timeout 10 set command "cat /etc/hosts" set user "denny" set password "password" set servers "127.0.0.1 denny-Vostro-1014" foreach ip $servers { spawn ssh -o StrictHostKeyChecking=no $user@$ip "$command" expect { "*password:*" { send "$password\r" expect eof } eof { } } }
3 DONE 在多server上执行某个费时命令,但不要串行执行,以提高速度
#!/usr/bin/expect set timeout 10 set logfile "/tmp/test.output" set command "cat /etc/hosts > $logfile" set command_timeout 5 set user "denny" set password "password" set servers "127.0.0.1 denny-Vostro-1014" foreach ip $servers { spawn ssh -o StrictHostKeyChecking=no $user@$ip "$command" expect "*password:*" send "$password\r" expect eof; } sleep $command_timeout set command "tail -n 20 $logfile" foreach ip $servers { spawn ssh -o StrictHostKeyChecking=no $user@$ip "$command" expect "*password:*" send "$password\r" expect eof; }
4 DONE ssh在server上执行一个命令
#!/usr/bin/expect set timeout 10 set command "cat /etc/hosts" set user "denny" set password "password" set ip "127.0.0.1" spawn ssh -o StrictHostKeyChecking=no $user@$ip "$command" expect "*password:*" send "$password\r" expect eof;
5 DONE expect模块化编程,来复用代码
#!/usr/bin/expect
set timeout 10
proc do_command {servers command} {
set user "denny"
set password "password"
foreach ip $servers {
spawn ssh -o StrictHostKeyChecking=no $user@$ip "$command"
expect {
"*password:*" {
send "$password\r"
expect eof
}
eof {
}
}
}
}
set servers "127.0.0.1 denny-Vostro-1014"
do_command $servers "cat /etc/hosts"
do_command $servers "ifconfig"
6 DONE concrete example: 相对完善的示例
运行: test.exp password
#!/usr/bin/expect
set timeout 10
set password [lindex $argv 0]
# common function
proc do_command {password servers command} {
set user "root"
foreach ip $servers {
spawn ssh -o StrictHostKeyChecking=no $user@$ip "$command"
expect {
"*password:*" {
send "$password\r"
expect eof
}
eof {
}
}
}
}
set all_servers "server1.server server2.server server3.server server4.server"
send_user "\n===========run command in all servers=============\n"
# puppet update
# do_command $password $all_servers "puppet agent --server puppet.server --waitforcert 60 --test"
# check error of router
# do_command $password $all_servers "find /var/log/service/ -name '*log*' | xargs grep -i 'error'"
do_command $password $all_servers "date"
7 # –8<————————– separator ————————>8–
8 [question] 登录ssh时,不需要人工输入密码,并保持session不断开,以继续使用terminal IMPORTANT
9 [question] expect ssh到一组server上执行一个命令,如果任何一台失败,都返回失败
10 web page: Now Code » 用Expect脚本在多台服务器上执行命令
11 web page: expect学习笔记及实例详解 – 点滴在线 – ifis – 和讯博客
Time: 2013-03-23 10:34:05 CST
