Below is code that works to a point. It utilizes the new Win 10 command ssh to log into a server, request a password to be entered, run a linux command, and exit. For any testers I've made the cmd window unhidden so you can see it works if you test it on your own server, with the exception of course of the linux command I'm running. That you would need to change to whatever you want.
Code: Select all
InputBox, server, Server Information, Enter either the server name or IP address
InputBox, username, Username, Enter your username
InputBox, password, Enter Password, (your input will be hidden), hide
sshcommand = ssh %username%@%server%
Run, %comspec% /k %sshcommand%,,Show, sshpid
IfWinExist, ahk_pid %sshpid%, , ,
{
ConsoleSend(password . "`r","ahk_pid " . sshpid)
}
sleep, 5000
;command below will output to command window but not to a variable or a msgbox.
ConsoleSend("dash retrieval ""General='Folder' and (start>now() OR last>now()) and (start<'2020-06-01' OR last<'2020-06-01')""`r", "ahk_pid " . sshpid)
sleep, 3000
ConsoleSend("exit`r")
Sleep, 2000
ConsoleSend("exit`r")
ExitApp
; Sends text to a console's input stream. WinTitle may specify any window in
; the target process. Since each process may be attached to only one console,
; ConsoleSend fails if the script is already attached to a console.
;Created by lexikos at the link below
;https://autohotkey.com/board/topic/25446-consolesend/
ConsoleSend(text, WinTitle="", WinText="", ExcludeTitle="", ExcludeText="")
{
WinGet, pid, PID, %WinTitle%, %WinText%, %ExcludeTitle%, %ExcludeText%
if !pid
return false, ErrorLevel:="window"
; Attach to the console belonging to %WinTitle%'s process.
if !DllCall("AttachConsole", "uint", pid)
return false, ErrorLevel:="AttachConsole"
hConIn := DllCall("CreateFile", "str", "CONIN$", "uint", 0xC0000000
, "uint", 0x3, "uint", 0, "uint", 0x3, "uint", 0, "uint", 0)
if hConIn = -1
return false, ErrorLevel:="CreateFile"
VarSetCapacity(ir, 24, 0) ; ir := new INPUT_RECORD
NumPut(1, ir, 0, "UShort") ; ir.EventType := KEY_EVENT
NumPut(1, ir, 8, "UShort") ; ir.KeyEvent.wRepeatCount := 1
; wVirtualKeyCode, wVirtualScanCode and dwControlKeyState are not needed,
; so are left at the default value of zero.
Loop, Parse, text ; for each character in text
{
NumPut(Asc(A_LoopField), ir, 14, "UShort")
NumPut(true, ir, 4, "Int") ; ir.KeyEvent.bKeyDown := true
gosub ConsoleSendWrite
NumPut(false, ir, 4, "Int") ; ir.KeyEvent.bKeyDown := false
gosub ConsoleSendWrite
}
gosub ConsoleSendCleanup
return true
ConsoleSendWrite:
if ! DllCall("WriteConsoleInput", "uint", hconin, "uint", &ir, "uint", 1, "uint*", 0)
{
gosub ConsoleSendCleanup
return false, ErrorLevel:="WriteConsoleInput"
}
return
ConsoleSendCleanup:
if (hConIn!="" && hConIn!=-1)
DllCall("CloseHandle", "uint", hConIn)
; Detach from %WinTitle%'s console.
DllCall("FreeConsole")
return
}
In the past I've worked around this problem by using plink to log into the server using a bat script or ahk however the problem with plink is that some of the linux command lines are heavy with parenthesis and double and single quotes, and so they often fail because there are too many characters to escape and it's difficult to tell which characters are causing the problems, especially on the cmd line. As you can see from the command I've got in my code it's a lot of parenthesis and double and single quotes.
Code: Select all
"dash retrieval ""General='Folder' and (start>now() OR last>now()) and (start<'2020-06-01' OR last<'2020-06-01')""`r"
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
InputBox, server, Server Information, Enter either the server name or IP address
InputBox, username, Username, Enter your username
InputBox, password, Enter Password, (your input will be hidden), hide
sshcommand = ssh %username%@%server%
querycommand := "dash retrieval ""General='Folder' and (start>now() OR last>now()) and (start<'2020-06-01' OR last<'2020-06-01')""`r"
MsgBox % RunWaitMany(sshcommand, password, querycommand)
RunWaitMany(initialcommand, pass, command){
shell := ComObjCreate("WScript.Shell")
; Open cmd.exe with echoing of commands disabled
exec := shell.Exec(ComSpec " /Q /K " initialcommand)
; Send the commands to execute, separated by newline
sleep 5000
;exec.StdIn.WriteLine(pass "`nexit")
;wait for server to accept credentials
sleep, 3000
exec.StdIn.WriteLine(command "`nexit")
; Read and return the output of all commands
exec.StdIn.WriteLine("exit`r")
return exec.StdOut.ReadAll()
}
Thanks in advance for all your help! xoxo jules