I'm using a VBScript to SendKeys to a Cisco Client because it prompts for a username and password. However, I am having problems getting the command prompt focused.
I added AppActivate before every SendKeys command, but normal computer usage often breaks the focus in the time between these commands.
How can I ensure the command prompt has focus before sending keys?
Dim host, username, password, pathToClient
host = "host"
username = "username"
password = "password"
pathToClient = "C:\Program Files {(}x86{)}\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe"
Set ws = WScript.CreateObject("WScript.Shell")
ws.run("TASKKILL.exe /F /IM vpnui.exe"), 0, false
ws.run("cmd.exe"), 2, false
WScript.Sleep 300
ws.AppActivate("Command Prompt")
ws.SendKeys """" & pathToClient & """ connect " & host & "~"
WScript.Sleep 1000
ws.AppActivate("Command Prompt")
ws.SendKeys(username & "~")
WScript.Sleep 50
ws.AppActivate("Command Prompt")
ws.SendKeys(password & "~")
ws.run("TASKKILL.exe /F /IM cmd.exe"), 0, false
With VBScript you can't. AppActivate is the only method available in VBScript to place the focus on a particular application, and it doesn't ensure the focus stays there. That's the exact reason why SendKeys shouldn't be used for automation anyway. Try something like AutoIt instead.
Or might setting Auto Connect on Start be an option, since you're apparently using AnyConnect?
Auto Connect on Start—AnyConnect, when started, automatically establishes a VPN connection with the secure gateway specified by the AnyConnect profile, or to the last gateway to which the client connected.
I used AutoHotKey to solve my issue. Here's my code:
host := "my.host.domain"
username := "myUsername"
password := "myPassword"
pathToClient = "C:\...\vpncli.exe"
DetectHiddenWindows, on
Process, Close, vpnui.exe
run, cmd.exe,, Hide
Sleep 100
ControlSend,, %pathToClient% connect %host%{Enter}, ahk_exe cmd.exe
Sleep 1000
ControlSend,, %username%{Enter}, ahk_exe cmd.exe
Sleep 50
ControlSend,, %password%{Enter}, ahk_exe cmd.exe
;Process, Close, cmd.exe
Related
Consider:
Dim WSHShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "MSTSC /v:servername"
WScript.Quit
I am able to open the RDP popup and give a server name to connect to. I need to provide the username, password, and click OK. Is there a way to achieve this from VBScript?
You can use the following code:
Dim objShell, strMachineName, strUserName, strUserPwd
set objShell = createObject("wscript.shell")
strMachineName = "enter-machine-name"
strUserName = "enter-your-user-name"
strUserPwd = "enter-user-password"
objShell.Run "cmdkey /generic:"&strMachineName&" /user:"&strUserName&" /pass:"&strUserPwd
objShell.run "mstsc /v: "&strMachineName
set objShell = Nothing
Reference on cmdkey
I have tested this on Windows 7 and it works.
I can suggest two options.
1: You can save an RDP connection (see picture below) and just run the new .rdp file from WshShell. After you create the .rdp file, you will have to log into it the first time, enter your credentials, and check the "Remember My Credentials" option.
2: You could use the SendKeys method. It is ugly but works. The downside is the password is left in the code, so you may want to look into encryption if you go this route. You may also need to tune a wait (sleep) for waiting for the popup to come up.
WScript.Sleep 5000 'Sleeps for 5 seconds
SendKeys “{TAB}”, 1 'Focus to the computer name
SendKeys "ServerName", 1
SendKeys "{TAB}", 1 'Focus to the user name
SendKeys "Password", 1
SendKeys "{ENTER}", 1 'Connect
I created a hyperlink in a pdf, this hyperlink is tied to vbs script, until this step all ok. When I run the script manually (double click), the script does that I want (open telnet connection). The problem is when I run the script through the hyperlink, CMD show the message " "telnet" is not recognized as an internal or external command". Please, Can anyone tell me why occurs that??
This is the script:
Dim WshShell, regexp
set regular = New RegExp
direccion = inputbox("Ingresa ip del equipo:")
' Set pattern.
regular.Pattern = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
' Set case insensitivity.
regular.IgnoreCase = True
' Set global applicability.
regular.Global = True
if regular.test(direccion) = TRUE then
set WshShell=CreateObject("WScript.Shell")
WshShell.run "cmd.exe"
WScript.Sleep 1000
'Send commands to the window as needed - IP and commands need to be customized
'Step 1 - Telnet to remote IP'
WshShell.SendKeys "telnet " & direccion
WshShell.SendKeys ("{Enter}")
WScript.Sleep 1000
else
msgbox "Ingresa una ip válida"
end if
probably working directory not set correctly.
try
WshShell.SendKeys "c:\windows\system32\telnet.exe " & direccion
p.s: you're doing it wrong. "sendkeys" is somehow understandable when telnet terminal is already running, but there is no reason to be manually sending keys to the standard command prompt.
Telnet isn't in my Windows 10 and 8. But anyway if you have "telnet" command couldn't you do:
WshShell.run "cmd.exe /k telnet"
That should work and saves space.
I am working on a VBScript where it activates an application, sends a password to that application and minimizes the application. My script fails sending the keys. The application does not get the password, but when I double-click on the script it receives the password and minimizes. I don't know where the error is.
It's called like this:
InitliszeUSb.bat:
pause
START %myDrive%"RunSanDiskSecureAccess_Win.exe"
pushd %~dp0
ping 10.10.10.10 -n 1 -w 10000 >nul
start /b "" cscript "D:\min.vbs"
Min.vbs:
Option Explicit
Dim oSHL : Set oSHL = CreateObject("WScript.Shell")
oSHL.AppActivate "SanDisk SecureAccess"
oSHL.SendKeys "pass1_word~" 'Enters Password
WScript.Sleep(3000)
oSHL.SendKeys "% n" 'Minimises the window
WScript.Quit
In Windows 7, I experienced issues when using SendKeys to send keys to other apps (the way like you do), when SendKeys is not running in elevated process. Sometimes the macro/script works 100%, sometimes it looks like keystrokes are getting lost. The only way around I found for that was running sender application (or script) elevated (i.e. "As Administrator").
First of all, thanks for reading.
I have a HTA to centralize some repetitive task.
Login into several servers via ssh and send multiple commands is one of them.
This code is working like a charm inside a vbs file
Option Explicit
Dim Shell, WMI, wql, process
Set Shell = CreateObject("WScript.Shell")
Set WMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
wql = "SELECT ProcessId FROM Win32_Process WHERE Name = 'putty.exe'"
dim cmd : cmd=InputBox("Enter command")
For Each process In WMI.ExecQuery(wql)
Shell.AppActivate process.ProcessId
Shell.SendKeys cmd & " {ENTER}"
Next
But this equivalent, inside a HTA only sends the command to one or two windows.
sub sendToPuttyWindow(cmd)
Dim Shell, WMI, wql, process
Set Shell = CreateObject("WScript.Shell")
Set WMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
wql = "SELECT ProcessId FROM Win32_Process WHERE Name = 'putty.exe'"
For Each process In WMI.ExecQuery(wql)
Shell.AppActivate process.ProcessId
Shell.SendKeys cmd & " {ENTER}"
Next
end sub
Currently I'm calling the vbs file from the hta, but I would like to maintain the HTA file as independent as possible.
Could you please help me?
Do not use SendKeys for automating PuTTY. It sends all emulated keystrokes to the current foreground window, whatever that may be. If you need to run several commands in a row: use plink from the PuTTY suite. It was built for this exact purpose.
plink -ssh -batch -m file user#host
I am logging into the server as Administrator to winserver2008.
I created a script called: vbscript.vbs
The purpose of this script is to auto login to linux via putty, then perform command line task.
Dim Shell
Set Shell = CreateObject("WScript.Shell")
output = Shell.Run("C:\putty.exe 1.2.3.4 9321")
wscript.sleep(500)
Shell.Sendkeys "root" & VBCrLf
wscript.sleep(30)
Shell.Sendkeys "password" & VBCrLf
wscript.sleep(30)
When I manually click on vbscript.vbs to execute it, vbscript will fill in root and password to putty.
When I use windows scheduler call to vbscript.vbs to execute it, vbscript won't fill in root and password to putty.
I suspect some permission issue.
I already set putty.exe to run as administrator, allow administrator, administrators group permission for it, but still fail to work when call via windows scheduler.
=====
I just tried with the second scenario, send 2 to the windows calculator, fail too..
testcalc.vbs
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "Calc.exe"
objShell.AppActivate "Calculator"
objShell.SendKeys "2"
Give up on trying to get SendKeys to work from a scheduled task, it's not going to happen. Instead simply pass the login and password on the command line:
output = Shell.Run("C:\putty.exe -l root -pw password 1.2.3.4 9321")
Alternatively do it with a session file and use -load.
If you are then going to execute commands over this connection then I believe you actually want plink rather than putty.