scripts in vbscript ??

Post support questions that relate to the Windows 2003/2000/XP operating systems.

Moderators: Developers, Moderators

Post Reply
bennyb
Posts: 17
Joined: Sun Sep 22, 2002 1:40 pm
Location: Netherlands

scripts in vbscript ??

Post by bennyb »

Has anyone tried to use vbscript instead of Perl ?

If so how did you manage to do the 'last' print statement to 'transfer' the data into cacti ?

:o
User avatar
bulek
Cacti Pro User
Posts: 854
Joined: Mon May 20, 2002 2:07 am
Location: Poland
Contact:

Post by bulek »

You need to use WSH in conjunction with VBS. To have a console output you need to run your VB scripts with cscript.exe (not wscript.exe which is used by default). The following link will help you:

http://www.devguru.com/Technologies/wsh ... intro.html

- bulek
bennyb
Posts: 17
Joined: Sun Sep 22, 2002 1:40 pm
Location: Netherlands

Post by bennyb »

Tried this seems to work in console window but when invoking in cacti following error appear:
The filename, directory name, or volume label syntax is incorrect.
Any hint ?
User avatar
bulek
Cacti Pro User
Posts: 854
Joined: Mon May 20, 2002 2:07 am
Location: Poland
Contact:

Post by bulek »

Let's make a simple test - running vbscript from php:
C:\>type test.vbs
WScript.StdOut.Write("512")

C:\>type test.php
<?
$result = `cscript c:/test.vbs //nologo`;
echo "Result is " . $result . "\n";
?>

C:\>php -q test.php
Result is 512

C:\>
Seems to be working :)
Some more examples how to interact with stdin/stdout with VBS:
http://msdn.microsoft.com/library/defau ... ations.asp

- bulek
bennyb
Posts: 17
Joined: Sun Sep 22, 2002 1:40 pm
Location: Netherlands

Post by bennyb »

Bulek,

Perhaps I was unclear but I want to run vbs scripts instead of perl scripts to 'gather' the data.
So instead of
perl <path_to_cati>/scripts/ping.vbs < ip >
I want to use
cscript //nologo <path_to_cacti>/scripts/ping.vbs < ip >

This is what is giving the errors, running the script itself in a DOS box works fine !

-b
User avatar
bulek
Cacti Pro User
Posts: 854
Joined: Mon May 20, 2002 2:07 am
Location: Poland
Contact:

Post by bulek »

I understood you correctly. My example was simplified process of cacti gathering data. Cacti uses cmd.php to run all data input scripts with line:

$out_data_array = ParseDelimitedLine(`$str`,$delimeter);

$str variable contains your script name with arguments. In case of perl it would be 'perl somescript.pl arg1 arg2'. In case of vbscript it would be 'cscript somescript.vbs //nologo arg1 arg2'.

Sumarizing my example showed that test.php (which is used in place of cmd.php) was able to run vbscript test.vbs, fetch script's output and store it in a variable.

Maybe add some debug info in cmd.php. Check if <path_to_cacti> is resolved to real path. Check cscript syntax (//nologo should be between script name and its arguments). Check string returned before ParseDelimitedLine function is called.

BTW, It's interesting idea to use VBS to make data input scripts. Why I didn't think about this before you? :)

- bulek
Guest

Post by Guest »

Bulek,

Problem solved, I used wscript.echo instead of wscript.stdout.write.
Works well now.

My reason for vbs is speed, I noticed it takes much longer to load and execute a perl script then a vbs script.

I already have the ping.vbs working, anybody interested ?

-b
Oddo

Post by Oddo »

Yes, post it please. If you don't mind. :D
chris[uk]

please post ping.vbs

Post by chris[uk] »

please post ping.vbs :D :D
bennyb
Posts: 17
Joined: Sun Sep 22, 2002 1:40 pm
Location: Netherlands

Post by bennyb »

Ok,

Here is my version of ping.vbs :

Dim oArgs, Answer, fso

Set oArgs = WScript.Arguments
If oArgs.Count <> 1 Then Wscript.Quit(0)

Set MyShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

Const TempFile = "./ping.txt"

Program = "%comspec% /c ping.exe -n 1 " & oArgs(0) & " >" & TempFile
Return = MyShell.Run(Program,0,True)
Set MyFile = fso.OpenTextFile(TempFile)
Answer = MyFile.ReadAll
MyFile.Close()
'Set MyFile = fso.GetFile(TempFile)
'MyFile.Delete

If InStr(Answer,"ytes=") Then
p= InStr(Answer,"Average")
Wscript.StdOut.Write Val(Trim(Mid(Answer,p+10,10)))
Else
Wscript.StdOut.Write "0"
End If

Wscript.Quit(0)

function Val(vValue)
Dim lCount
Dim lResult
Dim strResult
if len(vValue) = 0 Then
lResult = 0
Else
if isnumeric(vValue) Then
lResult = cdbl(vValue)
Else
lCount = 1
Do until (isnumeric(mid(vValue,lCount,1))=false) or (lCount > len(vValue))
strResult = strResult & mid(vValue, lCount, 1)
lCount = lCount + 1
Loop

if len(strResult) = 0 Then
lResult = 0
Else
lResult = cdbl(strResult)
End if
End if
End if
Val = lResult
End function

Perhaps it can be shorter, but it is faster then the perl equivalent !
At least on my machine (P266).

-b
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest