Simon
Simon

Reputation: 33

script to ping a 'router' from pinging a single hostname

I am trying to write a script where it will prompt you for a hostname, say 222012-DC01 from this it will resolve the ip address from dns via ping or nslookup.

It should then modify the ip address to ping the router for that site. If the ip address was 10.123.2.1 it should ping 10.123.1.1 for the router for that site.

The script should give you an output of server offline or router offline.

Work in an enviroment looking after several hundred sites, we take ownership of servers but not network incidents such as a router failure.

Your help is much appreciated...

Upvotes: 0

Views: 1367

Answers (1)

peter
peter

Reputation: 42182

In the future please post what you have got and tried. Here is a tested script

function get_ip(strTarget)
  set objShell = CreateObject("WScript.Shell")
  set objExec = objShell.Exec("ping -n 2 -w 1000 " & strTarget)
  strPingResults = LCase(objExec.StdOut.ReadAll)
  get_ip = ""
  if InStr(strPingResults, "reply from") then
    wscript.echo VbCrLf & strTarget & " responded to ping."
    set regEx = New RegExp
    regEx.Pattern = "\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"
    set matches = regEx.Execute(strPingResults)
    if matches.count > 0 then
      get_ip = matches(0)
    end if
  else
    wscript.echo vbCrLf & strTarget & " did not respond to ping."
    wscript.quit 1
  end If
end function

function is_online(strTarget)
  set objShell = CreateObject("WScript.Shell")
  set objExec = objShell.Exec("ping -n 2 -w 1000 " & strTarget)
  strPingResults = LCase(objExec.StdOut.ReadAll)
  if inStr(strPingResults, "reply from") then
    is_online = true
  else
    is_online = false
  end If
end function

pcname = inputbox( "Give the pc-name to ping:")
ip = get_ip(pcname)
wscript.echo "ip of pc " & pcname & " is " & ip
aIp = split(ip, ".")
aIp(2) = 1
router = join(aIp, ".")
wscript.echo "pinging " & router
if is_online(strTarget) then
  wscript.echo "router is online"
else
  wscript.echo "router is offline"
end if

Upvotes: 1

Related Questions