打开神奇的BAT盒子(1)

2023-12-15 21:50:15

1. 基础远程控制

1.1 本地IP地址


@echo off
setlocal

for /f "tokens=2 delims=:" %%i in ('ipconfig ^| find "IPv4 地址"') do set IP=%%i
echo 本地IP地址: %IP%

endlocal

BAT程序清单1.1


如果你是BAT初学者,先来学习一下BAT程序的基础。

1. @echo off: 这个命令用于关闭脚本的输出到命令行界面。它可以使脚本看起来更干净,只显示想要的输出结果。

2. setlocal: 这个命令启用了局部环境变量。在BAT脚本中,环境变量用于存储和访问各种值和配置信息。

3. endlocal: 这个命令用于结束局部环境变量的作用范围,并恢复到之前的环境变量状态。

好。我们一起看一下,在BAT程序清单1.1种,我们可以看到第4行代码离谱地长。先来给大家科普一下那个令人疑惑的东西。

ipconfig ^|: 这个命令用于获取电脑的IP地址。其中^|表示回车键。

现在,我们来运行一下。哦对了,还没有保存呢。

1. 新建一个文本文档。

2. 将扩展名改为.bat。

3. 双击BAT文件,直到弹出黑窗口。

1.2 关机 & 重启


@echo off

set /p IP=请输入远程计算机的IP地址: 
set /p User=请输入用户名: 
set /p Password=请输入密码: 

shutdown -s -m \\%IP% -u %User% -p %Password% -t 0
@echo off

set /p IP=请输入远程计算机的IP地址: 
set /p User=请输入用户名: 
set /p Password=请输入密码: 

shutdown -r -m \\%IP% -u %User% -p %Password% -t 0

BAT程序清单1.2


shutdownの知识:

-s ? ? ? ? ? 选择开机选项

-r ? ? ? ? ? ?选择重启选项

-m ? ? ? ? ?选择目标计算机

-t n? ? ? ? ?n秒后关机?

1.3 发消息


@echo off

set /p computer=请输入远程计算机的名称或IP地址: 
set /p message=请输入要发送的消息: 

msg %computer% %message%

BAT程序清单1.3


msg功能:

有两个参数。

%computer% 表示computer变量,是要发送的电脑对象。

%message% 表示message变量,是要发送的消息。

2. 附录


from tkinter import *
import os

def remote_shutdown():
    ip = ip_entry.get()
    user = user_entry.get()
    password = password_entry.get()
    command = f'shutdown -s -m \\\\{ip} -u {user} -p {password} -t 0'
    os.system(command)
    status_label.config(text="远程关机命令已发送")

def remote_restart():
    ip = ip_entry.get()
    user = user_entry.get()
    password = password_entry.get()
    command = f'shutdown -r -m \\\\{ip} -u {user} -p {password} -t 0'
    os.system(command)
    status_label.config(text="远程重启命令已发送")

def remote_message():
    computer = computer_entry.get()
    message = message_entry.get()
    command = f'msg {computer} {message}'
    os.system(command)
    status_label.config(text="远程消息已发送")

root = Tk()
root.title("远程控制软件")

# IP地址输入框
ip_label = Label(root, text="远程计算机的IP地址:")
ip_label.pack()
ip_entry = Entry(root)
ip_entry.pack()

# 用户名输入框
user_label = Label(root, text="用户名:")
user_label.pack()
user_entry = Entry(root)
user_entry.pack()

# 密码输入框
password_label = Label(root, text="密码:")
password_label.pack()
password_entry = Entry(root, show="?")
password_entry.pack()

# 远程关机按钮
shutdown_button = Button(root, text="远程关机", command=remote_shutdown)
shutdown_button.pack()

# 远程重启按钮
restart_button = Button(root, text="远程重启", command=remote_restart)
restart_button.pack()

# 远程消息发送部分
line_break = Label(root, text="\n")
line_break.pack()

computer_label = Label(root, text="远程计算机的名称或IP地址:")
computer_label.pack()
computer_entry = Entry(root)
computer_entry.pack()

message_label = Label(root, text="要发送的消息:")
message_label.pack()
message_entry = Entry(root)
message_entry.pack()

send_button = Button(root, text="发送消息", command=remote_message)
send_button.pack()

# 状态标签
status_label = Label(root, text="")
status_label.pack()

root.mainloop()

Python Tkinter 远程控制的综合实现


文章来源:https://blog.csdn.net/joe_g12345/article/details/135023594
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。