expect 远程交互脚本
来源:互联网 发布:开放式网络结构 编辑:程序博客网 时间:2024/04/27 23:18
本文着重介绍如何通过expect 来完成远程服务器程序的升级,对于想了解他的朋友只是个引子。目前该程序测试已成功。
主要有三个文件:
1、要升级的程序:
firstapp
update.sh
ip.txt
firstapp: 是你修改的程序,将要去替换服务器上正在运行的程序。
update.sh: 就是用expect编写的交互脚本。
ip.txt: 为服务器的IP地址,如果服务器过多,可全部写入该文件,#号可注释掉某行 ,也可用//添加注释
update.sh脚本内容如下:
#!/usr/bin/expect -f
proc my_fun {ip} {
set USER "tony"
set PASSWD "123456"
set FILE "firstapp"
set BACKUP "${FILE}_`date +%Y%m%d`"
set PATH "/home/tony/bin/"
spawn scp ${FILE} ${USER}@${ip}:/tmp/
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "${PASSWD}\r" }
}
expect eof
spawn ssh -l ${USER} ${ip}
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "${PASSWD}\r" }
}
expect {
"*$ " { send "sudo killall ${FILE}\r" }
"*$" { send "${PASSWD}\r" }
}
expect "tony:"
send "${PASSWD}\r"
expect {
"*$ " { send "rm ${PATH}${FILE}-* ${PATH}${FILE}_*; mv ${PATH}${FILE} ${PATH}${BACKUP}; cp /tmp/$FILE ${PATH}; chmod 777 ${PATH}${FILE}\r" }
"*$" { send "rm ${PATH}${FILE}-* ${PATH}${FILE}_*; mv ${PATH}${FILE} ${PATH}${BACKUP}; cp /tmp/$FILE ${PATH}; chmod 777 ${PATH}${FILE}\r" }
}
expect {
"*$ " { send "${PATH}${FILE} &\r" }
"*$" { send "${PATH}${FILE} &\r" }
}
expect {
"*$ " { send "ll $PATH |grep ${FILE}\r" }
"*$" { send "ll $PATH |grep ${FILE}\r" }
}
expect {
"*$ " { send "exit\r" }
"*$" { send "exit\r" }
}
expect eof
}
set fd [open ip.txt r]
set done 0
while {[gets $fd line] >= 0} {
incr done
set ops [string first "#" $line]
if { ${ops} == -1 } {
set index [string first "/" $line]
if { ${index} != -1 } {
set line [string range $line 0 [expr $index-1]]
set line [string trim $line]
}
if { "$line" != "" } {
puts "(${done}) /***************************Client IP: $line*******************************/"
my_fun $line
}
}
}
close $fd
运行时会提示你安装expect,几秒钟的事情。
spawn是进入expect环境后才可以执行的expect内部命令,如果没有装expect或者直接在默认的SHELL下执行是找不到spawn命令的。
expect {
"*$ " { send "sudo killall ${FILE}\r" }
"*$" { send "${PASSWD}\r" }
}
意思是说直至"$"或者"$ "出现,这个空格是我调试时发现的,我这ubuntu系统会多一个空格。大家注意下到底自己的系统返回的是什么就行了,则会一直阻塞在这,因为我们对ecpect设置了阻塞模式”set timeout -1“ 。while中也只是简单的读取文件操作,这就不解释了,呵呵,我也只是为了升级方便乱写的。
最后贴下ip.txt的内容:
192.168.1.100 //升级该IP地址的服务器程序
#192.168.1.102 //这个我不想升级,我用#注释掉
192.168.1.101 //等1.100 升级完成,下个就到我了
PS: 该文存在诸多BUG,别当真!!
- expect 远程交互脚本
- 交互脚本学习--expect
- expect 应用 用于远程交互
- Linux 远程执行脚本 expect
- 自动远程拷贝expect脚本
- 利用expect在linux shell脚本中交互(scp远程传输文件示例)
- linux 下使用expect编写shell 脚本交互访问远程服务器
- expect实现远程交互scp,ssh
- expect用法[自动连接远程机器脚本]
- 使用expect脚本实现scp远程传输
- Expect ssh免秘钥登入自动脚本(ssh-copy-id、expect免交互输入脚本)
- 用expect实现shell脚本的自动交互
- 使用expect实现自动交互,自动登录脚本
- 用expect编写Linux自动化交互登录脚本
- linux下一脚本自动交互执行小例(expect)
- 自动化交互脚本Expect简单介绍及应用实例
- shell脚本调用expect命令实现命令行自动交互
- shell脚本交互:expect学习笔记及实例详解
- ASP.NET Page执行顺序
- 黑马程序员__基础知识
- java删除文件
- 为什么要屏蔽SIGPIPE信号?转载:春天花会开 http://blog.sina.com.cn/springdannier
- 什么是原创?
- expect 远程交互脚本
- 飞思卡尔单片机DZ60---时钟初始化
- Android应用程序的安装位置(二)
- 虚析构函数
- Android——获取Android控件的宽和高
- nodejs 自定义事件处理
- 正确理解 clear:both
- Spring AOP + AspectJ Annotation Example
- Linux静态库和动态库