smb共享简单连接并创建快捷方式 程序制作
1: 安装 Python 和必要的库
安装 Python:如果你还没有安装 Python,可以从 Python 官网 下载并安装。
安装 Tkinter:Tkinter 通常随 Python 一起安装。如果没有,可以通过以下命令安装:
pip install tk安装 PyInstaller:使用以下命令安装 PyInstaller:
pip install pyinstaller
步骤 2: 创建 Python 脚本
将以下代码保存为 smb_manager.py:
import tkinter as tk
from tkinter import messagebox
import subprocess
import os
import re
import webbrowser
# 示例文本定义
EXAMPLE_SMB_SHARE = "\\\\192.168.1.1(示例)"
EXAMPLE_USERNAME = "张三(示例)"
EXAMPLE_PASSWORD = "123456"
EXAMPLE_SHORTCUT_NAME = "我的共享盘(示例)"
# 控制是否允许提交示例信息
ALLOW_SUBMIT_EXAMPLES = False
# 样式参数定义
STYLE_CONFIG = {
"LABEL_PADX": 5,
"LABEL_PADY": 5,
"ENTRY_WIDTH": 20,
"BUTTON_WIDTH": 15,
"BG_COLOR": "#f0f0f0",
"BUTTON_COLOR_1": "#4CAF50",
"BUTTON_COLOR_2": "#2196F3",
"CREDITS_COLOR": "blue",
}
# 函数:验证输入的 IP 地址是否有效
def is_valid_ip(ip):
pattern = re.compile(r"^(?:(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")
return pattern.match(ip) is not None
# 函数:检查输入是否为示例文本
def is_placeholder(entry, placeholder):
return entry.get() == placeholder
# 函数:添加 SMB 共享的凭据
def add_credentials():
if (not ALLOW_SUBMIT_EXAMPLES and
(is_placeholder(smb_share_entry, EXAMPLE_SMB_SHARE) or
is_placeholder(username_entry, EXAMPLE_USERNAME) or
is_placeholder(password_entry, EXAMPLE_PASSWORD))):
messagebox.showerror("错误", "请修改示例信息后再提交!")
return
smb_share = smb_share_entry.get()
username = username_entry.get()
password = password_entry.get()
if not smb_share or not username or not password:
messagebox.showerror("错误", "所有字段都是必填的!")
return
# 只提取 IP 地址部分
if smb_share.startswith("\\\\"):
smb_share = smb_share[2:] # 去掉前面的两个反斜杠
ip_address = smb_share.split("\\")[0] # 提取 IP 地址
else:
ip_address = smb_share # 如果不以 \\ 开头,直接使用输入的路径
if not is_valid_ip(ip_address):
messagebox.showerror("错误", "共享路径必须是有效的 IP 地址!")
return
cmd = f'cmdkey /add:"{ip_address}" /user:"{username}" /pass:"{password}"'
try:
subprocess.run(cmd, check=True, shell=True)
messagebox.showinfo("成功", "凭据已成功添加!")
except subprocess.CalledProcessError as e:
messagebox.showerror("错误", f"添加凭据失败: {str(e)}")
except Exception as e:
messagebox.showerror("错误", f"发生错误: {str(e)}")
# 函数:创建快捷方式
def create_shortcut():
if (not ALLOW_SUBMIT_EXAMPLES and
(is_placeholder(smb_share_entry, EXAMPLE_SMB_SHARE) or
is_placeholder(shortcut_name_entry, EXAMPLE_SHORTCUT_NAME))):
messagebox.showerror("错误", "请修改示例信息后再创建快捷方式!")
return
smb_share = smb_share_entry.get()
shortcut_name = shortcut_name_entry.get()
if not smb_share or not shortcut_name:
messagebox.showerror("错误", "共享路径和快捷方式名称是必填的!")
return
if not smb_share.startswith("\\\\"):
smb_share = "\\\\" + smb_share
shortcut_path = f"{os.path.expanduser('~')}/Desktop/{shortcut_name}.lnk"
cmd = f'powershell.exe -Command "$s=(New-Object -COM WScript.Shell).CreateShortcut(\'{shortcut_path}\');$s.TargetPath=\'{smb_share}\';$s.Save()"'
try:
subprocess.run(cmd, check=True, shell=True)
messagebox.showinfo("成功", "快捷方式已创建!")
except subprocess.CalledProcessError as e:
messagebox.showerror("错误", f"创建快捷方式失败: {str(e)}")
except Exception as e:
messagebox.showerror("错误", f"发生错误: {str(e)}")
# 新增函数:打开链接
def open_link(event):
webbrowser.open("https://www.helloimg.com/i/2025/01/03/677799239cd82.png")
# 清空输入框示例文本
def clear_placeholder(event, entry, placeholder):
if entry.get() == placeholder:
entry.delete(0, tk.END)
entry.config(fg='black')
# 设置示例文本
def set_placeholder(event, entry, placeholder):
if entry.get() == "":
entry.insert(0, placeholder)
entry.config(fg='gray')
# 创建主窗口
root = tk.Tk()
root.title("SMB 连接工具")
root.geometry("300x220")
root.resizable(False, False)
root.configure(bg=STYLE_CONFIG["BG_COLOR"])
# 创建外框架
frame = tk.Frame(root, bg=STYLE_CONFIG["BG_COLOR"], padx=10, pady=10)
frame.pack(fill=tk.BOTH, expand=True)
# 创建输入框
def create_input(label_text, example_text, row):
tk.Label(frame, text=label_text, bg=STYLE_CONFIG["BG_COLOR"]).grid(row=row, column=0, padx=STYLE_CONFIG["LABEL_PADX"], pady=STYLE_CONFIG["LABEL_PADY"], sticky="w")
entry = tk.Entry(frame, width=STYLE_CONFIG["ENTRY_WIDTH"], fg='gray')
entry.insert(0, example_text)
entry.bind("<FocusIn>", lambda e: clear_placeholder(e, entry, example_text))
entry.bind("<FocusOut>", lambda e: set_placeholder(e, entry, example_text))
entry.grid(row=row, column=1, padx=STYLE_CONFIG["LABEL_PADX"], pady=STYLE_CONFIG["LABEL_PADY"])
return entry
smb_share_entry = create_input("共享路径:", EXAMPLE_SMB_SHARE, 0)
username_entry = create_input("用户名:", EXAMPLE_USERNAME, 1)
password_entry = create_input("密码:", EXAMPLE_PASSWORD, 2)
password_entry.config(show="*")
shortcut_name_entry = create_input("快捷方式名称:", EXAMPLE_SHORTCUT_NAME, 3)
# 创建按钮
add_button = tk.Button(frame, text="提交账号密码", command=add_credentials, bg=STYLE_CONFIG["BUTTON_COLOR_1"], fg="white", width=STYLE_CONFIG["BUTTON_WIDTH"])
add_button.grid(row=4, column=0, padx=STYLE_CONFIG["LABEL_PADX"], pady=5)
shortcut_button = tk.Button(frame, text="创建桌面快捷方式", command=create_shortcut, bg=STYLE_CONFIG["BUTTON_COLOR_2"], fg="white", width=STYLE_CONFIG["BUTTON_WIDTH"])
shortcut_button.grid(row=4, column=1, padx=STYLE_CONFIG["LABEL_PADX"], pady=5)
# 添加 "by: zhangt" 标签
credits_label = tk.Label(frame, text="by:zhangt", fg=STYLE_CONFIG["CREDITS_COLOR"], cursor="hand2", bg=STYLE_CONFIG["BG_COLOR"])
credits_label.grid(row=5, column=0, columnspan=2, pady=5)
credits_label.bind("<Button-1>", open_link)
# 启动主循环
root.mainloop()
查看全部
步骤 3: 使用 PyInstaller 打包成 EXE
打开命令提示符:在 Windows 中,按下
Win + R,输入cmd,然后按回车。导航到脚本所在目录:使用
cd命令导航到保存smb_manager.py的目录。例如:cd C:\path\to\your\script运行 PyInstaller:
pyinstaller --onefile --windowed smb_manager.py--onefile表示生成一个单独的 EXE 文件。--windowed表示不显示命令行窗口。
查找生成的 EXE 文件:打包完成后,生成的 EXE 文件将位于
dist文件夹中,文件名为smb_manager.exe。
步骤 4: 运行 EXE 文件
双击 smb_manager.exe 文件以运行程序。你将看到一个图形用户界面,允许你输入共享路径、用户名、密码,并创建快捷方式。
效果:添加账号密码到windows凭据,并创建共享快捷方式到桌面。

- 感谢你赐予我前进的力量
赞赏者名单
因为你们的支持让我意识到写文章的价值🙏
本文是原创文章,采用CC BY-NC-ND 4.0协议,完整转载请注明来自 halo.taofile.cn。
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果

