Written by: Helio Loureiro
Category: Python
Hits: 3824

Hoje chegou um mail pedindo pra testar uma mudança de máquinas que saíram da empresa pra irem habitar o cloud.  A tarefa era testar máquina e porta.  Algumas máquinas com uma porta somente, outras com várias.  E todas TCP.

Pra fazer isso rapidamente eu escrevi um script em python3 que basicamente estabelece uma conexão TCP e mostra OK se conectar ou FAIL se não conseguir.  Bem básico, mas resolveu meu problema muito mais rápido que se eu fosse testar máquina por máquina, porta por porta provavelmente com o comando telnet.

#! /usr/bin/python3

import socket

servers = [ 
        "helio.loureiro.eng.br:443", 
        "helio.loureiro.eng.br:389", 
        "helio.loureiro.eng.br:8081", 
        "helio.loureiro.eng.br:80", 
        "helio.loureiro.eng.br:22" 
        ]

for server in servers:
    try:
        host, port = server.split(":")
        port = int(port)
        socket.create_connection((host, port), 3)
        print(server, "OK")
    except:
        print(server, "FAIL")

O resultado:

helio.loureiro.eng.br:443 OK
helio.loureiro.eng.br:389 FAIL
helio.loureiro.eng.br:8081 FAIL
helio.loureiro.eng.br:80 OK
helio.loureiro.eng.br:22 OK

Boa diversão!