Skip to content

Instantly share code, notes, and snippets.

@DanAmt
Last active February 8, 2022 15:35
Show Gist options
  • Save DanAmt/132e8dd4fd5abe4f982ca6dab64784f6 to your computer and use it in GitHub Desktop.
Save DanAmt/132e8dd4fd5abe4f982ca6dab64784f6 to your computer and use it in GitHub Desktop.
Here we can test a socket without netstat telnet and so on
#!/bin/python
#For socket checking in case no tools around except python
import socket
import sys
if len(sys.argv) != 3:
print("Usage: " + sys.argv[0] + " <IP or Name> <Port>")
sys.exit(2)
s = socket.socket()
address = str(sys.argv[1])
port = int(sys.argv[2]) # port number is a number, not string
ipaddress = socket.gethostbyname(sys.argv[1])
print("IPaddress checked is " + ipaddress + " " + str(port) )
result = s.connect_ex((address, port))
s.close()
if result:
print("Problem with socket!")
else:
print("Socket ok!")
print("Returnvalue is " + str(result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment