strive_tan shell编程实战2-分发系统
Posted tanzhirong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了strive_tan shell编程实战2-分发系统相关的知识,希望对你有一定的参考价值。
1. expect的应用
1)传输文件
2)远程执行命令,无需交互,无需输入密码
3)上线的shell脚本(工具),核心是expect,即分发系统
2. expect的安装
yum install -y expect
3. expect语言实例1:自动远程登陆某台服务器
#! /usr/bin/expect set host "192.168.133.132" set passwd "123456" spawn ssh root@$host expect { "yes/no" { send "yes\\r"; exp_continue} "password:" { send "$passwd\\r" } } interact |
4. expect语言实例2:自动远程登陆某台服务器后,并执行命令
#!/usr/bin/expect set user "root" set passwd "123456" spawn ssh $user@192.168.133.132 expect { "yes/no" { send "yes\\r"; exp_continue} "password:" { send "$passwd\\r" } } expect "]*" send "touch /tmp/12.txt\\r" expect "]*" send "echo 1212 > /tmp/12.txt\\r" expect "]*" send "exit\\r" |
5. expect脚本之传递参数
#!/usr/bin/expect set user [lindex $argv 0] set host [lindex $argv 1] set passwd "123456" set cm [lindex $argv 2] spawn ssh $user@$host expect { "yes/no" { send "yes\\r"} "password:" { send "$passwd\\r" } } expect "]*" send "$cm\\r" expect "]*" send "exit\\r" |
$argv0 表示要传递给变量user的参数;$argv1,$argv2同理
6. expect脚本之同步文件
7. expect脚本之构建文件分发系统
8.
#!/usr/bin/expect set host [lindex $argv 0] set passwd "123456" set cm [lindex $argv 1] spawn ssh root@$host expect { "yes/no" { send "yes\\r"} "password:" { send "$passwd\\r" } } expect "]*" send "$cm\\r" expect "]*" send "exit\\r" |
以上是关于strive_tan shell编程实战2-分发系统的主要内容,如果未能解决你的问题,请参考以下文章