expect学习

简介: 1. Expect a program to control interactive applications it maybe possible to control interactive applications from other scripting languages as well python, perlexpect programming

1. Expect

  • a program to control interactive applications
    • it maybe possible to control interactive applications from other scripting languages as well
      • python, perl
  • expect programming is very easy compared to perl!
    • crackers use expect to try and gain access to machines via ssh, ftp, telnet
      • ask AJC Blyth


2. Expect example uses

  • as system administrators we may have need for automating interactive activities
    • testing remote access works (telnet, ftp)
    • down loading ftp files
    • changing password
    • running security checks
  • we could build a script which test that our server is safe
    • from guest accounts
    • no telnet access is allowed
    • no ssh from lab machines is allowed
      • but ssh from privileged machines is working


3. Expect and examples

  • automatically creating passwords
    • can be done via C
      • takes a lot of time to write such programs
      • will it work with NIS, shadow passwords or Kerboros?
  • with expect we run the user program passwd and send keyboard input


4. Password Trial

  • advice first run the program that you want to connect to expect by hand
    • note the output and build expect around it
    • so let us change a password for bob
  • passwd bob
    Enter new UNIX password:
    Retype new UNIX password:
    passwd: password updated successfully

    • passwords were entered, but obviously the passwd program did not echo back our input! 


5. Trial results

  • we note that the program prompted us for a password twice
    • both times it ended its sentence with password:
    • as we are lazy we can wait until we see password: and ignore anything that was before
      • why is this good practice?
      • what must we watch out for?
  • #!/usr/bin/expect
    
    
    spawn passwd [lindex $argv 0]
    set password [lindex $argv 1]
    expect "password:"
    send "$password\r"
    expect "password:"
    send "$password\r"
    expect eof


6. Testing the example

  • ./exp1.exp bob 123
    spawn passwd bob
    Enter new UNIX password:
    Retype new UNIX password:
    passwd: password updated successfully

  • here we change user bob password to 123 


7. Script explanation

  • #!/usr/bin/expect
    
    
    spawn passwd [lindex $argv 0]
    set password [lindex $argv 1]
    expect "password:"
    send "$password\r"
    expect "password:"
    send "$password\r"
    expect eof

  • #!/usr/bin/expect script is interpreted via expect located in directory /usr/bin
  • spawn passwd [lindex $argv 0]
    • run the program passwd bob and connect expect to this program
    • note that [lindex $argv 0] resolves to bob
      • actually argument one (hmm..) 


8. Script explanation

  • set password [lindex $argv 1]
    • defines a variable password and sets it to 123
  • expect "password:"
    • waits for the program passwd to issue password: before continuing
  • send "$password\r"
    • sends the users password to passwd followed by a carriage return
  • note the script repeats the last two commands, why?
  • finally expect eof wait for passwd to finish


9. Anchoring

  • you might want to match text at the beginning or end of a line, this is via
    • ^ for the beginning of a line
    • $ for the end of a line
  • also note that * means any number of characters


10. Pattern action pairs

  • #!/usr/bin/expect
    
    set timeout 15
    
    
    expect "hi"  { send "You said hi\n" } \
         "hello"  { send "Hello to you\n" } \
         "bye"    { send "Goodbye\n" } \
         timeout  { send "I’m fed up\nbye\n" }

  • if we run the script as shown below and type nothing we get:
  • I’m fed up
    bye

  • note that different actions can be associated with different input
    • note also that the default timeout time is set at 10 seconds to disable the timeout facility
    • set timeout -1 


11. Autoftp and expect

  • so far we have built the front end to autoftp
    • scans the input file for URLs
    • handles arguments
  • we will use expect to control ftp, we will build this up this utility
  • firstly we will ftp manually


