V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
crytis
V2EX  ›  Linux

如何读取 expect 的 output 到一个变量?

  •  
  •   crytis · Nov 7, 2017 · 3947 views
    This topic created in 3105 days ago, the information mentioned may be changed or developed.

    我在脚本里用 expect ssh 到机器上执行命令,比如 ls,如何把这个结果拿回来在脚本里使用?

    expect -c "
        spawn  ssh -f -o StrictHostKeyChecking=no [email protected] ls
        expect {
            \"*assword\" {set timeout 6000; send \"123456\`\n\"; exp_continue ; sleep 30; }
            \"yes/no\" {send \"yes\n\"; exp_continue;}
    
            }
        interact"
    

    搜了半天没整明白

    4 replies    2020-09-07 15:20:24 +08:00
    araraloren
        1
    araraloren  
       Nov 8, 2017
    ssh 应该可以获取执行的程序的返回值

    try this: echo $(ssh -f -o StrictHostKeyChecking=no [email protected] ls)
    crytis
        2
    crytis  
    OP
       Nov 8, 2017
    @araraloren 但是我要用 expect 输入密码
    dongrenwen
        3
    dongrenwen  
       Sep 7, 2020
    ret=$(
    expect -c "
    spawn ssh -f -o StrictHostKeyChecking=no [email protected] ls
    expect {
    \"*assword\" {set timeout 6000; send \"123456\`\n\"; exp_continue ; sleep 30; }
    \"yes/no\" {send \"yes\n\"; exp_continue;}

    }
    interact"
    )

    cat <<EOF
    ${ret}
    EOF
    dongrenwen
        4
    dongrenwen  
       Sep 7, 2020
    ```bash
    ret=$(
    expect -c "
    spawn ssh -f -o StrictHostKeyChecking=no [email protected] ls
    expect {
    \"*assword\" {set timeout 6000; send \"123456\`\n\"; exp_continue ; sleep 30; }
    \"yes/no\" {send \"yes\n\"; exp_continue;}

    }
    interact"
    )

    cat <<EOF
    ${ret}
    EOF
    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1326 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 17:30 · PVG 01:30 · LAX 10:30 · JFK 13:30
    ♥ Do have faith in what you're doing.