shell脚本需要交互的地方可以使用here文档是实现,但是有些命令却需要用户手动去就交互如passwd、scp

对自动部署免去用户交互很痛苦,expect能很好的解决这类问题。

expect的核心是spawn expect send set

spawn 调用要执行的命令

expect 等待命令提示信息的出现,也就是捕捉用户输入的提示:

send 发送需要交互的值,替代了用户手动输入内容

set 设置变量值

interact 执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。如果没有这一句登录完成后会退出,而不是留在远程终端上。

expect eof 这个一定要加,与spawn对应表示捕获终端输出信息终止,类似于if....endif

expect脚本必须以interact或expect eof结束,执行自动化任务通常expect eof就够了。

设置expect永不超时

set timeout -1

设置expect 300秒超时,如果超过300没有expect内容出现,则推出

set timeout 300

expect编写语法,expect使用的是tcl语法。

一条Tcl命令由空格分割的单词组成. 其中, 第一个单词是命令名称, 其余的是命令参数

cmd arg arg arg

foo

方括号执行了一个嵌套命令. 例如, 如果你想传递一个命令的结果作为另外一个命令的参数, 那么你使用这个符号

[cmd arg]

双引号把词组标记为命令的一个参数. "

"符号, 引号,方括号和大括号的特殊含义

expect使用实例

1。首先确认expect的包要安置。

rpm -qa | grep expect

如果没有则需要下载安装,

yum install expect

2.安装完成后,查看expect的路径,可以用

which expect

/usr/bin/expect

3.编辑脚本

vi test.sh

添加如下内容

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

spawn su - nginx //切换用户

expect "password:" //提示让输入密码

send "testr" //输入nginx的密码

interact //操作完成

4.确定脚本有可执行权限

chmod +x autosu.sh

5.执行脚本 expect autosu.sh 或 ./autosu.sh

expect常用脚本

登陆到远程服务器:

#!/usr/bin/expect

set timeout 5

set server [lindex $argv 0]

set user [lindex $argv 1]

set passwd [lindex $argv 2]

spawn ssh -l $user $server

expect {

"(yes/no)" { send "yesr"; exp_continue }

"password:" { send "$passwdr" }

}

expect "*Last login*" interact

scp拷贝文件

#!/usr/bin/expect

set timeout 10

set 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_file

expect {

"(yes/no)?"

{

send "yesn"

expect "*assword:" { send "$passwordn"}

}

"*assword:"

{

send "$passwordn"

}

}

expect "100%"

expect eof

#!/bin/bash

set timeout 30

expect<

spawn ssh-keygen -t rsa

expect "Enter file in which to save the key (/root/.ssh/id_rsa): "

send "\n"

expect "Overwrite (y/n)? "

send "\n"

expect eof

exit

END

for i in `cat ./serverlist.ini |grep "="|awk -F= '{print $2}'`

do

echo $i

expect<

spawn ssh root@$i "mkdir /root/.ssh/"

expect "password: "

send "du77\n"

expect eof

exit

END

expect<

spawn scp /root/.ssh/id_rsa.pub root@$i:/root/.ssh/id_rsa.pub

expect "password: "

send "du77\n"

expect eof

exit

END

expect<

spawn ssh root@$i "touch /root/.ssh/authorized_keys"

expect "password: "

send "du77\n"

expect eof

exit

END

expect<

spawn ssh root@$i "cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys"

expect "password: "

send "du77\n"

expect eof

exit

END

done

#!/bin/bash

set timeout 30

for i in `cat ./serverlist.ini |grep "="|awk -F= '{print $2}'`

do

echo $i

expect<

spawn scp /root/.ssh/id_rsa.pub root@$i:/root/.ssh/id_rsa.pub

expect "password: "

send "123@123\n"

expect eof

exit

END

expect<

spawn ssh root@$i "touch /root/.ssh/authorized_keys"

expect "password: "

send "123@123\n"

expect eof

exit

END

expect<

spawn ssh root@$i "cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys"

expect "password: "

send "123@123\n"

expect eof

exit

END

done

