有点好玩的python运维脚本

2024-06-10 16:28

本文主要是介绍有点好玩的python运维脚本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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 portsfor port in top_ports:try:sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)sock.settimeout(1)  # Adjust timeout as neededresult = sock.connect_ex((target, port))if result == 0:open_ports.append(port)sock.close()except KeyboardInterrupt:sys.exit()except socket.error:passreturn open_portsdef 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 portstop_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 shutildef 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 extensionsextension_folders = {}# Create the "Duplicates" folder if it doesn't existduplicates_folder = os.path.join(folder_path, 'Duplicates')os.makedirs(duplicates_folder)# Create a dictionary to store file hashesfile_hashes = {}# Iterate through files in the folderfor 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 folderif 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 extensionif not os.path.exists(destination_folder):os.makedirs(destination_folder)extension_folders[extension] = destination_folder# Calculate the file hashfile_hash = get_file_hash(file_path)# Check for duplicatesif file_hash in file_hashes:# File is a duplicate, move it to the "Duplicates" foldershutil.move(file_path, os.path.join(duplicates_folder, filename))print("Moved duplicate file {%s} to Duplicates folder."%filename)else:# Store the file hashfile_hashes[file_hash] = filename# Move the file to the destination foldershutil.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)

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

这篇关于有点好玩的python运维脚本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1048668

相关文章

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

Python正则表达式匹配和替换的操作指南

《Python正则表达式匹配和替换的操作指南》正则表达式是处理文本的强大工具,Python通过re模块提供了完整的正则表达式功能,本文将通过代码示例详细介绍Python中的正则匹配和替换操作,需要的朋... 目录基础语法导入re模块基本元字符常用匹配方法1. re.match() - 从字符串开头匹配2.

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

通过Docker容器部署Python环境的全流程

《通过Docker容器部署Python环境的全流程》在现代化开发流程中,Docker因其轻量化、环境隔离和跨平台一致性的特性,已成为部署Python应用的标准工具,本文将详细演示如何通过Docker容... 目录引言一、docker与python的协同优势二、核心步骤详解三、进阶配置技巧四、生产环境最佳实践

Python一次性将指定版本所有包上传PyPI镜像解决方案

《Python一次性将指定版本所有包上传PyPI镜像解决方案》本文主要介绍了一个安全、完整、可离线部署的解决方案,用于一次性准备指定Python版本的所有包,然后导出到内网环境,感兴趣的小伙伴可以跟随... 目录为什么需要这个方案完整解决方案1. 项目目录结构2. 创建智能下载脚本3. 创建包清单生成脚本4

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、