12. Ftp session

  • fred@merlin:$ ftp guenevere
    Connected to guenevere.
    220 guenevere FTP server (Version wu-2.6.0(1))
    Name (guenevere:fred): anonymous
    331 Guest login ok, send your complete e-mail
    331 address as password.
    Password:nobody@nowhere.com
    
    
    230-Welcome, archive user anonymous@merlin !
    230-
    230-The local time is: Mon Feb 12 15:18:14 2001
    230-
    230 Guest login ok, access restrictions apply.
    Remote system type is UNIX.
    Using binary mode to transfer files.

  • ftp> dir
    200 PORT command successful.
    150 Opening ASCII mode data connection for /bin/ls.
    total 20
    d--x--x--x    2 0 0 4096 Aug 10  2000 bin
    d--x--x--x    2 0 0 4096 Aug 10  2000 etc
    d--x--x--x    2 0 0 4096 Aug 10  2000 lib
    dr-xr-xr-x    4 0 0 4096 Sep 28 22:17 pub
    -rw-r--r--    1 0 0  346 Aug 10  2000 welcome.msg
    226 Transfer complete.
    ftp> quit
    221-You have transferred 0 bytes in 0 files.
    221-Total traffic for this session was 1182
    221-bytes in 1 transfers.
    221-Thank you for using the FTP service on
    221-guenevere. Goodbye.


13. Simple semi automated ftp script

  • this script will log us into a site and then interact with the user
  • #!/usr/bin/expect
    
    
    set site [lindex $argv 0]
    spawn ftp $site
    expect "Name"
    send "anonymous\r"
    expect "Password:"
    send "nobody@nowhere.com\r"
    interact

  • note the interact statement
    • connects the keyboard to the ftp program
  • most ftp servers do not check for a valid email address! 


14. Expect control constructs

  • expect uses extends the Tcl language
  • expect provides: if then else, while, set, interact, expect, for, switch, incr, return, proc
  • if {$count < 0} {
       set total 1
    }

  • if {$count < 5} {
       puts "count is less than five"
    } else {
       puts "count is not less than five"
    }

  • you must place the statement begin brace on the same line as the if or else
    • if in doubt follow the templates in these notes 


