Skip to content

Commit

Permalink
Added utils.is_port_open()
Browse files Browse the repository at this point in the history
  • Loading branch information
p0dalirius committed Jun 24, 2024
1 parent e121047 commit bd39492
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions smbclientng/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import ntpath
import os
import re
import socket
import stat


Expand Down Expand Up @@ -399,3 +400,28 @@ def resolve_remote_files(smbSession, arguments):
resolved_files.append(arg)
resolved_files = sorted(list(set(resolved_files)))
return resolved_files


def is_port_open(target, port) -> bool:
"""
Check if a specific port on a target host is open.
This function attempts to establish a TCP connection to the specified port on the target host.
If the connection is successful, it indicates that the port is open. If the connection fails,
it indicates that the port is closed or the host is unreachable.
Args:
target (str): The hostname or IP address of the target host.
port (int): The port number to check.
Returns:
bool: True if the port is open, False otherwise.
"""

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(0.1)
# Non-existant domains cause a lot of errors, added error handling
try:
return s.connect_ex((target, port)) == 0
except Exception as e:
return False

0 comments on commit bd39492

Please sign in to comment.