How to establish an ssh connection in a Lua script to execute a command on a remote server?
I want to write a script in Lua for开发者_高级运维 establishing an ssh connection to execute a command on a remote server
Can anyone give me a hint
Thank you
You can use os.execute ('ssh user@127.0.0.1')
to make the connection, but you may have to use os.execute ('ssh user@127.0.0.1 &'..yourCommand)
to make it execute afterwards in the shell, but I'm not entirely certain that it would work. It maybe better to create the script in Bash and execute that from Lua. If you needed to run differing commands, then you could have the script receive arguments.
As said by U319344, os.execute
would be enough if you simply want to execute some program at the remote side.
If you need to interact with this program, you will need io.popen
- it returns a file handle which you can use to read from and write to the remote command.
(And usually you will want to setup public-key authentication to not have to deal with passwords here.)
The simplest solution is to use io.popen
as others suggested. If you want more control, try lpty.
精彩评论