Coder Social home page Coder Social logo

hymkor / expect Goto Github PK

View Code? Open in Web Editor NEW
108.0 1.0 14.0 164 KB

Expect-lua for Windows

License: MIT License

Go 80.70% Batchfile 0.23% Lua 13.29% Common Lisp 2.57% Makefile 3.21%
go windows lua commandprompt gopher-lua golang golang-tools golang-application

expect's Introduction

hymkor (HAYAMA_Kaoru)

Github

  • A Go/Lua/.NET/C++/Oracle/DWG-CAD programmer

Products

On Windows, some tools are able to be installed with scoop-installer. ( See also my scoop bucket )

$ scoop bucket add hymkor https://github.com/hymkor/scoop-bucket
$ scoop install NAME

expect's People

Contributors

a690700752 avatar hymkor avatar trackpadpro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

expect's Issues

Any other way of simulating the Enter key?

I'm trying to use this with Bitvise's stnlc ssh client, but \r and \n are just counted as regular characters and not as pressing the Enter key.
Are there any other ways of confirming your input?

scp and ssh seems to impact earlier sendln commands

Hi,
Thanks for the tool.
I have found a workaround but thought best to check if this is expected behaviour

I am trying to script a number of actions but have a problem where all actions are in the same .lua script.
The script will be used to update an Ubuntu server(s) with some configuration from windows10
the script takes two arguments IP and Password and arg[] is used within the .lua
using sendln the script clears/updates the Known_hosts and remove files from a windows directory.
testing to this point works fine where no additional code exists

When I start adding the scp via the spawn commands the ALL sendln commands previously working stop working and as if they are getting bypassed

the script needs to execute mutiple scp and ssh command .

I tried adding a wait/sleep with no sucess

Splitting the .lua to do the clearing/update and then using sendln to execute a separate .lua to do the scp and ssh commands seems to have worked with the number of scp and ssh call so far with only issue having

the attached is the script in one lua file where all the three sendln do not execute

Thanks

lua sample

loop condition for expect

After a few certain expections, I need conditional expect for send something. But I could not deal with it.

Also is it possible to read console output?

Provide installers for package managers such as Scoop and Chocolatey

It would be nice to be able to automatically install and uninstall Expect using a preferred package manager, such as Scoop or Chocolatey. Information regarding contributing to these:

I don't know if Expect can run as a self-contained executable or not, but there are options (at least with Scoop, there are) for installing system-wide to the C:\ drive, if that's a requirement.

runtime error: makeslice: len out of range

runtime error: makeslice: len out of range
stack traceback:
        [G]: in function 'expect'
        C:\Users\hymko\Share\bin\alfatech.cmd:27: in main chunk
        [G]: ?
  • Console width = 82 , heiht=9001

Support to run as administrator

When using this script to run my binary/exe that asks for series of questions, and need administrator privileges, is it possible to execute this without prompting the user for admin privilege? Can this silently run everything as admin?

Redirect output?

I would like to use this program to logon run a remote command and logout the remote host. It does this very well, thank you! But I would also like to redirect the output/stout to a file. When I try this I get invalid handle. If there a way to workaround this issue in a batch file?

Help me ssh Auto input password with Expect

I found Expect.exe for my Windows, but not found Spawn.exe Send.exe
I want auto enter password with batch, script:

setLocal EnableDelayedExpansion
@expect.exe "%~f0"
@EXIT /b

-- Lines starting with '@' are replaced to '--@' by expect.exe
-- to embed the script into the batchfile.

echo(true)
if spawn([[ssh.exe]],"root@localhost -p 2222") then
expect("password:")
echo(false)
send("alpine\r")
expect("~]$")
echo(true)
send("exit\r")
end

`The handle is invalid.` is output when using in GitHub Actions PowerShell.

