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)
Related
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.
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
I am using VBS to
Upload a file to FTP
Verify the upload process
I am using the method which creates a text file, fills it with the appropriate command and then execute it using ftp.exe in windows.
FTPCommand = "%systemroot%\System32\ftp.exe -s:session.txt"
FTPCommand = objshell.ExpandEnvironmentStrings(FTPCommand)
objshell.Run FTPCommand,, vbTrue
fso.DeleteFile "session.txt", vbTrue
Part 1 is done using this code:
Set SessionFile = fso.OpenTextFile("session.txt", 2, vbTrue)
With SessionFile
.WriteLine "open abcd.com"
.WriteLine "username"
.WriteLine "pwd"
.WriteLine "cd /Test/Test1"
.WriteLine "put """ & File.Path & """"
.WriteLine "quit"
.Close
End With
FTPCommand = "%systemroot%\System32\ftp.exe -s:session.txt"
FTPCommand = objshell.ExpandEnvironmentStrings(FTPCommand)
objshell.Run FTPCommand,, vbTrue
fso.DeleteFile "session.txt", vbTrue
And Part 2 is done using this code:
Set SessionFile = fso.OpenTextFile("session.txt", 2, vbTrue)
With SessionFile
.WriteLine "open abcd.com"
.WriteLine "username"
.WriteLine "pwd"
.WriteLine "cd /Test/Test1"
.WriteLine "ls"
.WriteLine "close"
.WriteLine "bye"
.Close
End With
FTPCommand = "%systemroot%\System32\ftp.exe -s:session.txt"
FTPCommand = objshell.ExpandEnvironmentStrings(FTPCommand)
set ObjExec=objshell.exec(FTPCommand)
DO WHILE ObjExec.status=0 : wscript.sleep 50 : LOOP
StrTemp=ObjExec.stdout.readall
IF instr(1,StrTemp,File.Name,1)<>0 THEN
AlertMessage = AlertMessage & vbTab & "STATUS: UPLOAD SUCCESSFUL" & vbCrLf & vbCrLf
ELSE
AlertMessage = AlertMessage & vbTab & "STATUS: UPLOAD FAILED" & vbCrLf & vbCrLf
END IF
fso.DeleteFile "session.txt", vbTrue
The problem is that (in part 2 code) the code after
DO WHILE ObjExec.status=0 : wscript.sleep 50 : LOOP
never returns.So the file gets uploaded but the code to check the status never returns.
The session.txt file does not get delete and when I execute the command
%systemroot%\System32\ftp.exe -s:session.txt
manually it indeed shows me the list of files (because of ls command).
I have 3 questions:
Why it does not return. Where to start debugging from?
Is there anyway I can upload the file and check its status(maybe by
the error code returned by the ftp command after the "put" command).
Is there is an incorrect directory specified in the code to upload
file, the cd command fails and it incorrectly "puts" the file in the
root folder. same goes for the code checking the file upload. So
even if the directory specified in wrong, the program returns it as
successful
Edit 1:
I tried it using
.WriteLine "cd /Test"
and it worked. Is that directory switching (two folders deep)causing the problem ?
Edit 2:
I ran the ls command manually and it ran fine. The output is:
226 Transfer complete.
ftp: 586493 bytes received in 4.28Seconds 137.00Kbytes/sec.
Is 586493 bytes too much for this ?
I believe the problem may be:
1)The large no of files returned by LS command.
2)The directory structure I am accessing.
Edit:3
From this microsoft website it looks like the above point 1 is the culprit:
A console application's StdOut and StdErr streams share the same
internal 4KB buffer. In addition, the WshScriptExec object only
provides synchronous read operations on these streams. Synchronous
read operations introduce a dependency between the calling script
reading from these streams and the child process writing to those
streams, which can result in deadlock conditions.
I believe the problem is the way you read the stdout from the process. I use this technique succesfully as follows, sorry, I have no time to adapt your script and try it out, so that is up to you.
The following executes a ping to check if the server is online.
Function IsOnline(Address)
Dim strText
IsOnline = False
Set oExecObj = oShell.Exec("%comspec% /c ping -a -n 1 -w 20 " & Address)
Do While Not oExecObj.StdOut.AtEndOfStream
strText = oExecObj.StdOut.ReadAll()
If Instr(strText, "Reply from") > 0 Then
IsOnline = True
Exit Function
End If
Loop
End Function
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.
I need to comeup with a vbscript to grab a file from an ftp site where the file name is in the form "vendor(date)(date)(random#).zip" . These files are updated daily so I need a regex or way to select the newest file on the server and download it. I know how to handle this on the local file system, but I don't know how to determine which file to get on a remote ftp server.
Funny you posted this as I just recently had to knock out a script to do almost exactly word for word what you're asking for. Basically, all you really need to do is to have your script create an FTP command file, then call it.
Use your method of choice to create a string to hold the date in whatever format you are looking for, I called it strDate and in my case it ended up being this syntax: headerinfo.13September10 Where headerinfo is a standard file header with a number attached to it.
Write out an FTP command file:
Dim objOutStream
Set objOutStream = objFSO.OpenTextFile(strCommandFile, ForWriting, True, TristateFalse)
With objOutStream
.WriteLine "USER xxxxxx" ' USERNAME
.WriteLine "xxxxxxftp" ' Password
.WriteLine "binary"
.WriteLine "prompt n"
.WriteLine "lcd " & strNetmonData ' FOLDER I'm changing into
.WriteLine "mget *." & strDate ' Get all files with today's date in it
.WriteLine "bye"
.Close
End With
Then later in your script you just call it:
WSHShell.Run "%comspec% /c FTP -n -s:" & strCommandFile & " " & strSite, 0, True
Where strSite is the IP or name of the site you are trying to connect to.
The following code will help you to get the latest file name from ftp server after which you can download it.
Const ForWriting = 2
Dim objOutStream, objjFSO, objShell
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutStream = objFSO.OpenTextFile("C:\temp\temp\empty.txt", ForWriting,True)
With objOutStream
.WriteLine sUsername ' USERNAME
.WriteLine sPassword ' Password
.WriteLine "cd /"& sRemotePath' FOLDER I'm changing into
.WriteLine "ls -rt tmp/listing.txt"
.WriteLine "quit"
.Close
End With
Set objShell = CreateObject("WScript.Shell")
objShell.Run "%Comspec% /c FTP -i -s:" & "C:\temp\temp\empty.txt" & " " & sSite
wait(2)
Set strCommand = objShell.Exec ("%Comspec% /c head -1 tmp\listing.txt")
Set objStdOut = strCommand.StdOut
strFilename = objStdOut.ReadLine