Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

对Windows脚本cfst_hosts.bat的修改建议 #508

Closed
Typhonling opened this issue Mar 22, 2024 · 3 comments
Closed

对Windows脚本cfst_hosts.bat的修改建议 #508

Typhonling opened this issue Mar 22, 2024 · 3 comments
Labels
功能建议 功能与建议

Comments

@Typhonling
Copy link

Typhonling commented Mar 22, 2024

功能需求

多次发现Windows脚本经过一段时间的自动运行后,系统hosts与nowip_hosts.txt不同步了,导致后续无法继续自动替换IP,猜测是某次脚本中断或者获取管理员权限出现问题。
建议先修改cfst_hosts.bat同目录下的hosts文件,然后对系统hosts进行覆盖,这样的好处是修改同目录下hosts不需要管理员权限,不太会出现问题,如果某次覆盖系统hosts失败,下次运行脚本后仍能正确修改系统hosts。
当然这么做会影响其他hosts条目,可以将系统hosts复制出来再做修改,如果有其他动态hosts需求,那这个方法就不适用了。

预期目标

::CD /d "C:\Windows\System32\drivers\etc"
echo.
echo 开始备份 Hosts 文件(hosts_backup)...
copy hosts hosts_backup
echo.
echo 开始替换...
(
    for /f "tokens=*" %%i in (hosts_backup) do (
        set s=%%i
        set s=!s:%nowip%=%bestip%!
        echo !s!
        )
)>hosts
copy hosts "C:\Windows\System32\drivers\etc\hosts"
@Typhonling Typhonling added the 功能建议 功能与建议 label Mar 22, 2024
@XIU2
Copy link
Owner

XIU2 commented Mar 22, 2024

那这样的话,你要如何同步 C:\Windows\System32\drivers\etc\hosts 与脚本旁边的 hosts 文件内容?
如果你在前面将系统的 hosts 复制过来同步了旁边的 hosts,那么当因为某些原因没有成功把旁边的 hosts 副本复制覆盖到 C:\Windows\System32\drivers\etc\hosts 时,此时 nowip_hosts.txt 和旁边 hosts 里的 IP 是新的,系统 hosts 里的 IP 还是旧的,下回再运行脚本依然会将系统 hosts 同步过来,导致旁边的 hosts 里的 IP 变成旧的,依然和 nowip_hosts.txt 里的 IP 不对应,从而无法继续替换下去。

@Typhonling
Copy link
Author

不是哦,将系统hosts复制出来再做修改这个操作是一次性的,是为了保留原来hosts中有用的条目。如果系统hosts中没有内容,那这步可以替换为在CF目录下新建一个hosts文件。

@XIU2
Copy link
Owner

XIU2 commented Mar 23, 2024

如果只是一开始将系统 hosts 复制出来一次,后续都只是修改旁边的 hosts 并复制到系统 hosts 覆盖,我知道你想的是即使某次覆盖出现问题,旁边 hosts 里的 IP 和 nowip_hosts.txt 里的 IP 也依然都是新的,依然可以继续替换继续覆盖系统 hosts 下去。

但这样的话,如果你要更改系统 hosts,那么就需要手动同步到旁边 hosts 的内容,否则等下次运行脚本替换 IP 覆盖系统 hosts 的时候,系统 hosts 里你修改的内容都会被重置为旁边 hosts 的内容了。

所以你用一个更大更麻烦的问题来解决一个小问题。。。
你可能不嫌麻烦,或者说不怎么改动系统 hosts 问题,这修改方案可能适合你,但绝对不适合所有人。
你可以把你的 cfst_hosts.bat 按照你的意愿修改。

猜测是某次脚本中断或者获取管理员权限出现问题
另外,这本来就是个小概率问题,至少我以前都没遇到过,也没有人提过这个问题。

@XIU2 XIU2 closed this as completed Mar 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
功能建议 功能与建议
Projects
None yet
Development

No branches or pull requests

2 participants