python运维脚本
- 1. 常用端口扫描
- 2. 文件整理
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)
不能说一点用没有,没啥太大用.