为远程桌面连接设置邮件提醒
需求:远程桌面连接Windows 2012时,会收到邮件提醒,提醒内容包含登录者的IP信息。
1.创建一个vbs脚本,能通过外部的smtp服务器发出邮件的
2.创建一个“定时任务”,条件是用户登录时运行,运行上边创建好的vbs脚本
3.如果通知内容还需带上远程桌面登录者的IP,可以在vbs脚本上加上提取IP的内容
例子:
set sh = CreateObject("Wscript.Shell")
set Connections = CreateObject("Scripting.Dictionary")
call Main()
Function Main()
call GetConnections()
call ProcessConnections()
End Function
Function GetConnections()
i = 0
set shExec = sh.Exec("netstat -f")
Do While Not shExec.StdOut.AtEndOfStream
Line = shExec.StdOut.ReadLine()
If Instr(Line, "TCP") <> 0 Then
Set Connection = New NetworkConnection
Connection.ParseText(Line)
call Connections.Add(i, Connection)
i = i + 1
End If
Loop
End Function
Function ProcessConnections()
For Each ConnectionID in Connections.Keys
'wscript.echo ConnectionID & Connections(ConnectionID).RemoteIP
Next
End Function
Class NetworkConnection
Public Protocol
Public LocalIP
Public LocalPort
Public RemoteIP
Public RemotePort
Public Sub ParseText(Line)
dim i
For i = 5 to 2 Step -1
Line = Replace(Line, String(i, " "), " ")
Next
Line = Replace(Line, ":", " ")
Line = Right(Line, Len(Line) - 1)
Line = Split(Line, " ")
Protocol = Line(0)
LocalIP = Line(1)
LocalPort = Line(2)
RemoteIP = Line(3)
RemotePort = Line(4)
End Sub
Private Sub Class_Initialize
'MsgBox "Initialized NetworkConnection object"
End Sub
End Class
NameSpace = "http://schemas.microsoft.com/cdo/configuration/"
Set Email = CreateObject("CDO.Message")
Email.From = "18900000000@189.cn"
Email.To = "18900000000@189.cn"
Email.Subject = "有人登陆服务器 - " & ConnectionID & Connections(ConnectionID).RemoteIP
Email.Textbody = "注意检查登陆者是否合法!"
With Email.Configuration.Fields
.Item(NameSpace&"sendusing") = 2
.Item(NameSpace&"smtpserver") = "smtp.189.cn"
.Item(NameSpace&"smtpserverport") = 25
.Item(NameSpace&"smtpauthenticate") = 1
.Item(NameSpace&"sendusername") = "18900000000"
.Item(NameSpace&"sendpassword") = "youxiangmima"
.Update
End With
Email.Send
参考:
- https://www.landui.com/help/show-7431.html
- https://stackoverflow.com/questions/10706401/netstat-with-wmi-and-vbscript
- https://www.reddit.com/r/sysadmin/comments/1xqycr/email_notifications_when_rdp_logon_or_logoff/
- https://community.spiceworks.com/scripts/show/2056-email-rdp-successful-logon
- http://help.189.cn/client/client.html