how to add multiple command inside if statment - vbscript

my objective is to check if the switch prompt either ">" or "#" if prompt to ">" to send enable password then the command. if the switch prompt to "#" send just the command. but some reason go to directly to the command with out checking the condition.
` result = crt.Screen.WaitForString ([">","#"])
If (result=1) Then
crt.Screen.Send chr(13)
crt.Screen.Send "XXXXX" & chr(13)
crt.Screen.WaitForString "Password: "
crt.Screen.Send "a" & chr(13)
crt.Screen.WaitForString "#"
End If
'++++++++++Now Send the command ++++++++++
crt.Screen.Send "sh ver" & chr(13)
crt.Screen.WaitForString vbcr
strReadScreen = crt.Screen.ReadString("#")`

Even if the condition is checked it may fail or not, the command is always executed.
Try to check for '>" first, then make your special stuff ... then check for both and call command
if(==">"){
// do stuff here
}
if(=="#" || ==">"){
// send command
}
">" and "#" would mean result==1 or result==2 in your code.

Related

Exit a Secure CRT script without clossing the application or session

I am using a vbscript via Secure CRT, containing different subs as well as global variables, outside the main sub, in order to be available for the subs.
I would like to ask if there is a way of exiting the script at any point desired, before reaching the end of the process and without using crt.Quit, because it would be better to have secure crt running and available for additional commands.
My sript is as follows
Intro = msgbox ("ONLY FOR AUTHORISED USE!!!", 1+16+0+4096)
If Intro =2 Then
MsgBox"Operation Cancelled"
ThisIsTheEnd
End If
DSLAM=inputbox ("Please Enter Dslam Name","Login")
If DSLAM="" Then
MsgBox"Operation Cancelled"
ThisIsTheEnd
End If
crt.Screen.Send "telnet " & DSLAM & Chr(13)
crt.Screen.WaitForString "login:"
crt.Screen.Send "xxxx" & Chr(13)
crt.Screen.WaitForString "Password:"
crt.Screen.Send "xxxxxxxx" & Chr(13)
crt.Screen.WaitForString ">"
crt.Screen.Send "enable" & Chr(13)
crt.Screen.WaitForString "Password:"
crt.Screen.Send "xxxxxxxx" & Chr(13)
crt.Screen.WaitForString "#"
crt.Screen.Send "conf t" & Chr(13)
crt.Screen.WaitForString "(config)#"
crt.Screen.Send "br" & Chr(13)
crt.Screen.WaitForString "#"
CARD=inputbox("Please Enter Dslam Card", "Dslam Card")
If Card="" Then
MsgBox"Operation Cancelled"
crt.Screen.Send "end" & Chr(13)
crt.sleep 500
crt.Screen.Send "exit" & Chr(13)
ThisIsTheEnd
End If
PORT=inputbox("Please Enter Dslam Port", "Dslam Port")
If Port="" Then
MsgBox"Operation Cancelled"
crt.Screen.Send "end" & Chr(13)
crt.sleep 500
crt.Screen.Send "exit" & Chr(13)
ThisIsTheEnd
End If
Sub Main()
Profile
NextAction
ThisIsTheEnd
End Sub
Sub Profile
SELPROFILE=inputbox("1.free_VDSL_8B_Default" & Chr(13) & "2.free_VDSL_12A" & Chr(13) & "3.free_VDSL_17A", "Please Select the Profile")
crt.Screen.Send "port lre" & " " & CARD & "/" & PORT & " disable" & Chr(13)
crt.sleep 500
If SELPROFILE=1 Then
MsgBox "Default 8B Profile Selected"
crt.Screen.Send "lre" & " " & CARD & "/" & PORT & " xdsl line-config free_VDSL_8B_Default" & Chr(13)
ElseIf SELPROFILE=2 Then
MsgBox "12A Profile Selected"
crt.Screen.Send "lre" & " " & CARD & "/" & PORT & " xdsl line-config free_VDSL_12A" & Chr(13)
ElseIf SELPROFILE=3 Then
MsgBox "17A Profile Selected"
crt.Screen.Send "lre" & " " & CARD & "/" & PORT & " xdsl line-config free_VDSL_17A" & Chr(13)
ElseIf SELPROFILE<>1 or 2 or 3 Then
MsgBox "INVALID PROFILE!!!", 0+16+0+4096
Profile
End If
crt.sleep 500
crt.Screen.Send "port lre" & " " & CARD & "/" & PORT & " enable" & Chr(13)
crt.sleep 500
crt.Screen.Send "end" & Chr(13)
crt.sleep 500
crt.Screen.Send "wr mem" & Chr(13)
crt.Screen.WaitForString "#"
End Sub
Sub NextAction
GOON=inputbox("1.Exit" & Chr(13) & "2.Show Sync" & Chr(13) & "3.Repeat", "Please select next action")
if GOON=1 then
crt.Screen.Send "exit" & Chr(13)
ElseIf GOON=2 then
crt.Screen.Send "sho lre" & " " & CARD & "/" & PORT & " xdsl phys-table linerates" & Chr(13)
NextAction
ElseIf GOON=3 then
crt.Screen.Send "conf t" & Chr(13)
crt.Screen.WaitForString "(config)#"
crt.Screen.Send "br" & Chr(13)
crt.Screen.WaitForString "#"
Profile
Else MsgBox "INVALID SELECTION!!!", 0+16+0+4096
NextAction
End if
End Sub
Sub ThisIsTheEnd
MsgBox"Thank you, come again"
crt.Quit
End Sub
So to summarize everything up, I want to have the option of exiting the script at the various message-input boxes stages, via the cancel option and without closing Secure CRT.
The whole script has to run through Secure CRT so the WScript.Quit is not an option.
Thank you for your time!
So the problem was finally solved by a declaring globally the variables used on the subs, adding a Do Until Loop and rearranging the subs in order that the script can end with the Exit and End Sub commands.
Looks like the End Sub (when used correctly) is enough for terminating the script at any point desired.

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

