收藏文章 楼主

spawn-expect关键字交互-KVM

版块:KVM虚拟化   类型:普通   作者:小绿叶技术博客   查看:1656   回复:0   获赞:2   时间:2019-03-28 08:43:03

================简洁方法程序==============

1.注释前面不能有字符 #!/usr/bin/expect  

2.匹配关键字需要匹配末尾,跟着末尾输入信息



#!/bin/bash

yum install -y expect

                # 安装交互工具包

set timeout 30

                # 设置匹配字符的等待时间

/usr/bin/expect << EOF

                # 执行交互程序,通过EOF 打包给程序

spawn ssh root@hc2.ssh.gs

                # 进行远程连接服务器

expect "password:"

                # 匹配密码提示

send "aA20200930\r"

                # 输入密码并换行

expect "#"

                # 登录成功后匹配符号 #

send "echo '登录成功' \r"

                # 执行打印命令并换行

expect eof

                # 匹配结束

exit

                # 退出

EOF

                # 总打包结束




chmod +x expect3.sh

#权限

./expect3.sh

#当前目录下执行脚本

#!/usr/bin/expect

sleep 5

spawn /usr/bin/mysql_secure_installation

expect {

"enter for none" {send "000000\r";exp_continue}

"Y/n" {send "y\r";exp_continue}

"password" {send "888888\r";exp_continue}

"Cleaning up" {send "\r"}

}

interact ' > mysql_secure_istallation.exp


#这是交互界面,用于在提示操作信息的时候自动输入,在expect{}里面添加多个匹配输入并回答

====================================





#===================普通程序=========================

yum install -y expect

-------------------

spawn启动新进程

expect接收字符串

-------------------

#!/bin/bash

expect -c "

spawn "/usr/bin/mysql_secure_installation"

#spawn为执行命令


expect \"Enter current password for root (enter for none):\"

#expect为:匹配定位关键字,


send \"123456\r\"

#send为:输入,符号   \    为转译特殊字符,特殊字符如  ?:  等;r  为回车符号


expect \"Change the root password\"

send \"y\r\"

expect \"New password:\"

send \"000000\r\"

expect \"Re-enter new password\"

send \"000000\r\"

expect \"Remove anonymous users\"

send \"y\r\"

expect \"Disallow root login remotely\"

send \"n\r\"

expect \"Remove test database and access to it\"

send \"y\r\"

expect \"Reload privilege tables now\"

send \"y\r\"

expect eof

#结束标

"

#==========管道命令交互界面=======

run sh -C '/bin/echo -e "yes\nno\yes\nroot\n000000"|yum install unzip'

# 在执行后面这条命令后,交互界面  输入关键字后 \n 换行(回车),



#=========================以ID来卸载============================

yum history list

查看安装的依赖包的ID


yum history undo 11

卸载软件“11”是ID号

提供企业建站服务,免费网防系统,提交信息登录 http://yundun.ddoss.cn 邮箱: proposal@ddoss.cn 
回复列表
默认   热门   正序   倒序

回复:spawn-expect关键字交互-KVM

头像

用户名:

粉丝数:

签名:

资料 关注 好友 消息