用vbs写入网址到hosts,要求不能从复写入。

效果如下,双击脚本 写入屏蔽网址 www.akak.com,要是有此网址就跳过不写入,没有就写入即可。

试一下 ,如有help给个采纳谢谢


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Windows\System32\drivers\etc\hosts", 8, True)
strIP = "127.0.0.1"
strWebSite = "www.akak.com"
strEntry = strIP & " " & strWebSite
Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    If InStr(strLine, strWebSite) = 0 Then
        objFile.WriteLine strEntry
    End If
Loop
objFile.Close

有帮助的话采纳一下


Dim fso, file, strHost, arrHost 
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\Windows\System32\drivers\etc\hosts", 1, True)

strHost = InputBox("请输入要添加的网址(格式: IP 网址):", "添加网址")
arrHost = Split(strHost, " ")

Do While Not file.AtEndOfStream
    strLine = file.ReadLine
    If InStr(strLine, arrHost(1)) > 0 Then 
        MsgBox "该网址已经存在,不再重复添加!"
        Exit Do
    End If
Loop

file.WriteLine strHost
file.Close

MsgBox "网址添加成功!"
Set objFSO = CreateObject("Scripting.FileSystemObject")  
Set objFile = objFSO.OpenTextFile("C:\Windows\System32\drivers\etc\hosts", 2, True)  
  
Dim url, existingUrl  
url = "127.0.0.1 example.com" '替换为您要写入的网址  
existingUrl = objFile.ReadLine '读取当前行的内容  
  
Do While Not existingUrl Is Nothing '循环读取文件的所有行  
    If InStr(existingUrl, url) > 0 Then '如果当前行包含要写入的网址  
        WScript.Echo "URL already exists in hosts file." '输出提示信息  
        Exit Do '退出循环  
    End If  
    existingUrl = objFile.ReadLine '读取下一行的内容  
Loop  
  
objFile.WriteLine url '如果网址不存在,则将网址写入文件  
objFile.Close '关闭文件

#如有帮助,恭请采纳

这个脚本会检查 hosts 文件是否已经存在 www.akak.com,如果存在则不执行写入操作。

On Error Resume Next

Dim FilePath, BlockedUrl, objFso, objFile, objTextStream, strContents

BlockedUrl = "127.0.0.1 www.akak.com"
FilePath = "C:\Windows\System32\drivers\etc\hosts"

' 创建一个文件系统对象
Set objFso = CreateObject("Scripting.FileSystemObject")

' 检查 hosts 文件是否存在,并且文件没有被锁定或只读
If objFso.FileExists(FilePath) Then
    Set objFile = objFso.GetFile(FilePath)
    If objFile.Attributes And 1 Then
        ' 文件被锁定或只读,输出错误消息并退出脚本
        WScript.Echo "The file is locked or read-only."
        WScript.Quit
    End If
Else
    ' 文件不存在,输出错误消息并退出脚本
    WScript.Echo "The file does not exist."
    WScript.Quit
End If

' 打开 hosts 文件并检查是否已存在要屏蔽的网址
Set objTextStream = objFso.OpenTextFile(FilePath)
strContents = objTextStream.ReadAll
objTextStream.Close

If InStr(strContents, BlockedUrl) > 0 Then
    ' hosts 文件中已存在要屏蔽的网址,输出提示消息
    WScript.Echo "The URL is already blocked."
Else
    ' hosts 文件中不存在要屏蔽的网址,进行写入操作
    Set objTextStream = objFso.OpenTextFile(FilePath, 8, True)
    objTextStream.Write BlankSpace(2) & "#" & BlockedUrl & vbCrLf
    objTextStream.Write BlockedUrl & vbCrLf
    objTextStream.Close
    
    ' 输出提示消息以及写入的网址信息
    WScript.Echo "The URL has been blocked: " & BlockedUrl
End If

' 释放对象资源
Set objFso = Nothing
Set objFile = Nothing
Set objTextStream = Nothing

' 返回一个包含指定空格数目的字符串
Function BlankSpace(n)
    Dim s
    For i = 1 To n
        s = s & " "
    Next
    BlankSpace = s
End Function
请将脚本保存为 .vbs 文件并双击运行。如果脚本成功写入 hosts 文件,则会输出 “The URL has been blocked: 127.0.0.1 www.akak.com”,否则会输出相应的错误消息。

使用VBS实现Hosts文件一键配置实现代码
有现成的
https://www.yingsoo.com/news/devops/42437.html