i need an example script or bat file where by i want to do the following :>
test the telnet connection and ports to 3 different machines and ports at the same time.
the machine is very locked down, so unable to install putty or any other 3rd party apps or have web access.
I just need the script to telnet to the 1st box and then move to the next.
i have tried the script below but it just stops on the 1st box
<job>
<script language="VBScript">
Option Explicit
On Error Resume Next
Dim WshShell
set WshShell=CreateObject("WScript.Shell")
WshShell.run "cmd.exe"
WScript.Sleep 1000
WshShell.SendKeys "telnet x.x.x.x 7005"
WshShell.SendKeys ("{Enter}")
WshShell.SendKeys "exit"
WshShell.SendKeys ("{Enter}")
WshShell.SendKeys "telnet x.x.x.x 8600"
WshShell.SendKeys ("{Enter}")
WshShell.SendKeys "exit"
WshShell.SendKeys ("{Enter}")
WScript.Quit
</script>
</job>
hope someone can shed some light on this.
Many thanks
Marzanur
There are several issues with this script:
You need to seperatate the
Telnet *.*.*.* 7005
Commands with
wscript.sleep 1000
So that the computer has time to connect. Secondly. Telnet opens in a unique command line interface and "exit" is not an accepted by the telnet interface. However, "quit" is used.
So to replace:
Wshshell.sendkeys "exit"
With
Wshshell.sendkeys "quit"
'Wait for close
Wscript.sleep 500
Telnet commands -> http://technet.microsoft.com/en-us/library/c.aspx
WshShell.SendKeys "telnet x.x.x.x 7005"
WshShell.SendKeys "{Enter}"
WshShell.SendKeys "^]"
WshShell.SendKeys "quit{Enter}"
CTRL + ] jumps out of your telnet session and to the terminal console where you can type quit.
Related
My script launches a MS Edge window and then tries to log into Netflix with my credentials. The problem is, at times, the browser window comes up behind the CMD console and the script doesn't work.
How do I launch the browser in the foreground or bring it to the foreground after it's launched?
<!-- :
rem #Echo Off
Set "usr_name=#####"
Set "usr_pass=#####"
%SystemRoot%\System32\cscript.exe //NoLogo "%~f0?.wsf" "%browser%" "%usr_name%" "%usr_pass%"
Exit /B
-->
<Job>
<Script Language="VBScript">
Dim ObjShell, MyChrome
Set ObjShell = WScript.CreateObject("WScript.Shell")
MyChrome = Chr(34) & WScript.Arguments(0) & Chr(34)
ObjShell.Run MyChrome & "--app=""https://www.netflix.com/login""", 1
WScript.Sleep 5000
ObjShell.SendKeys "{TAB}"
ObjShell.SendKeys "{TAB}"
ObjShell.SendKeys(WScript.Arguments(1))
ObjShell.SendKeys "{TAB}"
ObjShell.SendKeys(WScript.Arguments(2))
ObjShell.SendKeys "{ENTER}"
WScript.Sleep 6000
ObjShell.SendKeys "{ESCAPE}"
ObjShell.SendKeys "{TAB}"
WScript.Sleep 1000
ObjShell.SendKeys "{TAB}"
WScript.Sleep 1000
ObjShell.SendKeys "{ENTER}"
WScript.Sleep 6000
ObjShell.SendKeys "%{F4}"
</Script>
</Job>
Scripts that depend on SendKeys are notoriously unreliable. A more reliable approach is to control the browser with Seleniumbasic and Chrome driver or use WebView2 in a C# program. Barring that, one option for adding window control functionality to your existing script is to use the command line tool Cmdow. For example:
ObjShell.Run "cmdow ""Netflix*"" /MAX",1,False
Update:
You can also try the built-in AppActivate method:
ObjShell.AppActivate "Netflix"
The solution to to use a powershell cmdlet to minimize all windows before running the script that launches the browser.
'''powershell -command "(new-object -com shell.application).minimizeall()'''
I am trying to automate the Group Policy editing process as much as possible.
I have the following script to spawn the gpedit.msc process but it's window goes out of focus as soon as it opens:
FINDSTR /E "'VbsCode" %~f0 > %temp%\~temp.vbs
CSCRIPT //NOLOGO %temp%\~temp.vbs
Sub GPEditOptions 'VbsCode
On Error Resume Next 'VbsCode
Set WshShell = WScript.CreateObject("WScript.shell") 'VbsCode
WshShell.Visible = False 'VbsCode
WshShell.Run "gpedit.msc",0 'VbsCode
:: WshShell.AppActivate "Local Group Policy Editor" 'VbsCode
End Sub 'VbsCode
GPEditOptions 'VbsCode
:: WScript.Quit 0 'VbsCode
How can I AppActivate the window that has been opened by the newly spawned gpedit.msc process ? Specifically how to know what's the name/title of that window that has been opened ? "Local Group...Editor" doesn't work.
I think I eventually figured out, how to approach this problem. There are different types of processes.
In this case, I first need to select the Microsoft Management Console window, since it's the parent process which spawns the actual "Local Group Policy Editor" child process.
So this code does the job of selecting the first Windows Component starting with letter "W" by sending a ton of keys priorly, and yes you do need Administrator elevation for proper selection of options in the gpedit.msc window:
#echo off
net file 1>nul 2>nul
if not '%errorlevel%' == '0' (
powershell Start-Process -FilePath "%0" -ArgumentList "%cd%" -verb runas >nul 2>&1
exit /b
)
cd /d %1
FINDSTR /E "'VbsCode" %~f0 > %temp%\~temp.vbs
CSCRIPT //NOLOGO %temp%\~temp.vbs
Sub GPEditOptions 'VbsCode
On Error Resume Next 'VbsCode
Set WshShell = WScript.CreateObject("WScript.shell") 'VbsCode
WshShell.Visible = False 'VbsCode
WshShell.Run "gpedit.msc",0 'VbsCode
WScript.Sleep 500 : WshShell.AppActivate "Microsoft Management Console" 'VbsCode
WScript.Sleep 500 : WshShell.AppActivate "Local Group Policy Editor" 'VbsCode
WScript.Sleep 500 : WshShell.sendKeys "% x{TAB}{ENTER}" 'VbsCode
WScript.Sleep 500 : WshShell.sendKeys "{TAB}{TAB}{TAB}{TAB}" 'VbsCode
WScript.Sleep 500 : WshShell.sendKeys "{DOWN}{DOWN}{ENTER}" 'VbsCode
WScript.Sleep 500 : WshShell.sendKeys "{TAB}{TAB}{TAB}{TAB}" 'VbsCode
WScript.Sleep 500 : WshShell.sendKeys "{DOWN}{DOWN}{DOWN}{DOWN}" 'VbsCode
WScript.Sleep 500 : WshShell.sendKeys "{DOWN}{DOWN}{ENTER}" 'VbsCode
WScript.Sleep 500 : WshShell.sendKeys "{TAB}{TAB}{TAB}{TAB}{W}" 'VbsCode
End Sub 'VbsCode
GPEditOptions 'VbsCode
WScript.Quit 0 'VbsCode
Hope this helps anyone facing similar issue.
There is no need of sending bunch of keys and getting the Group policy editor window title.
Actually every group policy setting has equivalent registry keys. And registry can be edited easily from VBScript. To find the equivalent registry key for the group policy setting:
Download a tool named Process Monitor from SysInternals.
Run it and click Filter > Filter.
Now create two filters like: "Process name" - "is" - "mmc.exe" - then "include" and "Operation" - "is" - "RegSetValue" - then "Include".
Now edit the group policy setting and the registry key will appear in Process monitor.
And Function to edit registry in VBScript:
Function RegSetValue(regkey,value)
Dim WshShell
Set WshShell = CreateObject("Wscript.Shell")
WshShell.RegWrite(regkey,value)
End Function
Brief explanation: trying to use sendkeys to type two variables/keys from the output of a batch file.
I keep getting syntax or out of subscript errors.
Two variables are passed from a batch script, via:
cscript //nologo sendKeys.vbs !config! !arguments!
rem Yes, both variables are defined, and delayed expansion in enabled.
My attempts to put them in sendkeys doesn't seem to work. I'm not familiar with VBS...
VBScript:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad.exe", 9 ' Start notepad in order to test my failure.
WScript.Sleep 500 ' Give notepad time to load
WshShell.SendKeys & WshShell.Arguments(0) ' write passed !config! variable
WshShell.SendKeys "{TAB}" ' tab key
WshShell.SendKeys & WshShell.Arguments(1) ' write passed !arguments! variable
WshShell.SendKeys "{ENTER}" ' enter key
The .Arguments belong to the WScript object, not the Shell. & is the concatenation operator, it makes no sense between a method name and the first/only argument. So use
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad.exe", 9 ' Start notepad in order to test my failure.
WScript.Sleep 500 ' Give notepad time to load
WshShell.SendKeys WScript.Arguments(0) ' write passed !config! variable
WshShell.SendKeys "{TAB}" ' tab key
WshShell.SendKeys WScript.Arguments(1) ' write passed !arguments! variable
WshShell.SendKeys "{ENTER}" ' enter key
or - better - think about a way to solve your real world problem without SendKeys.
I have batch file which gets user input, tranfers the input to a vb script.
set /p "whichPort= Which port do you want to reset:"
start /b "" cscript.exe //NoLogo loginInXyplex.vbs /portname:"%whichPort%"
Then a telnet session is exceuted from within the batch file, followed by the exection of the same script file in order to issue commands in the telnet session.
telnet.exe 192.120.187.35 2000
REM run the script
cscript loginInXyplex.vbs
When in the telnet session it appears the two scripts are running at the same time as the conmmands are out of synch so cannot log on properly
This is my first vb script so I could be missing somethong obivious, so i have included all source code :
Batch File:
#echo off
cls
echo.
echo Welcome to Xyplex Server Port Reset
echo.
pause
echo.
set /p whichXplex= Which Xyplex server is your device connected too: 1 or 2 ?
echo.
set /p "whichPort= Which port do you want to reset:"
echo Sending port "%whichPort%" to script file
echo.
pause
rem send using strat /b so we open script file on same command line window
start /b "" cscript.exe //NoLogo loginInXyplex.vbs /portname:"%whichPort%"
echo.
echo Conencting to Xyplex "%whichXplex%"
pause
if "%whichXyplex%" == "1" (
REM COnnecting to xyplex one IP:Socket
echo.
echo Connecting to Xpyplex %whichXyplex%...
telnet.exe 192.120.187.35 2000
REM run the script
cscript loginInXyplex.vbs
) ELSE (
rem ^ missing spaces in )ELSE(
echo Conencting to xyplex %whichXyplex%....
telnet.exe 193.120.187.245 2000
REM run the script in same widow wuth start /b
cscript loginInXyplex.vbs
)
Script:
set OBJECT=WScript.CreateObject("WScript.Shell")
OBJECT.SendKeys"{ENTER}"
OBJECT.SendKeys "access{ENTER}"
WScript.sleep 50
OBJECT.SendKeys "access{ENTER}"
OBJECT.SendKeys "SET PRIV{ENTER}"
OBJECT.SendKeys "system{ENTER}"
OBJECT.SendKeys "sh po all{ENTER}"
OBJECT.SendKeys "{ENTER}"
' source heer: http://stackoverflow.com/questions/21013428/pass-variable-from-batch-to-vbs
port = WScript.Arguments.Named.Item("portname")
OBJECT.SendKeys port
OBJECT.SendKeys "Port number selected is: " & port
OBJECT.SendKeys "{ENTER}"
OBJECT.SendKeys "Port reset....logging off...{ENTER}"
OBJECT.SendKeys "SET NOPRIV {ENTER}"
Wscript.Sleep 1000
OBJECT.SendKeys "Quit"
Any help appreciated.
I have equipment that comes with a default ip address that I need to change via telnet.
The new ip will always be the same except for the last two octets.
I currently access the equipment with a batch file that call on a .vbs
#ECHO OFF
:: Get Hub IP
set /p input1="Enter Hub and press ENTER "
set /p input2="Enter Shelf and press ENTER "
#ECHO OFF
::Run script to Set IP Address
start telnet.exe 10.230.%input1%.%input2%
cscript SetIPAddress.vbs .
echo "DONE!"
pause
HERE IS THE VBS
set OBJECT=WScript.CreateObject("WScript.Shell")
WScript.sleep 1000
OBJECT.SendKeys "admin{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "admin{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "configure{ENTER}"
WScript.sleep 2000
This works fine
What I need is to add something like this
#ECHO OFF
SET /p paramA=Hub:
SET /p paramB=Shelf:
ECHO set interfaces eth0 10.230.%paramA%.254 mask 255.255.255.0 gateway 10.230.%paramA%.254
PAUSE
This also works fine but I’m not sure how to get it to send to the command prompt.
Can it be added to the .vbs or can the elements of the .vbs be added to the batch file?