expect自动化交互应用程序工具

2024-06-09 1664阅读

目录

expect自动化交互应用程序工具
(图片来源网络,侵删)

安装 expect

语法介绍

基本命令

控制结构

变量和字符串

样例 expect 脚本

Example 1: 自动登录 SSH 并执行命令

Example 2: 自动与 FTP 服务器交互

Example 3: 与 telnet 自动交互

调试与运行脚本

调试模式

运行脚本

sshpass与expect应用-CSDN博客


  expect 是一个用于自动化交互应用程序的工具,主要在 UNIX 系统上使用。它可以通过脚本控制和自动化与其他交互式程序的对话,例如 ssh、ftp、telnet 等等。expect 使得自动执行一些需要用户输入的命令变得非常简单和方便。

安装 expect

sudo yum -y install expect

语法介绍

基本命令
  • spawn:启动一个进程并进入交互模式。
  • expect:等待特定的模式或字符串出现。
  • send:发送字符串到启动的进程(模拟用户输入)。
  • interact:允许用户接管控制,手动进行交互。
  • exp_continue:继续期待下一个模式匹配,这是在 expect 块中常用的命令。
  • expect eof :  结束expect匹配
    控制结构
    • if:条件判断语句。
    • while:循环语句。
    • for:for 循环语句。
      变量和字符串
      • set:定义和赋值变量。
      • $:用于引用变量。

        样例 expect 脚本

        Example 1: 自动登录 SSH 并执行命令
        #!/usr/bin/expect
        # 设置超时时间为20秒
        set timeout 20
        # 变量定义
        set host "your.server.com"
        set user "your_username"
        set password "your_password"
        # 开始SSH会话
        spawn ssh $user@$host
        # 等待各种可能的提示信息
        expect {
            "yes/no" { 
                send "yes\r"; 
                exp_continue 
            }
            "*assword:" { 
                send "$password\r" 
            }
        }
        # 执行命令
        expect "$ " { send "ls -l\r" }
        expect "$ " { send "echo 'Hello, World!'\r" }
        expect "$ " { send "exit\r" }
        # 等待会话结束
        expect eof
        
        Example 2: 自动与 FTP 服务器交互
        #!/usr/bin/expect
        # 设置超时时间
        set timeout 20
        # 变量设置
        set server "ftp.example.com"
        set user "your_username"
        set password "your_password"
        # 开始FTP会话
        spawn ftp $server
        # 期待用户名提示
        expect "Name*"
        send "$user\r"
        # 期待密码提示
        expect "Password:"
        send "$password\r"
        # 登录后的交互
        expect "ftp>" { send "ls\r" }
        expect "ftp>" { send "get example.txt\r" }
        expect "ftp>" { send "bye\r" }
        # 等待会话结束
        expect eof
        
        Example 3: 与 telnet 自动交互
        #!/usr/bin/expect
        # 设置超时时间
        set timeout 20
        # 变量设置
        set host "your.telnetserver.com"
        set user "your_username"
        set password "your_password"
        # 启动telnet会话
        spawn telnet $host
        # 等待各种可能的提示信息
        expect {
            "login:" { send "$user\r" }
            "Username:" { send "$user\r" }
            "*username:" { send "$user\r" }
        }
        # 期待出现的密码提示
        expect "*assword:" { send "$password\r" }
        # 登录后执行命令
        expect "> " { send "help\r" }
        expect "> " { send "exit\r" }
        # 等待会话结束
        expect eof

        调试与运行脚本

        调试模式

        使用 expect 提供的一些选项可以进行调试。

        • -d:启用调试模式,输出期望和接收到的内容。
        • log_user 1 or log_user 0:打开或关闭标准输出日志。打开时,所有 send 和 expect 操作都会在终端输出。关闭时则不会。

          例如:

          expect -d ./your_expect_script.exp
          
          运行脚本

          保存脚本到文件并确保它具有可执行权限:

          chmod +x your_expect_script.exp
          ./your_expect_script.exp
VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]