VBS监控飞信机器人在线,支持多个机器人(代码实例)
‘call StartAllBAT()
‘call KillAllBAT()
‘忽视运行错误
‘On Error Resume Next
‘飞信可执行文件名称
ExeName=”fetion.exe”
‘循环监听的时间(单位:秒)
ListenTime=60
‘是否隐藏DOS窗口
HideDos=false
‘所有Fetion可执行文件列表(与BAT文件索引对应)
ExePath=array(_
“C:\fetion_990875\fetion.exe”,_
“C:\fetion_dltp\fetion.exe”,_
“C:\fetion_jh\fetion.exe”,_
“C:\fetion_wxyy\fetion.exe”,_
“C:\fetion_yx\fetion.exe”,_
“C:\kmlele\fetion.exe”,_
“C:\FectionTest\fetion.exe”_
)
‘所有BAT文件列表(与Fetion可执行文件索引对应)
BatPath=array(_
“C:\fetion_990875\start.bat”,_
“C:\fetion_dltp\start.bat”,_
“C:\fetion_jh\start.bat”,_
“C:\fetion_wxyy\start.bat”,_
“C:\fetion_yx\start.bat”,_
“C:\kmlele\start.bat”,_
“C:\FectionTest\start.bat”_
)
‘获取监听列表
Function GetListenList(ExeName)
On Error RESUME Next
Dim TaskArr(10)
i=0
Dim objWMIService,colItems,objItem,objAddress
strComputer=”.”
Set objWMIService = GetObject(“winmgmts:\\”&strComputer&”\root\cimv2”)
Set colItems = objWMIService.ExecQuery(“Select * From Win32_Process Where name='”&ExeName&”‘”)
For Each objItem in colItems
‘累计所有进程路径
‘Msgbox objItem.ExecutablePath
TaskArr(i)=objItem.ExecutablePath
i=i+1
Next
GetListenList=TaskArr
End Function
‘执行BAT文件
Function ExecuteBAT(file,HideDos)
Set w=Createobject(“wscript.shell”)
w.run file,HideDos
Set w=nothing
End Function
‘判断数组是否存在某元素
Function In_Array(val,arr)
Reval=false
For Each e_arr in arr
IF e_arr=val Then
Reval=true
Exit For
End If
Next
In_Array= Reval
End Function
‘————————–
‘执行所有BAT文件
Function StartAllBat()
For Each Bat in BatPath
call ExecuteBAT(Bat,HideDos)
Next
End Function
‘终止所有BAT
Function KillAllBAT()
Set w=Createobject(“wscript.shell”)
For i=1 to 10 step 1
w.run “taskkill /f /im cmd.exe”,0
Next
End Function
‘————————
‘检测进程不存在时执行的函数
Function NotExist(file,bat)
call ExecuteBAT(bat,1)
call WriteLog(now()&” 执行 “&bat&space(5)&file&vbcrlf,”log.txt”)
End Function
‘写入日志文件
Function WriteLog(str,file)
Set fso=Createobject(“scripting.filesystemobject”)
if Not fso.fileexists(file) Then fso.createTextfile(file)
Set File=fso.openTextFile(file,8)
File.write str
File.close
Set File=nothing
Set fso=nothing
End Function
‘—————————
‘执行循环监听
Do
tasklist=GetListenList(ExeName)
j=0
For Each JC IN ExePath
If Not In_Array(JC,tasklist) Then
call NotExist(JC,BatPath(j))
End If
j=j+1
Next
wscript.sleep ListenTime*1000
Loop