15. More complex if

  • if {$count < 0} {
       puts "count is less than zero"
    } elseif {$count > 0 {
       puts "count is greater than zero"
    } else {
       puts "count is equal to zero"
    }


16. While statement

  • #!/usr/bin/expect
    
    
    set count 10
    while {$count > 0} {
       puts "the value of count is $count"
       set count [expr $count-1]
    }

  • note the effect of the braces in {$count > 0}
    • they defer evaluation of $count
    • if you remove the braces then $count > 0 is internally replaced via: 10 > 0 


17. for command

  • has the syntax

    for start expression next {

    }

  • #!/usr/bin/expect
    
    
    for {set count 10} {$count > 0} {incr count -1} {
       puts "the value of count is $count"
    }


18. Expressions

  • 0 is false, 1 is true
  • boolean operators || (or), && (and), ! (not)
  • comparison operators <=, ==, != etc
  • the brackets [ ] give this expression a higher precedence
  • so in the example
  • set count [expr $count-1]

  • the [expr $count-1] is evaluated before the set! 


19. proc and return

  • #!/usr/bin/expect
    
    proc mycompare {a b} {
        if {$a < $b} {
            puts "$a is less than $b"
            return -1
        } elseif {$a > $b} {
            puts "$a is greater than $b"
            return 1
        } else {
            puts "$a is equal to $b"
            return 0
        }
    }
    
    
    set value [mycompare 1 4]
    puts "comparison returned $value"

  • ./exp6.exp
    1 is less than 4
    comparison returned -1


20. Autoftp Tutorial

  • #!/usr/bin/expect
    # this program is called exp7.exp
    proc connect {} {
       expect {
           "Name*:" {
               send "anonymous\r"
               expect {
                   "Password:" {
                       send "nobody@nowhere.com\r"
                       expect "login ok*ftp>"
                       return 0
                   }
               }
           }
       }
       # timed out
       return 1
    }

  • set site [lindex $argv 0]
    spawn ftp $site
    while {[connect]} {
        send "quit\r"
        expect eof
        spawn ftp $site
    }
    send "binary\r"
    send "cd  [lindex $argv 1]\r"
    send "get [lindex $argv 2]\r"
    send "quit\r"
    expect eof


21. Laboratory session

目录
相关文章
|
24天前
|
人工智能 JSON API
全网刷屏的 Jev 模型正式开放!一手实战测评 + 保姆级教程
全网爆火的 Jev 模型是什么?有什么用?怎么使用?怎么接入 AI 编程工具?效果真的好么?傻子可懂的 Jev 保姆级实战教程 + 项目实战测评来啦
9477 31
|
1天前
|
云安全 人工智能 运维
当攻击全面Agent化,你的安全防御能否跟上AI Speed?
多起客户侧AK泄露事件应急的真实观察
1154 0
|
10天前
|
存储 人工智能 并行计算
大模型本地部署终端选型方法论:以 Qwen3.8-27B 为例的四档分层完整流程
本文提出一套大模型本地部署终端选型方法论:定约束、定档位、定框架、定参数四步决策法,配合入门、主力、质量、无损四档分层模型。以 Qwen3.8-27B 实测数据为例,逐环节解读显存、带宽、存储、散热、系统、预算等要素,给出面向不同预算的优选方案、决策自查清单与市场观察框架。文末前瞻 AI 笔记本的 CPU+GPU 与统一内存两条路线,论证四步决策法在新品类上的延续性。
|
9天前
|
人工智能 JSON 自然语言处理
2026 年 Jev 决策模型深度拆解:原理解读、实战测评与保姆级落地教程
有一款特殊AI模型在开发者圈子刷屏,它摒弃传统大模型擅长的对话聊天能力,专注做高速结构化决策,它就是TypeSafe AI推出的Jev模型。该模型由ChatGPT共同发明人Diogo Almeida主导研发,定位为**System One Model(系统一模型)**,对标人类大脑快速直觉判断的思维模式,在响应延迟、调用成本、结构化输出稳定性上相比传统生成式大模型有着巨大差异。本文会完整拆解Jev底层原理、三大核心原语能力、适用业务场景,同时提供可直接运行的curl、Python代码示例,并且结合多组实测数据,客观分析模型优势与能力边界,帮助普通开发者和AI应用从业者快速上手落地。
715 1
|
5天前
|
人工智能 JSON 自然语言处理
2026 年 Jev 决策模型全解析:核心原理拆解,性能实战测评与落地部署教程
有一款特殊AI模型在开发者圈子刷屏,它摒弃传统大模型擅长的对话聊天能力,专注做高速结构化决策,它就是TypeSafe AI推出的Jev模型。该模型由ChatGPT共同发明人Diogo Almeida主导研发,定位为System One Model(系统一模型),对标人类大脑快速直觉判断的思维模式,在响应延迟、调用成本、结构化输出稳定性上相比传统生成式大模型有着巨大差异。本文会完整拆解Jev底层原理、三大核心原语能力、适用业务场景,同时提供可直接运行的curl、Python代码示例,并且结合多组实测数据,客观分析模型优势与能力边界,帮助普通开发者和AI应用从业者快速上手落地。
467 0
|
22天前
|
人工智能 测试技术 API
最近全网爆火的 Jev 到底是什么?适合干什么、怎么用,一篇讲透!
Jev是TypeSafe AI推出的“系统一模型”,不生成文本,专做毫秒级结构化决策:Choice(多选)、Score(打分)、Noul(是非概率)。响应快193倍、成本低444倍,适合工单路由、内容审核、测试定级等高频判断场景。
2542 4
最近全网爆火的 Jev 到底是什么?适合干什么、怎么用,一篇讲透!
|
4天前
|
存储 缓存 测试技术
QWT 3D 模块重构记:旧渲染管线改进介绍及实时瀑布图示例
QWT 3D模块完成重大重构:解耦窗口与图形(Plot+Item),摒弃OpenGL 1.x固定管线,迁至Core Profile着色器渲染;支持多图元同场景叠加,并以实时3D瀑布图验证——滑动窗口、固定色域、64帧×256点达61fps。兼容Qt 5.12+/6,LGPL协议。
174 120