Need a Windows batch script that will telnet into a server and run a command

Looking for Windows batch file which will telnet to one IP address, automatically provide username & password which is specified in batch file, execute some series of commands and exit from telnet.
#echo off
SET username=abc
SET password=xyz#1234
SET servername=192.168.1.40
echo user %username%> telnetcmd.dat
echo %password%>> telnetcmd.dat
echo adbd^& >> telnetcmd.dat
echo exit>> telnetcmd.dat
telnet %servername% < telnetcmd.dat
del telnetcmd.dat
Here I am trying to do telnet to IP with username & password. After successful login it should execute adbc& (This will run necessary service in background) command and exit command. Once I complete this batch file and if I check adbc service running on server or not it is showing not running on server.
Output I am getting after completion of batch file.
Welcome to Microsoft Telnet Client
Escape Character is 'CTRL+]'
c:\Users\vshah\Desktop>
Expected Output:
root#pqrs:/ # ps | grep adbd
shell 31899 31828 1348 148 ffffffff 00013348 S adbd
Actual Output after executing batch file:
root#pqrs:/ # ps | grep adbd
1|root#pqrs:/ #
Kindly help me on this to achieve this scenario.
Thank your very much in advance.
It is not actually a batch, but rather a vbscript, and it uses sleep, so in high latency situations, it easily fail, but I have used the following script to test sending email via telnet
smtpadr = InputBox("SMTP server address", "","192.168.1.35" , 100, 200)
localname = InputBox("SMTP", "","mx01.domain.com" , 100, 200)
emailadr = InputBox("Email address", "","jonbdk#domain.com" , 100, 200)
if smtpadr & "" = "" OR localname & "" = "" OR emailadr & "" = "" then
wscript.quit
end if
set Shell=CreateObject("WScript.Shell")
Shell.run "cmd /K"
wscript.sleep(1000)
sendkeys ("telnet "&smtpadr&" 25{ENTER}")
wscript.sleep(4000)
'sendkeys ("quit{ENTER}")
sendkeys ("HELO "&localname&"{ENTER}")
wscript.sleep(2000)
sendkeys ("MAIL FROM:<"&emailadr&">{ENTER}")
wscript.sleep(1000)
sendkeys ("RCPT TO:<"&emailadr&">{ENTER}")
wscript.sleep(1000)
sendkeys ("DATA{ENTER}")
wscript.sleep(1000)
sendkeys ("FROM:<"&emailadr&">{ENTER}")
wscript.sleep(100)
sendkeys ("TO:<"&emailadr&">{ENTER}")
wscript.sleep(100)
sendkeys ("Date:16-05-2010{ENTER}")
wscript.sleep(100)
sendkeys ("{ENTER}")
wscript.sleep(100)
sendkeys ("Hello World{ENTER}")
wscript.sleep(100)
sendkeys (".{ENTER}")
function sendkeys (strkeys)
on error resume next
' BACKSPACE {BACKSPACE}, {BS}, or {BKSP}
' BREAK {BREAK}
' CAPS LOCK {CAPSLOCK}
' DEL or DELETE {DELETE} or {DEL}
' DOWN ARROW {DOWN}
' END {END}
' ENTER {ENTER} or ~
' ESC {ESC}
' HELP {HELP}
' HOME {HOME}
' INS or INSERT {INSERT} or {INS}
' LEFT ARROW {LEFT}
' NUM LOCK {NUMLOCK}
' PAGE DOWN {PGDN}
' PAGE UP {PGUP}
' PRINT SCREEN {PRTSC}
' RIGHT ARROW {RIGHT}
' SCROLL LOCK {SCROLLLOCK}
' TAB {TAB}
' UP ARROW {UP}
' F1 {F1}
shell.sendkeys(strKeys)
if err.number <> 0 then debug "Failed to sendkeys """ & strkeys &""""
on error goto 0
end function

how to echo a command to terminal in vbs

I am using SecureCRT and want to run a vbs script while I am connected in a session, I want to get a site name from user by prompt and then use this variable inside a specific command (pmxh is a session specific command) to send this command to the session, here is my code but I don't know why my echo is not working and returning error (i just want to send pmxh command to the terminal that I have an already open session in it)
Sub Main()
' Prompt the end user for data
strAnswer = InputBox("Please enter site Name:")
' Check to see if the user provided any data, or canceled.
If strAnswer = "" Then
MsgBox "Canceled."
Exit Sub
End If
wscript.echo("pmxh strAnswer -m 0.25 -a pmTotNoRrcConnectReq")
End Sub
I believe you are looking for the crt.Screen.Send command.
crt.Screen.Synchronous = True
' This automatically generated script may need to be
' edited in order to work correctly.
Sub Main()
crt.Screen.Send "cd vshell" & chr(9) & chr(13)
crt.Screen.WaitForString "$ "
crt.Screen.Send "ls" & chr(13)
crt.Screen.WaitForString "$ "
crt.Screen.Send "rpm -U vshell-3.5.0-3" & chr(9) & chr(13)
crt.Screen.WaitForString "$ "
crt.Screen.Send "telnet localhost 22" & chr(13)
crt.Screen.Send chr(13)
End Sub
Read more on sending commands to the terminal -> here (page 10)

Call command program that requires user input

Using VBScript (run through cscript), how can I call another command program (pscp in this case), which also requires user input, etc.?
The purpose is that the initial VBScript will gather the parameters (password, user, etc.), then the pscp command
pscp -r -pw password copyFromPath user#host:copyToPath
can be issued and the user will be able to see the output from the pscp command, as well as being able to input (if, for example, they gave the wrong password and are required to input it again).
I currently have:
' create the command, that calls pscp from cmd
Dim comSpec : comSpec = objShell.ExpandEnvironmentStrings("%comspec%")
Dim command : command = comspec & " /C " & """" & "pscp -r -pw " & "^""" & (Replace(pscpPassword,"""","\^""")) & "^"" " _
& "^""" & (windowsPath) & "^"" " _
& pscpUser & "#" & pscpHostName & ":" & Replace(linuxPath," ","\ ") & """"
Dim objExec : Set objExec = objShell.Exec(command)
An alternative I came up with for generating command was:
Dim command : command = "pscp -r -pw " & Chr(34) & Replace(pscpPassword,"""","\""") & Chr(34) & " " _
& Chr(34) & windowsPath & Chr(34) & " " _
& pscpUser & "#" & pscpHostName & ":" & Replace(linuxPath," ","\ ")
But neither of these allow me to interact with pscp once it's called.
Edit
The fact that I'm calling pscp is almost irrelevant. I could be calling any program which asks for user input and displays things to stdout and stderr
I put all the commands together into a script (separated with &), then run them all after the loop. I append "& pause" to keep the window open (see comments under question for full details)

Resources