expect使用小结

1)使用expect命令在shell脚本中实现的简单登陆

#!/bin/bash
#!/usr/bin/expect -f

if [ $# != 3 ];then
    echo "Usage: cmd [host] [user] [passwd]"
fi

host=$1
user=$2
password=$3

echo
echo $user@$host
echo

#eof {exit 0}
auto_ssh_copy_id ()
{
    expect -c "set timeout -1;
        spawn ssh $2;
        expect
        {
            "*yes/no" {send "yes\r"; exp_continue}
            "*password:" {send "$1\r"}
        };
        interact
    "
}

#auto_ssh_copy_id $password $user@$host

set ip [lindex $argv 0 ]
set password [lindex $argv 1 ]
set timeout 10
spawn ssh root@$ip
    expect {
        "*yes/no" { send "yes\r"; exp_continue}
        "*password:" { send "$password\r" }
    }
interact

exit 0

    2)expect脚本简单使用(*.exp 脚本)

    #!/usr/bin/expect -f
    
    set ip 192.168.2.101
    set password "321"
    
    spawn ssh root@$ip
    
    expect {
         "*yes/no" {send "yes\r"; exp_continue}
         "*password:" {send "$password\r" }
    }
    
    expect "#*"
    send "passwd\r"
    
    expect {
        "Enter*" { send "123\r"; exp_continue}
        "Retype*" { send "123\r"; exp_continue}
    }
    
    send "exit\r"
    expect eof
    
    exit


    3)自动登陆shell代码段

    ……
    
    auto_ssh_login ()
    {
        expect -c "
            set timeout 5
    
            spawn sshpass -p $2 ssh -o StrictHostKeyChecking=no -p 39000 root@$1
    
            send "passwd\\r"
            
            expect {
                Enter* {send -- $3\r; exp_continue}
                Retype* {send -- $3\r; exp_continue}
            }
    
            send "exit\\r"
            expect eof
        "
    }
    
    ……


    暂时更新到这里, 继续研究

    在编程或自动化测试中,`try` 和 `expect` 并不是必须结合使用的结构。它们分别属于不同的语义范畴,用途也有所不同。 - `try` 通常用于异常处理机制中,表示尝试执行某段代码,并在发生异常时捕获它以便进行相应的处理。这种结构常见于多种编程语言中,例如 Python 中的 `try...except` 块[^1]。 ```python try: # 尝试执行的代码 result = 10 / 0 except ZeroDivisionError as e: # 处理除以零的异常 print(f"An error occurred: {e}") ``` - `expect` 则更多出现在测试框架中,特别是在行为驱动开发(BDD)或断言库中,用来声明对程序状态或输出的预期结果。例如,在某些测试框架中使用 `expect(value).to(equal(5))` 这样的语法来验证值是否符合预期[^2]。 虽然两者可以独立存在,但在一些特定的测试场景下,它们可能会被一起使用。例如,在编写单元测试时,可能希望验证某个操作是否会抛出预期的异常。这时可以在 `try` 块中触发异常,并通过 `expect` 来确认是否捕获到了正确的异常类型[^3]。 ```python from pytest import raises def test_exception(): with raises(ValueError): # 使用了类似 expect 的方式来期望一个异常 try: raise ValueError("This is a test exception.") except Exception as e: assert isinstance(e, ValueError) raise # 重新抛出异常以便 pytest 能够捕捉到它 ``` 在这个例子中,`try` 主要是为了演示如何捕获异常并再次抛出,而 `raises` 是 Pytest 提供的一个上下文管理器,其作用类似于 `expect`,用于断言接下来的代码块会引发指定类型的异常[^4]。 总结来说,`try` 和 `expect` 不是强制性地绑定在一起使用,而是根据实际需要灵活选择是否同时应用。在测试逻辑中,它们有时会被共同利用以更好地控制流程和验证结果。
    评论
    成就一亿技术人!
    拼手气红包6.0元
    还能输入1000个字符
     
    红包 添加红包
    表情包 插入表情
     条评论被折叠 查看
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值