expect返回值给shell_使用expect实现shell自动交互相关推荐

  1. shell自动交互之expect脚本_转

    转自:linux expect详解(ssh自动登录) shell脚本实现ssh自动登录远程服务器示例: #!/usr/bin/expect spawn ssh root@192.168.22.194e ...

  2. expect返回值给shell_expect获取返回值

    标签: 对于获取多台服务器状态且不用交互需要用到expect,但有时候expect无法获取返回值,这里解释一下expect如何获取返回值 expect -c " spawn $1; expe ...

  3. 使用expect实现shell自动交互

    shell脚本需要交互的地方可以使用here文档是实现,但是有些命令却需要用户手动去就交互如passwd.scp 对自动部署免去用户交互很痛苦,expect能很好的解决这类问题. expect的核心是 ...

  4. cmd管道无法接收特定程序返回值_渗透不会反弹shell?来教你写一个cmd的shell

    渗透不会反弹shell?来教你写一个cmd的shell 包含的库: #include #include #include #include #include #pragma comment(lib, ...

  5. Linux基于expect(tcl)实现shell自动交互

    1.需求:在shell中执行scp命令时,可以自动输入密码,而不用手工交互输入. 2.方案:采用expect来实现.       Expect是一个基于TCL开发出的语言包.       而TCL(T ...

  6. 使expect脚本传回返回值

    使expect脚本传回返回值 1.使用expect示例: spawn $SPAWN_CMD expect { -re "Enter password for new role:" ...

  7. linux命令返回值的妙用

    什么是返回值 在shell终端中,你所输入的一切命令其实都有返回值,而这个返回值默认保存在"$?"中,举例看一下 [root@localhost ~]# touch 123 [ro ...

  8. linux 所有命令的返回值

    linux命令返回值的妙用 在shell终端中,你所输入的一切命令其实都有返回值,而这个返回值默认保存在"$?"中,举例看一下 返回值的好处: 在编写shell脚本的时候我们要确认 ...

  9. 关于waitpid的返回值问题

    一次偶然的测试中,发现su程序的BUG后,着手排查问题出自哪. 首先是简化代码作为测试. int main(int argc, char *const argv[]) {pid_t pid = for ...

最新文章

  1. javascript对象和json字符串之间转换的问题
  2. python中的threading_python中threading超线程用法实例分析
  3. 【算法竞赛学习】AI助力精准气象和海洋预测
  4. dx绘制2d图像_【3D建模】聊聊2D动画软件
  5. Jmeter脚本录制和压测
  6. h264, h265 和 libvpx 比较(h264/avc, hevc 和vp9比较)
  7. Mysql sql执行错误#1436 Thread stack overrun:
  8. 2020秋招腾讯群面场景题:给莫高窟设计一款互联网+产品
  9. 【闪亮的玻璃图标悬浮效果】
  10. VSCode远程连接服务器使用R语言
  11. allegro中design size无法修改
  12. 中国智慧生活博览会(CEE 2017)—数字世界亚洲博览会会刊(参展商名录)
  13. 企业服务器托管比租用有哪些优势
  14. Verilog HDL 硬件描述语言基础
  15. 测试opencl软件,OpenCL应用测试
  16. 精致的利己主义者之论,转自知乎
  17. 买电视好还是投影仪好,最新2000余字专业讲解分享给大家
  18. 二进制文件和文本文件
  19. 微软老照片AI修复开源代码运行错误
  20. 第八课.TPAMI2021年多篇GNN相关工作

热门文章

  1. 搭建SpringMVC+Hibernate4+Spring3+Ajax+Maven项目(二)
  2. WCF中安全的那些事!!!
  3. 连接oracle数据库,新建用户登录界面
  4. Netty(二)——TCP粘包/拆包
  5. 论一只爬虫的自我修养
  6. 使用RSS订阅喜欢的微博博主
  7. 初识ajaxpro以及使用
  8. [导入]Vista的屏幕截图小工具:Snipping Tool
  9. 【进阶技巧】如何绘制高颜值XMind思维导图?色彩使用很重要!
  10. 【防衰老教程】记录一次IDEA,开发JavaWeb项目时JS中文乱码排错