汕头市公司网站建设多少钱,呼和浩特企业网站排名优化,如何进行网站关键词优化,网址免费全自动推广平台pythonselenium实现自动联网认证#xff0c;并实现断网重连
echo off python “E:\autoD\auto_login.py”
要使自动登录脚本在系统重启后自动运行#xff0c;你可以使用Windows的任务计划程序来设置。以下是详细的步骤#xff1a;
1. 保存脚本
首先#xff0c;将你的Py…pythonselenium实现自动联网认证并实现断网重连
echo off python “E:\autoD\auto_login.py”
要使自动登录脚本在系统重启后自动运行你可以使用Windows的任务计划程序来设置。以下是详细的步骤
1. 保存脚本
首先将你的Python脚本保存为一个文件例如 auto_login.py。
2. 创建批处理文件
为了方便任务计划程序调用我们可以创建一个批处理文件.bat来运行Python脚本。假设你已经安装了Python并且Python的路径已经添加到系统的环境变量中。
打开记事本或任何文本编辑器。输入以下内容echo off
python C:\path\to\your\script\auto_login.py将上述内容保存为 run_auto_login.bat确保路径指向你的Python脚本的实际位置。
3. 设置任务计划程序 打开任务计划程序 按 Win R 打开运行对话框输入 taskschd.msc 并按回车打开任务计划程序。 创建基本任务 在右侧操作栏中点击“创建基本任务…”。输入任务名称例如“自动登录认证”然后点击“下一步”。选择触发器为“当计算机启动时”点击“下一步”。选择操作为“启动程序”点击“下一步”。 配置启动程序 浏览并选择刚刚创建的 run_auto_login.bat 文件。点击“下一步”然后点击“完成”。 高级设置可选 如果需要进一步配置任务可以在任务列表中找到你创建的任务右键点击它选择“属性”。在“常规”选项卡中可以设置任务的描述、安全选项等。在“触发器”选项卡中可以设置更具体的触发条件。在“操作”选项卡中可以查看和修改启动的程序。在“条件”和“设置”选项卡中可以设置更多的任务执行条件和行为。
4. 测试任务 手动测试 你可以在任务计划程序中手动运行这个任务以确保它能够正确执行。右键点击任务选择“运行”。 重启测试 重启计算机检查任务是否在启动时自动运行。
5. 验证脚本
确保Python脚本和WebDriver路径正确。确保浏览器驱动如ChromeDriver已下载并放置在正确的路径。确保Python和WebDriver版本兼容。
6. 其他注意事项
权限问题确保任务计划程序有足够的权限运行脚本。防火墙和杀毒软件确保防火墙和杀毒软件不会阻止脚本的运行。日志记录如果需要调试可以在Python脚本中添加日志记录以便追踪脚本的执行情况。
通过以上步骤你应该能够成功设置一个在系统重启后自动运行的自动登录脚本。如果有任何问题请随时告诉我。
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time
设置WebDriver路径
webdriver_path r’C:\Program Files\Google\Chrome\chromedriver-win64\chromedriver.exe’ # #请下载这个替换为你的WebDriver实际路径 service Service(webdriver_path)
创建一个新的浏览器实例
driver webdriver.Chrome(serviceservice)
try: # 打开认证页面 driver.get(“http://172.1sType1”)
# 等待页面加载
wait WebDriverWait(driver, 10)# 填写用户名
username_field wait.until(EC.presence_of_element_located((By.ID, username)))
username_field.send_keys(dfdfg)# 填写密码
password_field wait.until(EC.presence_of_element_located((By.ID, password)))
password_field.send_keys(Z)# 选择“已阅读并同意用户须知”复选框
agree_checkbox wait.until(EC.element_to_be_clickable((By.XPATH, //img[idagreeCheck])))
agree_checkbox.click()# 提交表单
submit_button wait.until(EC.element_to_be_clickable((By.ID, loginBtn)))
submit_button.click()# 如果需要保持页面打开一段时间可以增加等待时间
time.sleep(5) # 可以根据需要调整等待时间finally: # 关闭浏览器 driver.quit()
###查看网页源代码 已阅读并同意用户须知 忘记密码 注册 根据上面修改下面的参数
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time
设置WebDriver路径
webdriver_path r’E:\Program Files\Tencent\QQBrowser\QQBrowser.exe’ # 请替换为你的WebDriver实际路径 service Service(webdriver_path)
创建一个新的浏览器实例
driver webdriver.Chrome(serviceservice)
try: # 打开认证页面 driver.get(“http://174ype1”)
# 等待页面加载
wait WebDriverWait(driver, 10)# 填写用户名
username_field wait.until(EC.presence_of_element_located((By.NAME, username))) # 替换为实际的用户名输入框name属性
username_field.send_keys(44g)# 填写密码
password_field wait.until(EC.presence_of_element_located((By.NAME, password))) # 替换为实际的密码输入框name属性
password_field.send_keys(45)# 选择“已阅读并同意用户须知”复选框
agree_checkbox wait.until(EC.element_to_be_clickable((By.XPATH, //input[typecheckbox]))) # 根据实际情况调整XPath
agree_checkbox.click()# 提交表单
submit_button wait.until(EC.element_to_be_clickable((By.XPATH, //button[typesubmit]))) # 根据实际情况调整XPath
submit_button.click()# 如果需要保持页面打开一段时间可以增加等待时间
time.sleep(5) # 可以根据需要调整等待时间finally: # 关闭浏览器 driver.quit()
要实现一个批处理文件.bat该文件能够自动检查网络状态并在网络出现问题时重新运行 auto_login.py你可以使用以下步骤来创建这个批处理文件。这个批处理文件将定期 ping 百度并在检测到网络问题时重新运行 auto_login.py。同时它会将日志记录到一个文本文件中。
批处理文件示例
创建一个新的批处理文件 打开记事本或任何文本编辑器。将以下内容复制并粘贴到编辑器中。
echo off
setlocal:: 设置日志文件路径
set log_fileE:\autoD\network_log.txt:: 初始登录
echo [%date% %time%]: Initial login %log_file%
python E:\autoD\auto_login.py %log_file% 21:: 设置检查间隔时间秒
set nexttime60:loop
:: Ping 百度四次
for /f tokens4 %%i in (ping -n 4 www.baidu.com ^| find Reply from) do set icmp_cnt%%i:: 获取成功的 ICMP 包数量
set /a success_count0
for /f tokens1,2 delims %%i in (ping -n 4 www.baidu.com ^| find Reply from) do set /a success_count1:: 记录当前时间和 ping 结果
echo [%date% %time%]: ping receives: %success_count% / 4 %log_file%:: 如果 ping 失败ICMP 包少于 4 个重新登录
if %success_count% LSS 4 (echo [%date% %time%]: Network issue detected, re-login %log_file%python E:\autoD\auto_login.py %log_file% 21
):: 等待指定的时间间隔
timeout /t %nexttime% nul
goto loop保存文件 将文件保存为 network_checker.bat确保扩展名为 .bat。
解释
日志文件network_log.txt 用于记录每次 ping 的结果和重新登录的时间。初始登录脚本启动时首先执行一次 auto_login.py并将输出重定向到日志文件。Ping 检查每隔 60 秒 ping 百度四次并统计成功的 ICMP 包数量。网络问题检测如果成功的 ICMP 包数量少于 4 个则认为网络有问题并重新运行 auto_login.py。日志记录每次 ping 的结果和重新登录的操作都会记录到 network_log.txt 文件中。循环使用 :loop 标签和 goto loop 实现无限循环。
使用方法
将上述批处理文件保存为 network_checker.bat并将其放置在与 auto_login.py 同一级目录下。在命令提示符或 PowerShell 中运行该批处理文件E:\autoD\network_checker.bat注意事项
确保 auto_login.py 脚本在同一目录下并且可以正常运行。如果你希望脚本在后台运行可以使用 nohup 或者将脚本打包成一个 Windows 服务。如果需要更复杂的日志管理如日志轮转可以考虑使用专门的日志管理工具。
通过这种方式你可以实现在 Windows 上定期检查网络状态并在网络出现问题时自动重新登录的功能。
sshpass 是一个非常方便的工具可以用来非交互式地提供 SSH 密码。你已经提到使用 sshpass.exe 来实现这个功能。以下是一个批处理脚本示例它使用 sshpass 来自动输入密码并定期检查 SSH 连接状态如果连接断开则重新连接。
批处理脚本
echo off
setlocal enabledelayedexpansion:: 设置变量
set sshCommandssh -g -N -R 1dfd.2:3389:localhost:3389 -o ServerAliveInterval60 root4f.12fdf8.178
set password666666
set sshpassPathC:\path\to\sshpass.exe :: 替换为你的 windows 版本的sshpass.exe 的实际路径:: 延迟一段时间以确保系统完全启动
ping localhost -n 31 nul:initial_connect
:: 初始连接
start %sshpassPath% -p %password% %sshCommand%
ping localhost -n 2 nul:check_and_reconnect
:: 检查 SSH 连接是否存活
tasklist | findstr /C:ssh.exe nul
if errorlevel 1 (:: 如果连接断开重新连接goto initial_connect
):: 每隔 60 秒检查一次
ping localhost -n 61 nul
goto check_and_reconnect解释 设置变量 sshCommand包含 SSH 命令。passwordSSH 密码。sshpassPathsshpass.exe 的路径请替换为你实际的 sshpass.exe 路径。 延迟一段时间以确保系统完全启动 使用 ping localhost -n 31 nul 命令等待 30 秒-n 31 表示发送 31 个 ping 请求每个请求间隔 1 秒。 初始连接 使用 start 命令打开一个新的命令提示符窗口并运行 sshpass 来处理 SSH 连接和密码输入。sshpass -p %password% %sshCommand% 会自动输入密码并执行 SSH 命令。使用 ping localhost -n 2 nul 命令等待 1.5 秒-n 2 表示发送 2 个 ping 请求每个请求间隔 1 秒。 检查 SSH 连接是否存活 使用 tasklist 和 findstr 检查是否有 ssh.exe 进程在运行。如果没有找到 ssh.exe 进程errorlevel 1则跳转到 initial_connect 标签重新连接。 每隔 60 秒检查一次 使用 ping localhost -n 61 nul 命令等待 60 秒-n 61 表示发送 61 个 ping 请求每个请求间隔 1 秒然后再次检查连接状态。
注意事项
安全性将密码硬编码在脚本中是非常不安全的做法。建议使用 SSH 密钥对认证来提高安全性。依赖性此脚本依赖于 sshpass.exe请确保 sshpass.exe 在你的系统上可用并且路径配置正确。权限可能需要管理员权限来运行某些命令特别是涉及到进程管理的部分。
使用方法 保存脚本 将上述批处理代码保存为一个 .bat 文件例如 auto_ssh_reverse_tunnel.bat。 运行批处理文件 双击 auto_ssh_reverse_tunnel.bat 文件或者在命令提示符中运行它auto_ssh_reverse_tunnel.bat通过这个批处理脚本你应该能够更可靠地实现 SSH 自动连接和密码输入。如果仍然遇到问题请确保 sshpass.exe 已正确安装并且路径配置正确。
整合认证和ssh
你可以将两个任务整合到一个批处理文件中。首先运行 Python 脚本 auto_login.py等待其执行完毕然后再启动 SSH 连接并定期检查连接状态。以下是整合后的批处理脚本
echo off
setlocal enabledelayedexpansion:: 设置变量
set sshCommandssh -g -N -R 1d.2:3389:localhost:3389 -o ServerAliveInterval60 root47d8
set passwordf666664
set sshpassPathE:\autoD\sshpass.exe :: 替换为你的 sshpass.exe 的实际路径
set pythonScriptE:\autoD\auto_login.py :: 替换为你的 Python 脚本的实际路径:: 运行 Python 脚本并等待其执行完毕
echo Running Python script...
python %pythonScript%
if %errorlevel% neq 0 (echo Python script failed with error code %errorlevel%exit /b %errorlevel%
):: 延迟一段时间以确保系统完全启动
ping localhost -n 31 nul:initial_connect
:: 初始连接
start %sshpassPath% -p %password% %sshCommand%
ping localhost -n 2 nul:check_and_reconnect
:: 检查 SSH 连接是否存活
tasklist | findstr /C:ssh.exe nul
if errorlevel 1 (:: 如果连接断开重新连接goto initial_connect
):: 每隔 60 秒检查一次
ping localhost -n 61 nul
goto check_and_reconnect解释 设置变量 sshCommand包含 SSH 命令。passwordSSH 密码。sshpassPathsshpass.exe 的路径请替换为你实际的 sshpass.exe 路径。pythonScriptPython 脚本的路径请替换为你实际的 Python 脚本路径。 运行 Python 脚本并等待其执行完毕 使用 python 命令运行 Python 脚本。检查 python 命令的退出代码 (errorlevel)如果非零则输出错误信息并退出批处理脚本。 延迟一段时间以确保系统完全启动 使用 ping localhost -n 31 nul 命令等待 30 秒-n 31 表示发送 31 个 ping 请求每个请求间隔 1 秒。 初始连接 使用 start 命令打开一个新的命令提示符窗口并运行 sshpass 来处理 SSH 连接和密码输入。sshpass -p %password% %sshCommand% 会自动输入密码并执行 SSH 命令。使用 ping localhost -n 2 nul 命令等待 1.5 秒-n 2 表示发送 2 个 ping 请求每个请求间隔 1 秒。 检查 SSH 连接是否存活 使用 tasklist 和 findstr 检查是否有 ssh.exe 进程在运行。如果没有找到 ssh.exe 进程errorlevel 1则跳转到 initial_connect 标签重新连接。 每隔 60 秒检查一次 使用 ping localhost -n 61 nul 命令等待 60 秒-n 61 表示发送 61 个 ping 请求每个请求间隔 1 秒然后再次检查连接状态。
注意事项
安全性将密码硬编码在脚本中是非常不安全的做法。建议使用 SSH 密钥对认证来提高安全性。依赖性此脚本依赖于 sshpass.exe 和 python请确保这些工具在你的系统上可用并且路径配置正确。权限可能需要管理员权限来运行某些命令特别是涉及到进程管理的部分。
使用方法 保存脚本 将上述批处理代码保存为一个 .bat 文件例如 auto_ssh_reverse_tunnel.bat。 运行批处理文件 双击 auto_ssh_reverse_tunnel.bat 文件或者在命令提示符中运行它auto_ssh_reverse_tunnel.bat通过这个整合后的批处理脚本你应该能够先运行 Python 脚本然后自动建立并维护 SSH 连接。如果仍然遇到问题请确保所有路径和工具都已正确配置。
https://github.com/xhcoding/sshpass-win32/releases/tag/v1.0.1
#########################最后大成的版本auto_login_ssh_reverse_tunnel.bat#####################
为了将两个功能整合到一个批处理文件中我们需要确保以下几点
先运行 Python 脚本 auto_login.py 并记录日志。定期检查网络连接状态如果检测到网络问题则重新运行 Python 脚本。建立并维护 SSH 反向隧道连接。
以下是整合后的批处理脚本
echo off
setlocal enabledelayedexpansion:: 设置变量
set sshCommandssh -g -N -R 1d3389:localhost:3389 -o ServerAliveInterval60 root4d8
set passwordd
set sshpassPathE:\autoD\sshpass.exe :: 替换为你的 sshpass.exe 的实际路径
set pythonScriptE:\autoD\auto_login.py :: 替换为你的 Python 脚本的实际路径
set log_fileE:\autoD\network_log.txt:: 设置检查间隔时间秒
set nexttime60:: 初始登录
echo [%date% %time%]: Initial login %log_file%
python %pythonScript% %log_file% 21
if %errorlevel% neq 0 (echo [%date% %time%]: Python script failed with error code %errorlevel% %log_file%exit /b %errorlevel%
):: 延迟一段时间以确保系统完全启动
ping localhost -n 31 nul:initial_connect
:: 初始连接
start %sshpassPath% -p %password% %sshCommand%
ping localhost -n 2 nul:check_and_reconnect
:: 检查 SSH 连接是否存活
tasklist | findstr /C:ssh.exe nul
if errorlevel 1 (:: 如果连接断开重新连接goto initial_connect
):: Ping 百度四次
for /f tokens4 %%i in (ping -n 4 www.baidu.com ^| find Reply from) do set icmp_cnt%%i:: 获取成功的 ICMP 包数量
set /a success_count0
for /f tokens1,2 delims %%i in (ping -n 4 www.baidu.com ^| find Reply from) do set /a success_count1:: 记录当前时间和 ping 结果
echo [%date% %time%]: ping receives: %success_count% / 4 %log_file%:: 如果 ping 失败ICMP 包少于 4 个重新登录
if %success_count% LSS 4 (echo [%date% %time%]: Network issue detected, re-login %log_file%python %pythonScript% %log_file% 21if %errorlevel% neq 0 (echo [%date% %time%]: Python script failed with error code %errorlevel% %log_file%)
):: 等待指定的时间间隔
timeout /t %nexttime% nul
goto check_and_reconnect解释 设置变量 sshCommand包含 SSH 命令。passwordSSH 密码。sshpassPathsshpass.exe 的路径请替换为你实际的 sshpass.exe 路径。pythonScriptPython 脚本的路径请替换为你实际的 Python 脚本路径。log_file日志文件的路径。nexttime检查间隔时间秒。 初始登录 记录日志并运行 Python 脚本 auto_login.py。检查 python 命令的退出代码 (errorlevel)如果非零则输出错误信息并退出批处理脚本。 延迟一段时间以确保系统完全启动 使用 ping localhost -n 31 nul 命令等待 30 秒-n 31 表示发送 31 个 ping 请求每个请求间隔 1 秒。 初始连接 使用 start 命令打开一个新的命令提示符窗口并运行 sshpass 来处理 SSH 连接和密码输入。sshpass -p %password% %sshCommand% 会自动输入密码并执行 SSH 命令。使用 ping localhost -n 2 nul 命令等待 1.5 秒-n 2 表示发送 2 个 ping 请求每个请求间隔 1 秒。 检查 SSH 连接是否存活 使用 tasklist 和 findstr 检查是否有 ssh.exe 进程在运行。如果没有找到 ssh.exe 进程errorlevel 1则跳转到 initial_connect 标签重新连接。 Ping 百度四次 使用 ping 命令对 www.baidu.com 发送 4 个 ICMP 请求并计算成功响应的数量。记录当前时间和 ping 结果到日志文件。如果成功的 ICMP 包少于 4 个则重新运行 Python 脚本 auto_login.py 并记录日志。 等待指定的时间间隔 使用 timeout /t %nexttime% nul 命令等待 60 秒然后再次检查连接状态。
注意事项
安全性将密码硬编码在脚本中是非常不安全的做法。建议使用 SSH 密钥对认证来提高安全性。依赖性此脚本依赖于 sshpass.exe 和 python请确保这些工具在你的系统上可用并且路径配置正确。权限可能需要管理员权限来运行某些命令特别是涉及到进程管理的部分。
使用方法 保存脚本 将上述批处理代码保存为一个 .bat 文件例如 auto_ssh_reverse_tunnel.bat。 运行批处理文件 双击 auto_ssh_reverse_tunnel.bat 文件或者在命令提示符中运行它auto_ssh_reverse_tunnel.bat通过这个整合后的批处理脚本你应该能够先运行 Python 脚本定期检查网络连接状态如果检测到网络问题则重新运行 Python 脚本并自动建立并维护 SSH 反向隧道连接。如果仍然遇到问题请确保所有路径和工具都已正确配置。