Showing posts with label cmdline. Show all posts
Showing posts with label cmdline. Show all posts

Tuesday, September 16, 2008

Automating Windows/Unix/Linux Server calls by w3Sockets

Earlier I have written a post Expect-Automating Command line tools for Expect utility. Now we can access any server machines by using VBScript. Dimac Development has implemented a COM interface to connect through Telnet and made as freeware. You need to download w3Sockets Dll and register it using SocketReg.exe included in a zip file. It is similar to expect utility. You can verify each command and can change the execution flow based on console output. It is much useful for automated testing as well as server administration. Below I have given a sample code to start windows services.

w3Sockets Reference
w3Sockets - Download Page

To start servers on Windows/Unix/Linux Systems



'To start servers on Windows/Unix/Linux server
Dim w3sock

Set w3sock = CreateObject("socket.tcp")
With w3sock
.timeout = 5000
.DoTelnetEmulation = True
.TelnetEmulation = "TTY"
.Host = "myserver:23"
.Open
.WaitFor "login:"
WScript.Echo .Buffer
.SendLine "myloginid"
.WaitFor "password:"
.SendLine "Password12"
.WaitFor ">"
.SendLine "net start ""MyServer Manager"""
.WaitFor ">"
WScript.Echo .Buffer
.SendLine "net start ""MYDB Repository"""
.WaitFor ">"
WScript.Echo .Buffer
.Close
End With
Set w3sock = Nothing

Friday, August 8, 2008

VBScript - Numbers as CmdLine Arguments

While I was preparing code for Excel Automation , I decided to pass the few integer values. Always the script got failed, if I pass the integer values through arguments. But the script went through fine, if I directly pass the parameters in that function call. While debugging the code, I was not able to find the reason for failure. I did Google search and unable to get the right solution.

Later I analyzed the code and put the simple conversion, while getting the arguments. The code worked as expected.


  1. To Convert String to Integer --> iStartRow = CInt (WScript.Arguments.Item(1))

  2. To Convert String to Double --> dExpVal = CDbl (WScript.Arguments.Item(2))

  3. To Convert String to Number --> nRowRange = Val (WScript.Arguments.Item (3))

To know more about the Command Line Arguments to VBScript, go through this post - Passing Command Line Arguments .

If you are unable to run any VBScript, See my old post - Unable to run VBScript / WScript / CScript in Windows XP .

Tuesday, May 6, 2008

VBScript - Passing CmdLine Arguments

I was searching on net, how to pass the command line arguments. It is possible. We can pass the command line arguments in VBScript. If you have worked with command-line tools, you are already familiar with the concept of arguments. Arguments can be divided as Named Arguments and UnNamed Arguments.

Named arguments
Named arguments are arguments that consist of two parts: a name and a value. The name must be prefaced with a forward slash, and a colon must separate the name from the value. The slash prefix and the colon separator are fixed and cannot be changed. See below Example.

RemoteServer.vbs /Host:AppSrv1 /App:Symantec /TimeLimit:2000
Item : Value
Host:AppSrv1
App:Symantec
TimeLimit:2000


UnNamed arguments
The WshUnnamed filtered collection contains the one unnamed argument. The WshUnnamed Count method and Length property return the number of filtered arguments in the WshUnnamed collection. Example is given below.

FormatMask.vbs "D:\VB\FormatMask\Complex.xls" 30 12
Item : Value
0 : D:\VB\FormatMask\Complex.xls
1 : 30
2 : 12


Source: Working with Command-Line Arguments

Code: Example of UnNamed arguments



If WScript.Arguments.Count = 3 Then
ServerName = WScript.Arguments.Item(0)
EventLog = WScript.Arguments.Item(1)
EventID = WScript.Arguments.Item(2)
Else
Wscript.Echo "Usage: GetEvents.vbs ServerName EventLog EventID"
Wscript.Quit
End If


Code: Named arguments


Set colNamedArguments = WScript.Arguments.Named

