================简洁方法程序==============
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号
Powered by ddoss.cn 12.0
©2015 - 2025 ddoss
渝公网安备50011302222260号
渝ICP备2024035333号
【实验平台安全承诺书】
小绿叶技术社区,优化网络中,点击查看配置信息
主机监控系统: 安全防火墙已开启检查cc攻击-下载文件完成后等待10s 恢复访问,检查连接数低于峰值恢复访问
您的IP:216.73.216.110,2025-12-01 15:19:01,Processed in 0.01802 second(s).