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

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...

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

Related

Secure CRT send command to an host VBS

Hey i have tried to make a simple script that can log in and send commands to the host/server:
Sub Main
crt.Screen.Synchronous = True
crt.Session.Connect "ip adress"
crt.Screen.WaitForString "Username: "
crt.Screen.Send "username" & chr(13)
crt.Screen.WaitForString "Password: "
crt.Screen.Send "password" & chr(13)
crt.Screen.Send "?" & chr(13)
crt.Screen.Synchronous = False
End Sub
my problem is it wont send anything.
Its working now:
Sub Main
host = Array("host1", "host2")
For each item in host
Dim user
user = "Username"
Dim passwd
passwd = "password"
cmd = "/SSH2 /L " & user & " /PASSWORD " & passwd & " /C AES-192-CTR /M SHA2-256 " & item
crt.Session.Connect cmd
Dim tab, index, nTabCount
nTabCount = crt.GetTabCount()
for index = 1 to nTabCount
set tab = crt.GetTab(index)
tab.activate
tab.screen.send "command" & vbcr
crt.Sleep 5000
crt.Session.Disconnect()
next
Next
End Sub

Vbscript Copy List of Folders to FTP Server

Hi I have the following script
Set oShell = CreateObject("Shell.Application")
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Path to file or folder to upload
Source0 = "C:\Users\User\Desktop\Work\CRC01\8BR01547\61000856"
FTPUpload(Source0)
Sub FTPUpload(Source0)
On Error Resume Next
'Copy Options: 16 = Yes to All
Const copyType = 16
'FTP Wait Time in ms
waitTime = 80000
FTPUser = "Martin"
FTPPass = ""
FTPHost = "PC247"
FTPDir0 = "/Martin/61000856"
strFTP = "ftp://" & FTPUser & ":" & FTPPass & "#" & FTPHost & FTPDir0
Set objFTP = oShell.NameSpace(strFTP)
'Make new folder on FTP site
'objFTP.NewFolder "FTP Backup"
'Upload single file
If objFSO.FileExists(Source0) Then
Set objFile = objFSO.getFile(Source0)
strParent = objFile.ParentFolder
Set objFolder = oShell.NameSpace(strParent)
Set objItem = objFolder.ParseName(objFile.Name)
Wscript.Echo "Uploading file " & objItem.Name & " to " & strFTP
objFTP.CopyHere objItem, copyType
End If
'Upload all files in folder
If objFSO.FolderExists(Source0) Then
'Code below can be used to upload entire folder
Set objFolder = oShell.NameSpace(Source0)
Wscript.Echo "Uploading folder " & Source0 & " to " & strFTP
objFTP.CopyHere objFolder.Items, copyType
End If
If Err.Number <> 0 Then
Wscript.Echo "Error: " & Err.Description
End If
'Wait for upload
WScript.Sleep waitTime
End Sub
Could someone please help on how to make it so that I can provide a list of sources and it copys the folders over to the FTP server. Also How to I fix the overwrite confirmation popup that still happens even if the copytype is set too 16

VBS Script for Windows Update

