首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在使用expect和send模块时,send不能正常工作。

在使用expect和send模块时,send不能正常工作。
EN

Stack Overflow用户
提问于 2020-03-10 07:34:07
回答 1查看 1.3K关注 0票数 1

我尝试了Shell模块的"expect“和"send”功能,代码正在运行,但它没有发送密码,它只是退出了剧本。

代码语言:javascript
复制
 ---
    - hosts: localhost
      become: yes
      vars:
      - remote_password: redhat
      tasks:
      - name: connecting to a remote host not an ansible slave
        shell: |
         set timeout 300
         spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.48.133
         expect "password: "
         send "{{ remote_password }}\n"
         interact
         exit 0
        args:
         executable: /usr/bin/expect

输出:

代码语言:javascript
复制
 "stdout_lines": [
        "spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.48.133",
        "/bin/ssh-copy-id: INFO: Source of key(s) to be installed: \"/root/.ssh/id_rsa.pub\"",
        "/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed",
        "/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys",
        "root@192.168.48.133's password: "
    ]
}

不知道为什么这不管用。为什么它不提供密码。

EN

回答 1

Stack Overflow用户

发布于 2021-10-19 09:06:48

您应该使用\r而不是\n,并删除双引号。下面是工作代码。

代码语言:javascript
复制
     spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.48.133
     expect "password: "
     send {{ remote_password }}\r
     interact
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60613201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档