有点好玩的python运维脚本

1. 常用端口扫描

在计算机网络中,端口是一个通信端点,允许不同的进程或服务通过网络连接和交换数据。端口通过数值来标识,并与特定的协议相关联。未采取适当安全措施而保持端口开放,可能会使网站容易受到网络攻击。

这个自动化脚本将以网站 URL 作为输入,检查该网站的任何开放端口。无论你是在红队执行任务,还是在蓝队坚守阵地,这个脚本都能成为你的一个有用工具。

#coding:utf-8
import sys
import socket
from prettytable import PrettyTable

# Dictionary mapping common ports to vulnerabilities (Top 15)
vulnerabilities = {
    80: "HTTP (Hypertext Transfer Protocol) - Used for unencrypted web traffic",
    443: "HTTPS (HTTP Secure) - Used for encrypted web traffic",
    22: "SSH (Secure Shell) - Used for secure remote access",
    21: "FTP (File Transfer Protocol) - Used for file transfers",
    25: "SMTP (Simple Mail Transfer Protocol) - Used for email transmission",
    23: "Telnet - Used for remote terminal access",
    53: "DNS (Domain Name System) - Used for domain name resolution",
    110: "POP3 (Post Office Protocol version 3) - Used for email retrieval",
    143: "IMAP (Internet Message Access Protocol) - Used for email retrieval",
    3306: "MySQL - Used for MySQL database access",
    3389: "RDP (Remote Desktop Protocol) - Used for remote desktop connections (Windows)",
    8080: "HTTP Alternate - Commonly used as a secondary HTTP port",
    8000: "HTTP Alternate - Commonly used as a secondary HTTP port",
    8443: "HTTPS Alternate - Commonly used as a secondary HTTPS port",
    5900: "VNC (Virtual Network Computing) - Used for remote desktop access",
    # Add more ports and vulnerabilities as needed
}

def display_table(open_ports):
    table = PrettyTable(["Open Port", "Vulnerability"])
    for port in open_ports:
        vulnerability = vulnerabilities.get(port, "No known vulnerabilities associated with common services")
        table.add_row([port, vulnerability])
    print(table)

def scan_top_ports(target):
    open_ports = []
    top_ports = [21, 22, 23, 25, 53, 80, 110, 143, 443, 3306, 3389, 5900, 8000, 8080, 8443]  # Top 15 ports
    for port in top_ports:
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.settimeout(1)  # Adjust timeout as needed
            result = sock.connect_ex((target, port))
            if result == 0:
                open_ports.append(port)
            sock.close()
        except KeyboardInterrupt:
            sys.exit()
        except socket.error:
            pass
    return open_ports

def main():
    # target = sys.argv[1]
    target = 'pypi.org'
    open_ports = scan_top_ports(target)
    if not open_ports:
        print("No open ports found on the target.")
    else:
        print("Open ports and associated vulnerabilities:")
        display_table(open_ports)

if __name__ == "__main__":
    main()

让我们扫描 pypi.org

Open ports and associated vulnerabilities:
+-----------+-----------------------------------------------------------------------+
| Open Port |                             Vulnerability                             |
+-----------+-----------------------------------------------------------------------+
|     80    | HTTP (Hypertext Transfer Protocol) - Used for unencrypted web traffic |
|    443    |          HTTPS (HTTP Secure) - Used for encrypted web traffic         |
+-----------+-----------------------------------------------------------------------+

当然我们也可以把端口列表指定范围

# top_ports = [21, 22, 23, 25, 53, 80, 110, 143, 443, 3306, 3389, 5900, 8000, 8080, 8443]  # Top 15 ports
    top_ports = [i for i in range(20,30)]
Open ports and associated vulnerabilities:
+-----------+----------------------------------------------------+
| Open Port |                   Vulnerability                    |
+-----------+----------------------------------------------------+
|     22    | SSH (Secure Shell) - Used for secure remote access |
+-----------+----------------------------------------------------+

这个指定端口范围不多说了^_^

2. 文件整理

这个自动化脚本可以在几分钟内帮助你整理文件夹。你只需指定需要清理的路径,该脚本会根据文件扩展名自动将所有文件分到不同的文件夹中。

不仅如此!它还可以通过比较文件的哈希值来检测和处理重复文件。

import os
import hashlib
import shutil

def get_file_hash(file_path):
    with open(file_path, 'rb') as f:
        return hashlib.sha256(f.read()).hexdigest()

def organize_and_move_duplicates(folder_path):
    # Create a dictionary to store destination folders based on file extensions
    extension_folders = {}

    # Create the "Duplicates" folder if it doesn't exist
    duplicates_folder = os.path.join(folder_path, 'Duplicates')
    os.makedirs(duplicates_folder)

    # Create a dictionary to store file hashes
    file_hashes = {}

    # Iterate through files in the folder
    for filename in os.listdir(folder_path):
        file_path = os.path.join(folder_path, filename)
        if os.path.isfile(file_path):
            # Get the file extension
            _, extension = os.path.splitext(filename)
            extension = extension.lower()  # Convert extension to lowercase

            # Determine the destination folder
            if extension in extension_folders:
                destination_folder = extension_folders[extension]
            else:
                destination_folder = os.path.join(folder_path,
                                                  extension[1:])  # Remove the leading dot from the extension
                if not os.path.exists(destination_folder):
                    os.makedirs(destination_folder)
                extension_folders[extension] = destination_folder

            # Calculate the file hash
            file_hash = get_file_hash(file_path)

            # Check for duplicates
            if file_hash in file_hashes:
                # File is a duplicate, move it to the "Duplicates" folder
                shutil.move(file_path, os.path.join(duplicates_folder, filename))
                print("Moved duplicate file {%s} to Duplicates folder."%filename)
            else:
                # Store the file hash
                file_hashes[file_hash] = filename
                # Move the file to the destination folder
                shutil.move(file_path, destination_folder)
                print("Moved {%s} to {%s}"%(file_path, destination_folder))

if __name__ == "__main__":
    folder_path = raw_input("Enter the path to the folder to organize: ")
    organize_and_move_duplicates(folder_path)

在这里插入图片描述
不能说一点用没有,没啥太大用.

相关推荐

  1. 网络建设与python脚本应用

    2024-06-11 11:16:05       21 阅读
  2. 2.3 Python应用

    2024-06-11 11:16:05       41 阅读
  3. python与自动化相关库有哪些?

    2024-06-11 11:16:05       21 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-11 11:16:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-11 11:16:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-11 11:16:05       82 阅读
  4. Python语言-面向对象

    2024-06-11 11:16:05       91 阅读

热门阅读

  1. 什么是主数据?

    2024-06-11 11:16:05       26 阅读
  2. 深度学习中2D分割

    2024-06-11 11:16:05       29 阅读
  3. go 基础笔记

    2024-06-11 11:16:05       29 阅读
  4. OPAMC架构介绍

    2024-06-11 11:16:05       26 阅读
  5. NOR flash和NAND flash的区别

    2024-06-11 11:16:05       33 阅读
  6. 数据仓库技术及应用(Hive调优)

    2024-06-11 11:16:05       32 阅读
  7. 现代 C++的高效并发编程模式

    2024-06-11 11:16:05       32 阅读