V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
crytis
V2EX  ›  Linux

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

  •  
  •   crytis · 2017-11-07 20:17:31 +08:00 · 3222 次点击
    这是一个创建于 2333 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我在脚本里用 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 条回复    2020-09-07 15:20:24 +08:00
    araraloren
        1
    araraloren  
       2017-11-08 09:12:17 +08:00
    ssh 应该可以获取执行的程序的返回值

    try this: echo $(ssh -f -o StrictHostKeyChecking=no [email protected] ls)
    crytis
        2
    crytis  
    OP
       2017-11-08 10:15:23 +08:00
    @araraloren 但是我要用 expect 输入密码
    dongrenwen
        3
    dongrenwen  
       2020-09-07 15:19:14 +08:00
    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  
       2020-09-07 15:20:24 +08:00
    ```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
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   952 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 21:06 · PVG 05:06 · LAX 14:06 · JFK 17:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.