shell编程expect用法

shell编程expect用法

2023年7月10日发(作者:)

shell编程expect⽤法shell脚本需要交互的地⽅可以使⽤here⽂档是实现,但是有些命令却需要⽤户⼿动去就交互如passwd、scp对⾃动部署免去⽤户交互很痛苦,expect能很好的解决这类问题。expect的核⼼是spawn expect send setspawn 调⽤要执⾏的命令expect 等待命令提⽰信息的出现,也就是捕捉⽤户输⼊的提⽰:send 发送需要交互的值,替代了⽤户⼿动输⼊内容set 设置变量值interact 执⾏完成后保持交互状态,把控制权交给控制台,这个时候就可以⼿⼯操作了。如果没有这⼀句登录完成后会退出,⽽不是留在远程终端上。expect eof 这个⼀定要加,与spawn对应表⽰捕获终端输出信息终⽌,类似于if....endifexpect脚本必须以interact或expect eof结束,执⾏⾃动化任务通常expect eof就够了。设置expect永不超时set timeout -1设置expect 300秒超时,如果超过300没有expect内容出现,则推出set timeout 300expect编写语法,expect使⽤的是语法。⼀条Tcl命令由空格分割的单词组成. 其中, 第⼀个单词是命令名称, 其余的是命令参数cmd arg arg arg$符号代表变量的值. 在本例中, 变量名称是foo.$foo⽅括号执⾏了⼀个嵌套命令. 例如, 如果你想传递⼀个命令的结果作为另外⼀个命令的参数, 那么你使⽤这个符号[cmd arg]双引号把词组标记为命令的⼀个参数. "$"符号和⽅括号在双引号内仍被解释"some stuff"⼤括号也把词组标记为命令的⼀个参数. 但是, 其他符号在⼤括号内不被解释{some stuff}反斜线符号是⽤来引⽤特殊符号. 例如:n 代表换⾏. 反斜线符号也被⽤来关闭"$"符号, 引号,⽅括号和⼤括号的特殊含义expect使⽤实例1。⾸先确认expect的包要安置。#rpm -qa | grep expect如果没有则需要下载安装,#yum install expect2.安装完成后,查看expect的路径,可以⽤#which expect/usr/bin/expect3.编辑脚本#vi 添加如下内容

#!/usr/bin/expect -f //这个expect的路径就是⽤which expect 查看的结果

spawn su - nginx //切换⽤户expect "password:" //提⽰让输⼊密码send "testr" //输⼊nginx的密码interact //操作完成

4.确定脚本有可执⾏权限chmod +x 5.执⾏脚本 expect 或 ./ct常⽤脚本登陆到远程服务器

#!/usr/bin/expect

set timeout 5set server [lindex $argv 0]set user [lindex $argv 1]set passwd [lindex $argv 2]spawn ssh -l $user $serverexpect {"(yes/no)" { send "yesr"; exp_continue }"password:" { send "$passwdr" }}expect "*Last login*" interact

scp拷贝⽂件#!/usr/bin/expectset timeout 10set host [lindex $argv 0] //第1个参数,其它2,3,4参数类似set username [lindex $argv 1]set password [lindex $argv 2]set src_file [lindex $argv 3]set dest_file [lindex $argv 4]spawn scp $src_file $username@$host:$dest_fileexpect {"(yes/no)?" { send "yesn" expect "*assword:" { send "$passwordn"}}"*assword:"{send "$passwordn"}}expect "100%"expect eof#!/bin/bashset timeout 30expect<<-ENDspawn ssh-keygen -t rsaexpect "Enter file in which to save the key (/root/.ssh/id_rsa): "send "n"expect "Overwrite (y/n)? "send "n"expect eofexitENDfor i in `cat ./ |grep "="|awk -F= '{print $2}'`do echo $iexpect<<-END spawn ssh root@$i "mkdir /root/.ssh/" expect "password: " send "du77n"expect eofexitENDexpect<<-END spawn scp /root/.ssh/id_ root@$i:/root/.ssh/id_ expect "password: " send "du77n"expect eofexitENDexpect<<-END spawn ssh root@$i "touch /root/.ssh/authorized_keys" expect "password: " send "du77n"expect eofexitENDexpect<<-END spawn ssh root@$i "cat /root/.ssh/id_ >> /root/.ssh/authorized_keys" expect "password: " send "du77n"expect eofexitENDdone

#!/bin/bashset timeout 30for i in `cat ./ |grep "="|awk -F= '{print $2}'`do echo $iexpect<<-END spawn scp /root/.ssh/id_ root@$i:/root/.ssh/id_ expect "password: " send "123@123n"expect eofexitENDexpect<<-END spawn ssh root@$i "touch /root/.ssh/authorized_keys" expect "password: " send "123@123n"expect eofexitENDexpect<<-END spawn ssh root@$i "cat /root/.ssh/id_ >> /root/.ssh/authorized_keys" expect "password: " send "123@123n"expect eofexitENDdone

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1688985701a191739.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信