Created
July 1, 2024 16:03
-
-
Save shimarin/a7b5c3620fc7f681e9ea9a2ba1f6b7db to your computer and use it in GitHub Desktop.
指定ホストの22/tcpに接続し、SSHサーバのバージョン情報を表示するスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import socket | |
import argparse | |
def get_ssh_version_string(host, port=22, timeout=5): | |
try: | |
# ソケットアドレス情報を取得 | |
addr_info = socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM) | |
for res in addr_info: | |
af, socktype, proto, canonname, sa = res | |
try: | |
# ソケットを作成して接続 | |
with socket.socket(af, socktype, proto) as sock: | |
sock.settimeout(timeout) | |
sock.connect(sa) | |
# バージョン文字列を受信 | |
version_string = sock.recv(1024).decode('utf-8') | |
# 接続を閉じる | |
sock.close() | |
return version_string | |
except Exception as e: | |
continue | |
return "Unable to connect to the host" | |
except Exception as e: | |
return f"Error: {e}" | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Get SSH server version string.") | |
parser.add_argument("host", help="Host name or IP address of the SSH server.") | |
args = parser.parse_args() | |
version = get_ssh_version_string(args.host) | |
print(f"SSHバージョン文字列: {version}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment