AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1121345
Accepted
zbro
zbro
Asked: 2023-01-28 14:55:12 +0800 CST2023-01-28 14:55:12 +0800 CST 2023-01-28 14:55:12 +0800 CST

如何在 Ansible 中启动 netcat 监听服务器?

  • 772

我正在研究更大的 Ansible 剧本。我想使用的任务之一是使用netcat. 在其中一台主机上,我想启动一个netcat“服务器”,然后启动“客户端”以将任何字符串发送到服务器。

我在没有 Ansible 的情况下对此进行测试——一切正常,但在 Ansible 中netcat返回1。我知道这意味着网络连接没有建立。这是我的netcat服务器任务:

- shell: |
    nc -d -q0 -l 1234
  poll: 0
  async: 60

但是,这是行不通的。我试过在命令末尾添加 & 号,但没有成功。我做的另一件事是将 poll 设置为 > 0,然后检查“服务器”主机上的端口是否已打开(通过netstat -tulpn)。不是。

我在这里错过了什么?

ansible
  • 1 1 个回答
  • 80 Views

1 个回答

  • Voted
  1. Best Answer
    U880D
    2023-01-29T04:33:56+08:002023-01-29T04:33:56+08:00

    在 CLI 上执行上述命令将导致输出

    nc -d -q0 -l 1234 &
    [1] 123456
    ~/test$ Ncat: Invalid -d delay "-q0" (must be greater than 0). QUITTING.
    ^C
    [1]+  Exit 2                  nc -d -q0 -l 1234
    

    其中一个最小的示例剧本

    ---
    - hosts: test
      become: false
      gather_facts: false
    
      tasks:
    
      - name: Start LISTENer on Remote Node
        shell:
          cmd: "nc --listen 1234"
        poll: 0
        async: 60 # seconds
    
      - name: Test from Control Node
        delegate_to: controlnode.example.com
        wait_for:
          host: "{{ inventory_hostname }}"
          port: 1234
          state: drained
          delay: 0
          timeout: 3 # seconds
          active_connection_states: SYN_RECV
    
      - name: Test via nc
        delegate_to: controlnode.example.com
        shell:
          cmd: "nc -vz {{ inventory_hostname }} 1234"
        register: result
    
      - name: Show result
        debug:
          var: result
    

    将导致输出

    TASK [Start LISTENer on Remote Node] ************************
    changed: [test1.example.com]
    changed: [test2.example.com]
    changed: [test3.example.com]
    
    TASK [Test from Control Node] *******************************
    ok: [test1.example.com -> controlnode.example.com]
    ok: [test2.example.com -> controlnode.example.com]
    ok: [test3.example.com -> controlnode.example.com]
    
    TASK [Test via nc] ******************************************
    changed: [test1.example.com -> controlnode.example.com]
    changed: [test2.example.com -> controlnode.example.com]
    changed: [test3.example.com -> controlnode.example.com]
    
    TASK [Show result] ******************************************
    ok: [test1.example.com] =>
      result:
        changed: true
        cmd: nc -vz test1.example.com 1234
        delta: '0:00:00.034145'
        end: '2023-01-28 13:30:00.291207'
        failed: false
        msg: ''
        rc: 0
        start: '2023-01-28 13:30:00.257062'
        stderr: |-
          Ncat: Version 7.50 ( https://nmap.org/ncat )
          Ncat: Connected to 192.168.2.1:1234.
          Ncat: 0 bytes sent, 0 bytes received in 0.02 seconds.
        stderr_lines:
        - 'Ncat: Version 7.50 ( https://nmap.org/ncat )'
        - 'Ncat: Connected to 192.168.2.1:1234.'
        - 'Ncat: 0 bytes sent, 0 bytes received in 0.02 seconds.'
        stdout: ''
        stdout_lines: []
    ...
    

    关于

    我想使用的任务之一是执行“TCP ping”...

    建议使用wait_for模块 – 在继续之前等待条件,而不是

    ...使用 netcat 进行“TCP ping”。

    正如人们已经从提供的示例输出中看到的那样,并且由于结果集和行为的差异。

    进一步的文件

    • 异步操作和轮询 - 同时运行任务:poll = 0
    • 0

相关问题

  • Ansible:将字符串转换为字典

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve