首页 > 系统相关 >linux expect

linux expect

时间:2022-10-31 15:00:07浏览次数:70  
标签:set send expect exp linux password ssh

linux expect

介绍

expect 是由Don Libes基于Tcl(Tool Command Language )语言开发的,主要应用于自动化交互式操作的场景,借助Expect处理交互的命令,可以将交互过程如:ssh登录,ftp登录等写在一个脚本上,使之自动化完成。尤其适用于需要对多台服务器执行相同操作的环境中,可以大大提高系统管理人员的工作效率

expect含有利用正则表达式进行模式匹配以及通用的编程功能,允许简单的脚本智能地管理如下工具:telnet,ftp和ssh(这些工具都缺少编程的功能),宏以及其它程序。Expect脚本的出现使得这些老的软件工具有了新的功能和更多的灵活性。

安装expect

首先检查系统是否自带了expect程序, 一般在 系统的/usr/bin目录下, 如果没有自带expect, 那么需要自行安装expect.
例如ubuntu系统安装:

sudo apt-get install expect

常用指令

expect语法

expect [选项][ -c cmds] [ [ -[f|b] ] cmdfile] [ args]

选项
-c:从命令行执行expect脚本,默认expect是交互地执行的
示例:

expect -c 'expect "\n" {send"pressed enter\n"}

-d:可以输出输出调试信息

expect中相关指令

  • spawn:交互程序开始后面跟命令或者指定程序(在shell内启动这个进程)
  • expect:获取匹配信息匹配成功则执行expect后面的程序动作(检测由shell内进程发出的特定交互指令反馈字符串后向下执行)
  • send:用于向进程发送字符串(从shell外向shell内进程发送一条字符串,换行符为确认结束)
  • interact:允许用户交互
  • exp_continue:在expect中多次匹配就需要用到
  • send_user:用来打印输出 相当于shell中的echo
  • exit:退出expect脚本
  • eof:expect执行结束 退出
  • set:定义变量
  • puts:输出变量
  • set timeout:设置超时时间

示例

创建一个ssh_auto_login.exp脚本文件

#!/usr/bin/expect
set timeout 30 
spawn ssh -l username 192.168.1.1 
expect "password:" 
send "ispass\r"
interact 
  • 第一行 #!/usr/bin/expect 与执行方式有关, 当我么以 ./ssh_auto_login.exp,我们必须通过第一行告诉操作系统,脚本需要通过/usr/bin/expect程序来运行,而且我们还要授予文件可执行权限。
    如果我们以下面这种方式执行ssh_auto_login.exp, 也可以不用写第一行, 而且不必授予其可执行权限。

    expect ssh_auto_login.exp
    
  • 第二行 set timeout 30
    设置命令的命令的超时时间, 单位是秒。

  • 第三行 spawn ssh -l username 192.168.1.1
    spawn是expect程序的一个内置命令,使用它将我们需要执行的各种shell命令包裹起来,这样就shell命令注入了和自动化脚本expect, send交互的能力。只有包裹后程序才有机会获取shell命令的输出,并根据输出做出反应。

  • 第四行expect "password:"
    这个命令的意思是判断上次输出结果里是否包含“password:”的字符串
    如果有则立即返回否则就等待一段时间后返回这里等待时长就是前面设置的30秒。
    注意这里的用词是包含,也就是如果完整输出是"please in put your password:"也算中.这里的输出是指控制台的输出不是文件输出

  • 第五行 send "ispass\r"
    当expect接收到“password:”提示符,所以上面的expect实际是在等待“password:”提示符出现,使用等待多好,wait, 等待“password:”提示符出现后继续往后执行,紧接着地就是send命令, send将密码发送到控制台,这里是输入通道。
    第六行 interact
    使将控制权由机器控制转换为人机交互, 由人来接管。

except最常用的语法(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"}
}

示例

cat ssh1.exp

