Skip to content

Instantly share code, notes, and snippets.

@Junzki
Created November 15, 2021 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Junzki/868212f6a08f8027ec9e25aabef42c8e to your computer and use it in GitHub Desktop.
Save Junzki/868212f6a08f8027ec9e25aabef42c8e to your computer and use it in GitHub Desktop.
# -*- coding:utf-8 -*-
"""
BSD 3-Clause License
Copyright (c) 2021, Andrew Junzki
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import socket
import paramiko
from typing import Optional
DEFAULT_TIMEOUT = 1
PORTS_RANGE = (1, 65535)
PORTS = (
7, # Echo
20, # FTP
21, # Telnet
22, # SSH
23, # Samba
80, # HTTP
139, # Samba
443, # HTTPS
445, # Samba
1080, # SOCKSv5
1433, # MSSQL
3306, # MySQL
3389, # RDP
5432, # PostgreSQL
6379 # Redis
)
def ssh_probe(host: str, port: int = 22,
username: Optional[str] = None, password: Optional[str] = None,
keyfile: Optional[str] = None):
client = paramiko.SSHClient()
try:
client.connect(hostname=host, port=port, timeout=DEFAULT_TIMEOUT)
except socket.error:
return False
except Exception:
return True
return True
def _connect(host: str, port: int):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.settimeout(0.01) # 10ms
try:
sock.connect((host, port))
except:
return False
else:
sock.close()
return True
def ports_probe(host: str, continue_on_success: bool = False, **kwargs):
for port in PORTS:
if _connect(host, port):
print("Found opened port: %d" % port)
if not continue_on_success:
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment