ryandaniels.connectivity_test

ansible-role-connectivity-test

使用 nc(netcat)测试与指定 IP 和端口(TCP 或 UDP)的网络问题。
根据目标 IP 动态确定使用的源 IP。

默认使用 TCP 协议。如果需要测试 UDP,可以指定 protocol: udp

测试的发行版

  • Ubuntu 18.04 / 16.04
  • CentOS / RHEL: 7.x, 6.5, 5.9

依赖项

操作系统命令:

  • nc(netcat) - 如果未安装,将会被安装
  • ip
  • awk

默认设置

  • 启用调试
debug_enabled_default: false
  • 超时时间
connectivity_test_wait_default: 5
  • 如果在配置中指定了“udp”,则使用的 UDP 参数
connectivity_test_udp_command: "-u"
  • 代理
proxy_env: []

示例配置文件 (group_vars/xxx/connectivity-test-vars.yml)

默认使用 TCP 协议(无需指定)。

---
connectivity_test_destinations:
  - { ip: 192.168.56.10, port: 22 }
  - { ip: 192.168.56.10, port: 5000 }
  - { ip: 127.0.0.1, port: 53, protocol: udp }

示例剧本 connectivity-test.yml

---
- hosts: '{{inventory}}'
  become: yes
  roles:
  - connectivity-test

使用方法

ansible-playbook connectivity-test.yml --extra-vars "inventory=all-dev" -i hosts

跳过安装软件包(如果已知已经存在 - 加快任务速度)

ansible-playbook connectivity-test.yml --extra-vars "inventory=all-dev" -i hosts --skip-tags connectivity_install_pkg

示例输出

TASK [setup] *******************************************************************
ok: [centos7]

TASK [connectivity-test : Install dependant packages in RedHat based machines] *
ok: [centos7] => (item=[u'nc'])

TASK [connectivity-test : Get source IP based on destination IP] ***************
ok: [centos7] => (item=source ip: 127.0.0.1, destination ip: 127.0.0.1)
ok: [centos7] => (item=source ip: 127.0.0.1, destination ip: 127.0.0.1)
ok: [centos7] => (item=source ip: 192.168.56.9, destination ip: 192.168.56.10)

TASK [connectivity-test : debug] ***********************************************

TASK [connectivity-test : test network connectivity] ***************************
ok: [centos7] => (item=source ip: 192.168.56.9, destination ip: 192.168.56.10, port: 22, protocol: tcp, time: 2019-07-09 23:02:56.518272, stdout: SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.8, stderr: Connection to 192.168.56.10 22 port [tcp/ssh] succeeded!

)
ok: [centos7] => (item=source ip: 127.0.0.1, destination ip: 127.0.0.1, port: 53, protocol: udp, time: 2019-07-09 23:03:12.337049, stdout: , stderr: Connection to 127.0.0.1 53 port [udp/domain] succeeded!

)
failed: [centos7] (item=source ip: 192.168.56.9, destination ip: 192.168.56.10, port: 5000, protocol: tcp, time: 2019-07-09 23:03:33.893978, stdout: , stderr: nc: connect to 192.168.56.10 port 5000 (tcp) timed out: Operation now in progress

)

TASK [connectivity-test : debug] ***********************************************

PLAY RECAP *********************************************************************
centos7                    : ok=3    changed=0    unreachable=0    failed=1  

手动方式

dIP="192.168.56.10 192.168.56.13";
dPORT="5000";
sIP=$(/sbin/ip route get "$(echo $dIP|awk -F" " 'NR==1{print $1}')"|awk 'NR==1{print $NF}');
uname -n;
echo "源 IP: $sIP";
for i in $dIP;
do date;
nc -v -w 3 -s "$sIP" "$i" "$dPORT";
echo " ";
done
关于项目

Role to test for network issues to specified IP and port (TCP or UDP) from your Ansible hosts.

安装
ansible-galaxy install ryandaniels.connectivity_test
许可证
mit
下载
5k
拥有者
Ansible all the things