#!/usr/bin/expect
spawn ssh 192.168.8.100
expect {
"yes/no" { send"yes\n";exp_continue }
"password" { send"magedu\n" }
}
interact
#expect eof

示例:变量

cat ssh2.exp

#!/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

示例:位置参数

vim ssh3.exp

#!/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 rootmagedu

示例:执行多个命令

cat ssh4.exp

#!/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 magedu |passwd –stdin haha\n" }
send "exit\n"
expect eof
#./ssh4.exp 192.168.8.100 rootmagedu

示例:shell脚本调用expect

vim ssh5.sh

#!/bin/bash
ip=$1
user=$2
password=$3
expect
set timeout 10
spawn ssh$user@$ip
expect {
"yes/no" { send"yes\n";exp_continue}
"password" { send"$password\n" }
}
expect "]#" { send"useraddhehe\n" }
expect "]#" { send "echomagedu|passwd–stdinhehe\n" }
expect "]#" { send"exit\n" }
expect eof
EOF
#./ssh5.sh 192.168.8.100 rootmagedus

参考

linux中expect命令详解
https://www.lxlinux.net/255.html
Expect—百科篇命令
https://ipcmen.com/expect
Linux expect 详解
https://www.jianshu.com/p/daeafa6efe30

标签:set,send,expect,exp,linux,password,ssh
From: https://www.cnblogs.com/trigger-cn/p/16844277.html

相关文章

  • Linux 硬件管理
    实验环境:Linux5.19.13-arch1-1konsole22.08.21.简介计算机主要硬件:主板、CPU、GPU、内存、硬盘、声卡、显卡、电源、鼠标、屏幕等,这里只讨论普通计算机的硬件......
  • linux安装python3.10
    1.下载python包https://www.python.org/ftp/python/3.10.5/Python-3.10.5.tgz2.安装依赖包yuminstall-ygccpatchlibffi-develpython-develzlib-develbzip2-dev......
  • mount: you must specify the filesystem type(linux)
    先执行:​​mkfs.ext3/dev/sdb1​​再执行:......
  • Linux管道命令与shell编程(隐私版)
    管道相关命令目标​​cut​​​​sort​​​​wc​​​​uniq​​​​tee​​​​tr​​​​split​​​​awk​​​​sed​​准备工作vimscore.txtzhangsan689926lisi......
  • linux常用命令(一【公开版】)
    目录​​常用的:​​​​help:(2种)​​​​文件和目录常用命令​​​​LS:​​​​cd:​​​​touch:​​​​rm: ​​​​拷贝和移动文件:​​​​查看文件内容:​​​​c......
  • vm中挂载linux持有系统镜像光驱
    挂载持有系统镜像光驱5.1目标因为linux系统镜像中包含了常用的软件包,就不用从网上下载了所以需要挂载持有系统镜像的光驱5.2路径第一步:将linux系统镜像放到光驱......
  • Linux各目录及每个目录的详细介绍
    Linux各目录及每个目录的详细介绍【常见目录说明】目录 /bin存放二进制可执行文件(ls,cat,mkdir等),常用命令一般都在这里。/etc存放系统管理和配置文件/home存放所有用户文......
  • linux入门(隐私版)
    1操作系统的概述:1.1操作系统:操作系统作为接口的示意图:注意:大数据(电脑配置:内存16g,固态520g)1.2不同的操作系统1.2.1桌面操作系统1.2.2服务器操作系统 1.2.3嵌入式操作系统 L......
  • Linux网络配置
    一.网络地址配置1.1网络地址查看–ifconfig命令格式:ifconfig或ifconfig+网卡名主要参数信息:1.2网络配置修改临时修改IP地址ifconfig+网卡+......
  • 如何在linux安装jdk?
    第一步:卸载linux系统提供的jdk#查询已安装的jdkrpm-qa|grepjava#卸载rpm-e--nodeps软件包名第二步:上传按照包到​​/export/softwares​​​,解压到​​/......