首页 > 系统相关 >shell中使用expect

shell中使用expect

时间:2023-02-23 15:12:48浏览次数:26  
标签:shell send hi set expect 使用 yes password

需要先安装expect

yum -y install expect

expect 语法

expect [选项] [ -c cmds ] [ [ -[f|b] ] cmdfile ] [ args ]
选项
-c:从命令行执行expect脚本,默认expect是交互地执行的
示例:expect -c 'expect "\n" {send "pressed enter\n"}
-d:可以输出输出调试信息
示例:expect -d ssh.exp

expect中相关命令
spawn 启动新的进程
send 用于向进程发送字符串
expect 从进程接收字符串
interact 允许用户交互
exp_continue 匹配多个字符串在执行动作后加此命令


expect最常用的语法(tcl语言:模式-动作)
单一分支模式语法:
expect “hi” {send “You said hi\n"}
匹配到hi后,会输出“you said hi”,并换行
多分支模式语法:
expect "hi" { send "You said hi\n" } \
"hehe" { send "Hehe yourself\n" } \
"bye" { send "Good bye\n" }
匹配hi,hello,bye任意字符串时,执行相应输出。等同如下:
expect {
"hi" { send "You said hi\n"}
"hehe" { send "Hehe yourself\n"}
"bye" { send " Good bye\n"}
}

示例

#!/usr/bin/expect
spawn scp /etc/fstab 192.168.8.100:/app
expect {
 "yes/no" { send "yes\n";exp_continue }
 "password" { send "abc\n" }
}
expect eof
#!/usr/bin/expect
spawn ssh 192.168.8.100
expect {
 "yes/no" { send "yes\n";exp_continue }
 "password" { send "abc\n" }
}
interact
#expect eof
#!/usr/bin/expect
set ip 192.168.8.100
set user root
set password magedu
set timeout 10
spawn ssh $user@$ip
expect {
 "yes/no" { send "yes\n";exp_continue }
 "password" { send "$password\n" }
}
interact

实例:位置参数

[lindex $argv 0] 相当于shell中的$1

[lindex $argv 1] 相当于shell中的$2

#!/usr/bin/expect
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh $user@$ip
expect {
 "yes/no" { send "yes\n";exp_continue }
 "password" { send "$password\n" }
}
interact
#./ssh3.exp 192.168.8.100 root magedu

示例:执行多个命令

#!/usr/bin/expect
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
set timeout 10
spawn ssh $user@$ip
expect {
 "yes/no" { send "yes\n";exp_continue }
 "password" { send "$password\n" }
}
expect "]#" { send "useradd haha\n" }
expect "]#" { send "echo abc |passwd --stdin haha\n" }
send "exit\n"
expect eof
#./ssh4.exp 192.168.8.100 root magedu

示例:shell脚本调用expect

用多行重定向来写expect的语句块

expect <<EOF

...

...

...

EOF
#!/bin/bash
ip=$1
user=$2
password=$3
expect <<EOF
set timeout 20
spawn ssh $user@$ip
expect {
 "yes/no" { send "yes\n";exp_continue }
 "password" { send "$password\n" }
}
expect "]#" { send "useradd hehe\n" }
expect "]#" { send "echo abc |passwd --stdin hehe\n" }
expect "]#" { send "exit\n" }
expect eof
EOF
#./ssh5.sh 192.168.8.100 root abc

标签:shell,send,hi,set,expect,使用,yes,password
From: https://www.cnblogs.com/guangdelw/p/17148032.html

相关文章

  • TCP和UDP的区别及使用场景
    一、TCP和UDP是什么?   TCP:   传输控制协议(TCP,TransmissionControlProtocol)是一种面向连接的、可靠的、基于字节流的传输层通信协议,由IETF的RFC793定义。   ......
  • easyes 使用
    easyes是一个国产的开源软件,提供类似于mybatis-plus查询数据的方式来查询es数据,极大提高了实现es增删改查功能的开发效率使用方式如下1.工程导入依赖<depen......
  • JPA在SpringBoot中简单使用
    前言在SpringBoot项目中可以与JPA进行搭配,这样会省很多的开发时间,以下为JPA的简单使用一、导入依赖<!--springbootjpa依赖--><dependency><groupId>org.spring......
  • shell中的运算符
    算术运算运算符举例结果+(加运算)3+58-(减运算)5-32*(乘运算)5*315/(除运算)8/32%(取余运算)15%43**(幂运算)5**3125bash中的......
  • shell中的各种测试语句
    变量测试表达式解释${var:-word}|若var存在且非空,则值为$var;若var未定义或为空值,则值为word,但var的值不变。${var:=word}|若var存在且非空,则值为$va......
  • MarkDown的使用
    MarkDown的使用规范标题1标题2标题3标题4标题5字体**Hello,Word!**Hello,Word!Hello,Word!Hello,Word!引用学java,的以一个博客.分割线图片![截图......
  • shell基础
    设置根据后缀补充title新建.vimrc文件,存放到家目录中vim.vimrc#做一些对vim的自定义设置setcursorlinesetautoindent#当新建的文件以sh结尾的,调用SetTitel()函数......
  • BTrace使用
    来源《深入理解java虚拟机》书中第四章提到一个VisualVM插件,叫做BTrace,竟然可以在不改变原有代码也不停进程的基础上,在进程嵌入一段代码,获取进程中的一些方法参数和返回......
  • CH32V203C8T6使用SPI2出现的问题
    最近调试一个项目CAN转SPI(SPI主机),另外一个SPI从机接收使用到SPI2,一直测试不通,特此记录首先使用沁恒官方给的历程(点击即可下载该历程)可以正常跑通,使用自己的程序 一样......
  • 直播软件源码,在vue中使用html2canvas在前端生成图片
    直播软件源码,在vue中使用html2canvas在前端生成图片1、安装 npminstallhtml2canvas​2、用法 importhtml2canvasfrom'html2canvas'; html2canvas(document.......