You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
4.2 KiB
136 lines
4.2 KiB
1 month ago
|
'''
|
||
|
脚本功能:4372修改MAC地址和时间
|
||
|
运行环境:python3 + paramiko
|
||
|
此脚本在连通控制器的电脑上运行,需要提前安装python环境和paramiko库
|
||
|
'''
|
||
|
|
||
|
import os
|
||
|
import time
|
||
|
import paramiko
|
||
|
from datetime import datetime
|
||
|
import re
|
||
|
|
||
|
# 配置参数
|
||
|
RemoteHostAddr = "192.168.10.100"
|
||
|
RemoteHostPort = 22
|
||
|
User = "root"
|
||
|
Pass = "root"
|
||
|
Mac1File = "/userapp/mac1.txt"
|
||
|
Mac2File = "/userapp/mac2.txt"
|
||
|
|
||
|
# 获取当前时间并格式化
|
||
|
def format_time(current_time:datetime, flag:int):
|
||
|
if flag == 1:
|
||
|
return current_time.strftime('%Y-%m-%d %H:%M:%S')
|
||
|
elif flag == 2:
|
||
|
return current_time.strftime('%Y-%m-%d')
|
||
|
elif flag == 3:
|
||
|
return current_time.strftime('%H:%M:%S')
|
||
|
elif flag == 4:
|
||
|
return current_time.strftime('%Y年%m月%d日')
|
||
|
elif flag == 5:
|
||
|
return current_time.strftime('%Y%m%d')
|
||
|
elif flag == 6:
|
||
|
return current_time.strftime('%Y%m%d_%H%M%S')
|
||
|
elif flag == 7:
|
||
|
return current_time.strftime('%y:%m:%d:%H:%M:%S')
|
||
|
else:
|
||
|
return current_time.strftime('%Y-%m-%d %H:%M:%S')
|
||
|
|
||
|
# Ping 命令的实现
|
||
|
def ping(host):
|
||
|
"""Ping 地址,返回 True 或 False"""
|
||
|
if os.name == 'nt':
|
||
|
response = os.system(f"ping -n 1 -w 1000 {host} > nul 2>&1")
|
||
|
return response == 0
|
||
|
else:
|
||
|
response = os.system(f"ping -c 1 -W 1 {host} > /dev/null 2>&1")
|
||
|
return response == 0
|
||
|
|
||
|
# 检查 IP 地址格式是否正确
|
||
|
def is_ipaddr(ip):
|
||
|
pattern = r"^([0-9]|[1-9][0-9]|1\d\d|2[0-4]\d|25[0-5])(\.([0-9]|[1-9][0-9]|1\d\d|2[0-4]\d|25[0-5])){3}$"
|
||
|
if re.match(pattern, ip):
|
||
|
return True
|
||
|
else:
|
||
|
print(f"IP 地址格式不正确: {ip}")
|
||
|
return False
|
||
|
|
||
|
# 使用 Paramiko 进行 SSH 登录并执行命令
|
||
|
def ssh_execute(host, port, username, password, commands:list):
|
||
|
"""通过 SSH 执行命令"""
|
||
|
try:
|
||
|
ssh_client = paramiko.SSHClient()
|
||
|
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 自动接受未知主机密钥
|
||
|
ssh_client.connect(host, port=port, username=username, password=password)
|
||
|
|
||
|
for command in commands:
|
||
|
print(f"正在执行命令: {command}")
|
||
|
stdin, stdout, stderr = ssh_client.exec_command(command)
|
||
|
print(stdout.read().decode()) # 打印命令输出
|
||
|
if stderr.read():
|
||
|
print(stderr.read().decode()) # 打印命令错误输出
|
||
|
|
||
|
ssh_client.close()
|
||
|
|
||
|
except Exception as e:
|
||
|
print(f"SSH 连接失败: {e}")
|
||
|
|
||
|
# 设置文件的 MAC 地址
|
||
|
def write_mac_to_file(mac1, mac2):
|
||
|
"""将 MAC 地址写入文件"""
|
||
|
with open(Mac1File, 'w') as f1:
|
||
|
f1.write(mac1)
|
||
|
with open(Mac2File, 'w') as f2:
|
||
|
f2.write(mac2)
|
||
|
|
||
|
def modify_mac_address(mac):
|
||
|
mac_bytes = [int(byte, 16) for byte in mac.split(':')]
|
||
|
|
||
|
# 判断第一个字节是否为奇数,若是则 +5
|
||
|
if mac_bytes[0] % 2 == 1:
|
||
|
mac_bytes[0] += 5
|
||
|
|
||
|
# 将字节列表重新转换为 MAC 地址字符串
|
||
|
new_mac = ':'.join(format(byte, '02x') for byte in mac_bytes)
|
||
|
return new_mac
|
||
|
|
||
|
# 主程序
|
||
|
def main():
|
||
|
# 获取当前时间
|
||
|
MAC1 = format_time(datetime.now(), 7)
|
||
|
time.sleep(1)
|
||
|
MAC2 = format_time(datetime.now(), 7)
|
||
|
time.sleep(1)
|
||
|
STRTIME = format_time(datetime.now(), 1)
|
||
|
|
||
|
# 打印信息
|
||
|
print(f"MAC1: {MAC1}")
|
||
|
print(f"MAC2: {MAC2}")
|
||
|
print(f"STRTIME: {STRTIME}")
|
||
|
|
||
|
mac1 = modify_mac_address(MAC1)
|
||
|
mac2 = modify_mac_address(MAC2)
|
||
|
print(f"修改后的 MAC1: {mac1}")
|
||
|
print(f"修改后的 MAC2: {mac2}")
|
||
|
|
||
|
# 循环 ping 测试
|
||
|
for i in range(1, 11):
|
||
|
if not ping(RemoteHostAddr):
|
||
|
print(f"{i}/10 Ping 设备失败,等待重新尝试...")
|
||
|
time.sleep(5)
|
||
|
else:
|
||
|
print("Ping 设备成功,开始 SSH 登录...")
|
||
|
commands = [
|
||
|
f"date -s \"{STRTIME}\"",
|
||
|
"hwclock --systohc",
|
||
|
f"echo {mac1} > {Mac1File}",
|
||
|
f"echo {mac2} > {Mac2File}",
|
||
|
"reboot"
|
||
|
]
|
||
|
ssh_execute(RemoteHostAddr, RemoteHostPort, User, Pass, commands)
|
||
|
break
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|