ping.py 702 B

1234567891011121314151617181920212223242526272829303132
  1. import subprocess
  2. from pprint import pprint
  3. import typer
  4. def ping_ip(ip_address: str, count: int) -> bool:
  5. """
  6. Ping IP_ADDRESS and return True/Flase
  7. """
  8. reply = subprocess.run(
  9. f"ping -c {count} -n {ip_address}",
  10. shell=True,
  11. stdout=subprocess.PIPE,
  12. stderr=subprocess.PIPE,
  13. encoding="utf-8",
  14. )
  15. if reply.returncode == 0:
  16. return True
  17. else:
  18. return False
  19. def cli(ip: str, count: int):
  20. result = ping_ip(ip, count)
  21. if result:
  22. print(f"IP {ip} пингуется")
  23. else:
  24. print(f"IP {ip} не пингуется")
  25. if __name__ == '__main__':
  26. typer.run(cli)