Run powershell -ExecutionPolicy ByPass -File .\\scripts\\exampleExpectLua.ps1 -STEAM_LOGIN_COOKIE ***
expect.lua v0.[6](https://github.com/trackpadpro/tppsss/runs/5965160218?check_suite_focus=true#step:6:6).2-6-g5[9](https://github.com/trackpadpro/tppsss/runs/5965160218?check_suite_focus=true#step:6:9)df0cc-windows-amd64
"D:\a\tppsss\tppsss\scripts\exampleExpectLua.ps1\..\exampleExpect.lua\..\..\tppsss.exe"
The handle is invalid.
The handle is invalid.
The handle is invalid.
The handle is invalid.
Unable to find steamLoginSecure. This cookie is used for fetching full item price history.
Would you like to set it? [y/n] Unrecognized command. The steamLoginSecure cookie can be set later using the "setup" command.
Online
Offline

^is the output

echo(true)
if spawn(arg[0]..[[\..\..\tppsss.exe]]) then
    timeout = 2
    RC = expect("[y/n]")
    timeout = 3600
    echo(false)
    if RC==0 then
        send("n\r")
    end
    expect("Online")
    send("setup\r")
    expect("cookie:")
    send(arg[1].."\r")
    expect("steamLoginSecure set")
    send("break\r")
end

^is called by expect.exe $pathToScripts\..\exampleExpect.lua $STEAM_LOGIN_COOKIE

Your program works very well for me in PowerShell on my own desktop, but in MSYS2 and in the GitHub runner PowerShell it outputs this error 4 times. I am not sure if I am simply handling the arguments too poorly. Is PowerShell not similar enough to Command Prompt? I guess I may be able to just build in MSYS and use the other Expect, but I really like using yours on my own computer and wanted to simulate the testing on actions, so I would greatly appreciate it if you could take a look at if I am using your program wrong or if it is easily fixable!

This is kind of similar to #12 maybe. Below are some links that involve what I think possible causes are, but I have never used Go or any of these libraries, so I have trouble understanding if it is indeed the cause in expect.lua.

moby/moby#15272
moby/moby#17738
golang/go#11914

Thanks!

Support to log the output/errors to a file when running lua script

Does this package support a way to log information to a file ?
My CLI app has a series of questions and answers and there are questions for which, if validation fails for the input provided, it asks the same question again after stating that "validation failed". So, my program does not exit in this case, but just reprompts with the same question, till the user has provided a valid answer. If I would like to add all these outputs/errors (like 'validation failed') and also any other information to a log file, is it something I can do from my .lua file? For example, following the readme, lets say I create a sample.lua file and run it against my .exe (Go CLI app) as "expect.exe sample.lua",

echo(true)
if spawn([[C:\test.exe]]) then
expect("Enter username")
echo(true)
send("xxx\r")
....
....
....
end

Lets say xxx was an invalid input and my CLI app prints fmt.print("Invalid value")
and then prompt same question to the user to re-enter the value,
Is there a way I can log this message and infact all the interactions to a file (including all the stdout/fmt I see as output from my CLI app) from this .lua file or something?

Invalid token

Hi When I run expect.exe test.lua, I get the following error:
.\test.lua line:1(column:1) near '?': Invalid token

This is my test.lua, which is exactly your sample.lua

echo(true)
if spawn([[c:\Program Files\Git\usr\bin\ssh.exe]],"[email protected]") then
    expect("password:")
    echo(false)
    send("PASSWORD\r")
    expect("~]$")
    echo(true)
    send("exit\r")
end

Send Arrow Keys

Is it possible to send up/down arrow keys so that the cli getting automated can be navigated by arrow keys?

If so, can you document how this would be done?

issue using "scp" accept only single quoted arguments (instead of double quoted)

I'm trying to automate , secure copy functionality from windows workstation to linux machine, by using your tool, I was suppose to overcome the prompt password barrier. However I immediately had difficulties to insert arguments, since they are not properly interpret by expect.exe. I'm trying to figure out if there is a way, that the tool could interpret the arguments (string) as single quoted or even without (a clean string):

cnf.lua - content:
image

running the tool output:
image

Segfault when using OpenSSH-Win64

Nice idea!

When I try to spawn the Win64 version of https://github.com/PowerShell/Win32-OpenSSH I am running into a crash:

D:\OpenSSH-Win64>expect expect.lua
runtime: VirtualAlloc of 1048576 bytes failed with errno=1455
fatal error: out of memory

runtime stack:
runtime.throw(0x598f21, 0xd)
        c:/go/src/runtime/panic.go:616 +0x88
runtime.sysMap(0xc3e4150000, 0x100000, 0x1, 0x69cd18)
        c:/go/src/runtime/mem_windows.go:122 +0x13b
runtime.(*mheap).sysAlloc(0x6842a0, 0x100000, 0x1f1337d0)
        c:/go/src/runtime/malloc.go:470 +0xdb
runtime.(*mheap).grow(0x6842a0, 0x1, 0x0)
        c:/go/src/runtime/mheap.go:907 +0x67
runtime.(*mheap).allocSpanLocked(0x6842a0, 0x1, 0x69cd28, 0x1f1337d0)
        c:/go/src/runtime/mheap.go:820 +0x308
runtime.(*mheap).alloc_m(0x6842a0, 0x1, 0x10, 0x0)
        c:/go/src/runtime/mheap.go:686 +0x126
runtime.(*mheap).alloc.func1()
        c:/go/src/runtime/mheap.go:753 +0x54
runtime.(*mheap).alloc(0x6842a0, 0x1, 0x10010, 0xc042032758)
        c:/go/src/runtime/mheap.go:752 +0x91
runtime.(*mcentral).grow(0x6859f0, 0x0)
        c:/go/src/runtime/mcentral.go:232 +0x9b
runtime.(*mcentral).cacheSpan(0x6859f0, 0x1f3)
        c:/go/src/runtime/mcentral.go:106 +0x2eb
runtime.(*mcache).refill(0x20506a8, 0xc04201c510)
        c:/go/src/runtime/mcache.go:123 +0xaa
runtime.(*mcache).nextFree.func1()
        c:/go/src/runtime/malloc.go:556 +0x39
runtime.systemstack(0x0)
        c:/go/src/runtime/asm_amd64.s:409 +0x7e
runtime.mstart()
        c:/go/src/runtime/proc.go:1175

The lua script is like this:

echo(true)
if spawn([[d:\OpenSSH-Win64\sftp.exe]],"-oBatchMode=no [email protected]") then
    expect("password:")
    echo(false)
    send("password\r")
    expect("sftp>")
    echo(true)
    send("put somebigfile.mp4 && exit\r")
end

System: Windows 7 PRO

Not working on git bash terminal

I am using VsCode for my development. And my default terminal is git bash in VsCode. But when I run expect.exe on real git bash, it could not read the output or it could not send any command after spawn. Here is my screenshot and code. It waits forever. But however it runs perfectly when i run it on vscode teminal. Despite VsCode also uses the git bash. First screenshot is vscode terminal which is success.

vscode

FAILED SCREEN IN BELOW

Capture

HERE IS MY CODE

echo(true) if spawn([[c:\Program Files\Git\usr\bin\bash.exe]],"ssh-copy-id","-i",arg[1],arg[2],arg[3]) then expect("?") echo(false) sendln("yes") expect("password:") sendln("toor") end

How use another port in ssh?

Hello!
You did a great thing! Thanks.
I turn to your example.
But I can't use an alternative ssh port.
Perhaps you can give an example of how to do this. If you do not specify the port, everything works.

Thank you in advance!

Telnet login

When I use the expect for telnet login, it shows this at login

username: t?ngi?gw (the ? appaears to be random
password:

%Authentication failed.

My spawn part is this:

ipAddress = arg[1]

if spawn([[c:\Users\tangingw\Desktop\plink.exe ]], "-telnet", ipAddress) then

Timeout seems does not work properly

Thanks for this awesome library!

But I have tried doing as suggested timeout=1 of setting timeout, but seems it does not work and return the RC after it?

how to suppress output lines works

trying to supress output lines from my EXPECT script. Have echo(false) statement as an first line of script (as in examples) but it still printing output lines. What is the logic of echo()statement? if it once set up to false, does it supress output until its set to true? have it false before any send command, but it does not help.

Also tried @expect.exe "%~f0" >NUL or @expect.exe "%~f0 | break" but does not work for some reason

How to pass batch variables to expect script?

have username and password stored in bach variables and want to pass it to EXPECT script:

 expect("login as:")
    send("%login%\r")
    expect("%login%@%hostname%'s password:")
    send("%passwd%\r")

is something like this possible at all?

Automatic logout happening?

Hi,

First of all thanks for the great script.

I'm trying to use this script to automatically log into some servers (and then do work manually). It works fine with the provided script and command
.\expect sample.lua name@server password
however it automatically ends the session after around 15 seconds. Is this a setting I can change somewhere?

To note: I needed to copy the ssh.exe into the same folder because it was not able to find it through the windows path (even though it was there).

How can use it in Linux

I use it in windows for a long time.
Now I work in Linux enviroment , there is a error when I build expect.go by myself.

โžœ  go build expect.go
go build github.com/zetamatta/go-console/screenbuffer: build constraints exclude all Go files in /root/go/src/github.com/zetamatta/go-console/screenbuffer
# github.com/zetamatta/go-console
/root/go/src/github.com/zetamatta/go-console/handle.go:38:15: undefined: syscall.Handle
/root/go/src/github.com/zetamatta/go-console/handle.go:40:16: undefined: syscall.NewLazyDLL

Ignore my poor English, Thanks

Capture output from program into LUA varaible?

First of all this is great! Fantastic work.

I want to replicate this functionality in TCL Expect:

#first step is to query the count from the program to build a loop count
#we use the -re tag to capture the site count from the output buffer after the token "Count: " and extend to the end of the whole output
expect $prompt
send -- "get_count\n"
expect -re {Count: (.*)\n.*}

set count $expect_out(1,string)
sleep 1
#sleeps are just to ensure we dont over-run the CLI

#we do not need to expect here since it was part of the last regex
#now we configure the output path
send -- "set_log_path $log_path\n"

Is this currently possible in the go implementation?

Thanks!

error message [syntax error] using example in Windows 10

Hi, guys:
I get a error message when I run the example. Could anyone know that?

sample.lua

echo(true)
if spawn([[C:\Program Files\Git\usr\bin\ssh.exe]],"xxx@1xxxxx3") 
then
  expect("password:")
  echo(false)
  send("xxxxx\r")
  expect("~]$")
  echo(true)
  send("exit\r")
end

terrmial shows error message:
sample.lua at EOF: syntax error

cannot find package: github.com/zetamatta/go-getch/consoleoutput

$ go get -u github.com/zetamatta/expect
package github.com/zetamatta/go-getch/consoleoutput: cannot find package "github.com/zetamatta/go-getch/consoleoutput" in any of:
        c:\go\src\github.com\zetamatta\go-getch\consoleoutput (from $GOROOT)
        C:\Users\Hayama\go\src\github.com\zetamatta\go-getch\consoleoutput (from $GOPATH)
exit status 1

Pipe stdout to stdin

Hi,

I am trying to do this:

func main() {
      output, error := exec.Command(
            "expect.exe",
            "test.lua"
      ).Output()

      fmt.Println(string(out))
}

It seems like the main.exe does not print the output, but freezes there. Hence I would like to know if expect.go is able to pipe the stdout to stdin of another process?

what's the principle of this project?

I'm sorry, I'm not very familiar with the mechanisms of expect in windows. I noticed that almost all expect Go code fails to work on Windows due to pty. Why does this project work? Is Lua required for it? If so, can this project run on Linux?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.