How would I remove the users request to "approve" everything in this .VBS script and instead just let it autorun/install everything found?
Details on how the script runs:
This .VBS script here runs on the Windows computer, searchs for Windows Update, then manually ask the user to "okay" each update it finds. Once the user hits "okay" and accepts the updates found, it then downloads it.
Once the Widnows Updates downloads, it then asks the user again to approve each Windows Update install. Which is not automated..... I'm not familiar with .VBS enough to edit this script.
Set updateSession = CreateObject("Microsoft.Update.Session")
updateSession.ClientApplicationID = "MSDN Sample Script"
Set updateSearcher = updateSession.CreateUpdateSearcher()
WScript.Echo "Searching for updates..." & vbCRLF
Set searchResult = _
updateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")
WScript.Echo "List of applicable items on the machine:"
For I = 0 To searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
WScript.Echo I + 1 & "> " & update.Title
Next
If searchResult.Updates.Count = 0 Then
WScript.Echo "There are no applicable updates."
WScript.Quit
End If
WScript.Echo vbCRLF & "Creating collection of updates to download:"
Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
For I = 0 to searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
addThisUpdate = false
If update.InstallationBehavior.CanRequestUserInput = true Then
WScript.Echo I + 1 & "> skipping: " & update.Title & _
" because it requires user input"
Else
If update.EulaAccepted = false Then
WScript.Echo I + 1 & "> note: " & update.Title & _
" has a license agreement that must be accepted:"
WScript.Echo update.EulaText
WScript.Echo "Do you accept this license agreement? (Y/N)"
strInput = WScript.StdIn.Readline
WScript.Echo
If (strInput = "Y" or strInput = "y") Then
update.AcceptEula()
addThisUpdate = true
Else
WScript.Echo I + 1 & "> skipping: " & update.Title & _
" because the license agreement was declined"
End If
Else
addThisUpdate = true
End If
End If
If addThisUpdate = true Then
WScript.Echo I + 1 & "> adding: " & update.Title
updatesToDownload.Add(update)
End If
Next
If updatesToDownload.Count = 0 Then
WScript.Echo "All applicable updates were skipped."
WScript.Quit
End If
WScript.Echo vbCRLF & "Downloading updates..."
Set downloader = updateSession.CreateUpdateDownloader()
downloader.Updates = updatesToDownload
downloader.Download()
Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")
rebootMayBeRequired = false
WScript.Echo vbCRLF & "Successfully downloaded updates:"
For I = 0 To searchResult.Updates.Count-1
set update = searchResult.Updates.Item(I)
If update.IsDownloaded = true Then
WScript.Echo I + 1 & "> " & update.Title
updatesToInstall.Add(update)
If update.InstallationBehavior.RebootBehavior > 0 Then
rebootMayBeRequired = true
End If
End If
Next
If updatesToInstall.Count = 0 Then
WScript.Echo "No updates were successfully downloaded."
WScript.Quit
End If
If rebootMayBeRequired = true Then
WScript.Echo vbCRLF & "These updates may require a reboot."
End If
WScript.Echo vbCRLF & "Would you like to install updates now? (Y/N)"
strInput = WScript.StdIn.Readline
WScript.Echo
If (strInput = "Y" or strInput = "y") Then
WScript.Echo "Installing updates..."
Set installer = updateSession.CreateUpdateInstaller()
installer.Updates = updatesToInstall
Set installationResult = installer.Install()
'Output results of install
WScript.Echo "Installation Result: " & _
installationResult.ResultCode
WScript.Echo "Reboot Required: " & _
installationResult.RebootRequired & vbCRLF
WScript.Echo "Listing of updates installed " & _
"and individual installation results:"
For I = 0 to updatesToInstall.Count - 1
WScript.Echo I + 1 & "> " & _
updatesToInstall.Item(i).Title & _
": " & installationResult.GetUpdateResult(i).ResultCode
Next
End If
Script output via WScript.Echo method: run your script using the command-line-based script host (e.g. Cscript.exe YourScript.vbs).
User input: replace strInput = WScript.StdIn.Readline with strInput = "Y" (all occurrences in your script).
To keep update log: use Cscript.exe YourScript.vbs > YourLog.txt.
Explanation:
Whether you use WScript or CScript, you still run the scripts in the same manner. The difference is only in the output — WScript generates windowed output, while CScript sends its output to the command window in which it was started. On initial installation, the default host is WScript. To change it to CScript, type the following at the command line: cscript //h:cscript.
I'd not use Cscript.exe YourScript.vbs < Prepared-Y.txt redirection: we don't know number of Y lines in input file as we can't estimate number of updates in advance; could lead to an error Microsoft VBScript runtime error: Input past end of file
Read Redirection

Ping script with email in vbs