strServer = colNamedArguments.Item("Server")
strPacketSize = colNamedArguments.Item("PacketSize")
strTimeout = colNamedArguments.Item("Timeout")
Wscript.Echo "Server Name: " & strServer
Wscript.Echo "Packet Size: " & strPacketSize
Wscript.Echo "Timeout (ms): " & strTimeout

Monday, February 25, 2008

Expect - Automating Command line tools

Automating command line (CLI) tasks are always challenging for GUI automation tools. In my previous company, I used to use EXPECT for this kind of tasks. You can use telnet utility or Expect scripts to do this. I used EXPECT with Silktest. Expect is having a feature, you can feed the input based on the previous command's result. You can take baseline and compare with the current result.

You can connect with different operating Systems by expect scripts. It is developed as a package with Perl and Tcl/Tk. Three commands are central to the power of Expect. They are send, expect and spawn . The send command sends strings to a process, the expect command waits for strings from a process, and the spawn command starts a process.

Important Links
EXPECT home page
Windows version of EXPECT is available here
Getting Started

Sample
Call your EXPECT script by an batch or shell script. There you can pass the data as arguments.

4test statement:
SYS_Execute ("{GetProgramDir ()}\runftp_twiceexp.bat
{GetProgramDir ()}\ {lsInput[1]} {lsInput[2]}}} {lsInput[8]}", lsOutput)

Sample EXPECT code:


-----------------------------------------
##!/usr/local/bin/expect -f
#
# Automation for Unix system
#
#To delete the files from outbox table.
#
# hub name, installation path name, mailbox name is required.
#
# Author: Palani Selvam
#

# Check expect version
exp_version -exit 5.0.3

# root user login details
set username "root"
set upass "myrootpwd"

#super user login details
#set superuser "su"
#set supass "g0india"

#client,mailbox details
set pesetup_path "/opt/comp/pal_pnr"
set peclient "testhub"
set pmailbox "test_auto"

set timeout 10

#Start Telnet session
#spawn telnet jaguar

spawn telnet
expect "telnet>"
#send "telnet\r"
send "open jaguar\r"
sleep 1

expect {
"ogin*" {
# In case login is "Login or login"
send "$username\r"
sleep 1
}
timeout {
send_user "connection has timed out.\r\n"
exit
}
}
expect {
"assword*" {
# In case password is "Password or password"
send "$upass\r"
sleep 5
}
timeout {
send_user "password field has timed out.\r\n"
exit
}
}

# Partner or hub
expect {
"#*" {
send "cd $pesetup_path/data/stpush\r"
sleep 5
send "cd $peclient/$pmailbox/outbox\r"
sleep 4
send "ls -ltr *\r"
sleep 5
}
timeout {
send_user "Super user login has failed.\r\n"
exit
}
}

expect {
"rw*" {
send "cd completed\r"
sleep 2
send "rm *\r"
sleep 2

send "cd ../failed\r"
sleep 2
send "rm *\r"
sleep 2

send "cd ../partial\r"
sleep 2
send "rm *\r"
sleep 2

send "cd ../pending\r"
sleep 2
send "rm *\r"
sleep 2

send "cd ../retry\r"
sleep 2
send "rm *\r"
sleep 2
#puts "All files are deleted in outbox table.\r\n"
}
timeout {
send_user "Files are not existing in outbox table.\r\n"
exit
}
}

#To exit from the telnet
send "exit\r"
sleep 2
send "exit\r"

#Close the session
expect eof
-----------------------------------------

Tuesday, February 19, 2008

Tip2 - CommandLine Tab

In unix systems, we will get a folder, which is under in current directory. Similarly we can make command prompt in Windows.

To do that, Go to “HKEY_CURRENT_USER\Software\Microsoft\Command Processor” in the registry (regedit.exe) and set the ‘CompletionChar’ value data as ‘9’. Here 9 refers the TAB key. Now close the command prompt and open a new command prompt. Now, if you press TAB key, the file/directory name will be displayed!.

Note: While using regedit, take care. Otherwise your system may not reboot properly...