SMS via Skype when server is down (fails ping)

Requires registration of downloadable ActiveX comp from Skype web site on Windows machine.
Register with:
regsvr32 Skype4COM.dll
Make sure Skype is installed and logged in. When you run the prog the first time you are asked a
security question. You will need credit on your Skype account and it must be useable
through the network (if doing this from within a company and behind a firewall).
Copy the code from Option Explicit to bottom into a file called isalive.ebs

usage:cscript isalive.ebs <server> <phone> <interval> <mail recipient>
Returns: Writes to console. Sends a text if server goes off line. Sends an email also
with contents of a log file.
Requires a batch file containing words to the affect:
tracert -d %1 >> c:\diags.txt
echo "Further Info:
ping server1.fdqn >>c:\diags.txt
ping server2.fqdn >>c:\diags.txt
.
Batch file must be in same dir as the vbs.
Sends a text when it comes back on-line. Code below:

Option Explicit
'skype me a text or voicemail when my server disappears off the network.
'runs on a windows platform (whatever)
'useage:
'cscript isalive.vbs 192.168.1.109 004412345678 3000 xxxxxxxxxxxx@gmail.com
'where 192.168.1.109 is the server to ping
'and 004412345678 is the number to send a message to

Dim strHost
Dim strPhoneUser
Dim fault_detected
Dim strMessage
Dim strRecipient
Dim sleeptime
Dim oSkype
Set oSkype = WScript.CreateObject("Skype4COM.Skype", "Skype_")
If Not oSkype.Client.IsRunning Then oSkype.Client.Start() End If
' Check that all arguments required have been passed.
If Wscript.Arguments.Count < 2 Then
    Wscript.Echo "Arguments required" & vbCrLf
     Wscript.Quit(0)
End If
strHost = Wscript.Arguments(0)
strPhoneUser=Wscript.Arguments(1)
sleeptime=3000
If (Wscript.Arguments.Count > 2) Then
	sleeptime = Wscript.Arguments(2)
End If
strRecipient = ""
If (Wscript.Arguments.Count >3 ) Then
	strRecipient = Wscript.Arguments(3)
End If

Wscript.Echo "Host " & strHost
Wscript.Echo "Phone " & strPhoneUser
Wscript.Echo "Recipient " & strRecipient
Wscript.Echo "sleep " & sleeptime

fault_detected = 0
do
if Ping(strHost) = True then
    Wscript.Echo "Host " & strHost & " contacted"
	if (fault_detected > 0) Then
		strMessage= "Server " & strHost & " back on network after " & ((fault_detected * sleeptime)/1000) & " seconds"
		call_contact

		fault_detected = 0
	End If
Else
    Wscript.Echo "Host " & strHost & " could not be contacted"
	fault_detected = fault_detected + 1
	If (fault_detected = 1) Then
		strMessage= "Server " & strHost & " missing from network"
		Wscript.Echo "Contacting user on tel. no." & strPhoneUser
		call_contact
		do_diagnostics ' do further research then mail someone with the information
		mailuser
	End If
end if
WScript.sleep (3000)
loop until (1=0)

Function call_contact
Dim oSMS
	Wscript.Echo "SMS : " & strMessage
         Set oSkype = WScript.CreateObject("Skype4COM.Skype", "Skype_")
        Set oSMS = oSkype.SendSms(strPhoneUser, strMessage)
         WScript.Sleep(20000)
End Function

 'Message event handler:
 Public Sub Skype_SmsMessageStatusChanged(ByRef aSms, ByVal aStatus)
   WScript.Echo  ">Sms " & aSms.Id & " status " & aStatus & " " & oSkype.Convert.SmsMessageStatusToText(aStatus)
 End Sub

 'Target event handler:
 Public Sub Skype_SmsTargetStatusChanged(ByRef aTarget, ByVal aStatus)
   WScript.Echo  ">Sms " & aTarget.Message.Id & " target " & aTarget.Number & " status " & aStatus & " " & oSkype.Convert.SmsTargetStatusToText(aStatus)
 End Sub

Function Ping(strHost)
    dim objPing, objRetStatus
    set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
      ("select * from Win32_PingStatus where address = '" & strHost & "'")

    for each objRetStatus in objPing
        if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 then
    Ping = False
        else
            Ping = True
         end if
    next
End Function

Function mailuser
Wscript.Echo "Mailing user"
Dim cdoConfig
Dim sch
Dim objMessage
Set cdoConfig = CreateObject("CDO.Configuration")
 sch = "http://schemas.microsoft.com/cdo/configuration/"
    With cdoConfig.Fields
        .Item(sch & "sendusing") = 2 ' cdoSendUsingPort
        .Item(sch & "smtpserver") = "aserver.net"
        .update
    End With 

Set objMessage = CreateObject("CDO.Message")
Set objMessage.Configuration = cdoConfig
objMessage.Subject = "Server Status for: " + StrHost
objMessage.From = "xxxx@xxxx.com"
objMessage.To = strRecipient
objMessage.TextBody = "The Server vanished from the network. Please see the attached file."
 objMessage.AddAttachment "c:\diags.txt"
objMessage.Send
set cdoConfig = nothing
set objMessage = nothing
set sch = nothing
End Function

Function do_diagnostics ' creates an attachment that will be emailed. diags.bat runs a tracert on the server and pings a few
Wscript.Echo "Running further diags"
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "diags.bat " + strHost '
'WshShell.Run "command.com /k " & CommandLine 'run DOS commands
WScript.Sleep(20000)
Set WshShell= nothing
End Function

1 Comment »

  1. Dejan said

    This is Fantastic! Thank you for making my life easier!

RSS feed for comments on this post · TrackBack URI

Leave a Comment