i know i asked already the question about the ping script but now i have a new question about it :-) I hope someone can help me again.
strText = "here comes the mail message"
strFile = "test.log"
PingForever strHost, strFile
Sub PingForever(strHost, outputfile)
Dim Output, Shell, strCommand, ReturnCode
Set Output = CreateObject("Scripting.FileSystemObject").OpenTextFile(outputfile, 8, True)
Set Shell = CreateObject("wscript.shell")
strCommand = "ping -n 1 -w 300 " & strHost
While(True)
ReturnCode = Shell.Run(strCommand, 0, True)
If ReturnCode = 0 Then
Output.WriteLine Date() & " - " & Time & " | " & strHost & " - ONLINE"
Else
Output.WriteLine Date() & " - " & Time & " | " & strHost & " - OFFLINE"
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "noreply#test.net"
objEmail.To = "test#test.net"
objEmail.Subject = "Computer" & strHost & " is offline"
objEmail.Textbody = strText
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
"smtpadress"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send
End If
Wscript.Sleep 2000
Wend
End Sub
My problem is now, the mail comes all 2 seconds, when the computer are offline. Can someone show me how to make it with flags? So only one mail comes when its offline?
Thanks for your help.
Use a flag and report only when state changes
FLAG0 = "ON"
While(True)
ReturnCode = Shell.Run(strCommand, 0, True)
If ReturnCode = 0 Then
Output.WriteLine Date() & " - " & Time & " | " & strHost & " - ONLINE"
FLAG0 = "ON"
Else
Output.WriteLine Date() & " - " & Time & " | " & strHost & " - OFFLINE"
IF FLAG0 = "ON" THEN
FLAG0 = "OFF"
Set objEmail = CreateObject("CDO.Message")
...... rest of mailing code
END IF
End If

Ping script with loop and save in a txt

i try to make an Ping script with vbs. I need a Script, that ping (no ping limit, the program will run all the time) a computername in the network every 2 seconds and save the results in a txt file.
For Example:
06/08/2010 - 13:53:22 | The Computer "..." is online
06/08/2010 - 13:53:24 | The Computer "..." is offline
Now i try a little bit:
strComputer = "TestPC"
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '"_
& strComputer & "'")
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) Or objStatus.StatusCode <> 0 Then
..........
Next
And than i don't know how to make it. (I'm new with vbs :-))
I hope some one can help me.
Greeting,
matthias
Try this
Option Explicit
Dim strHost, strFile
strHost = "www.google.com" '"127.0.0.1"
strFile = "C:\Test.txt"
PingForever strHost, strFile
Sub PingForever(strHost, outputfile)
Dim Output, Shell, strCommand, ReturnCode
Set Output = CreateObject("Scripting.FileSystemObject").OpenTextFile(outputfile, 8, True)
Set Shell = CreateObject("wscript.shell")
strCommand = "ping -n 1 -w 300 " & strHost
While(True)
ReturnCode = Shell.Run(strCommand, 0, True)
If ReturnCode = 0 Then
Output.WriteLine Date() & " - " & Time & " | The Computer " & strHost & " is online"
Else
Output.WriteLine Date() & " - " & Time & " | The Computer " & strHost & " is offline"
End If
Wscript.Sleep 2000
Wend
End Sub
You put your pings inside a loop of some kind and then use Wscript.Sleep 2000 to sleep for 2 seconds.
Then you use the File System Object (FSO) to write to a file. Information can be found here.
Edit: Something like this might work:
Const OpenFileForAppending = 8
Dim fso, ts
Set fso = CreateObject("Scripting. FileSystemObject")
While 1 > 0 ' loop forever
Set ts = fso.OpenTextFile("c:\temp\test.txt", OpenFileForAppending, True)
' do your pinging code
'if ok
ts.WriteLine("OK")
'else
ts.WriteLine("Not OK")
'endif
ts.Close()
Wscript.Sleep 2000
Wend

Resources