Execute lua script in mvp from the terminal - lua

I would like to use the ipc server in mvp. It does exactly what I need. But from my app I can't talk to sockets using echo. Thats why I am thinking about lua scripts.
It seems that mpv autoload the lua scripts so they can be called by keystrokes o events.
How can I execute a lua script from the terminal and get a response if the script return something?
Just in case: in the end, I need to build something like json http responses in VLC.

Related

How to properly load a Lua script in StackExchange.Redis?

In the StackExchange.Redis docs for scripting, it says a LoadedLuaScript can be used to avoid retransmitting the Lua script to redis each time it is evaluated. It then gives an example where the script is loaded into the server. I assume this shouldn't be done everytime you call the script, so where should the scripts be loaded?
Scripts are not persitent if redis server is restarted, so how should this be handled? Should they be loaded in ConnectionRestored event of the ConnectionMultiplexer? Presumably you would need to store them in a ConcurrentDictionary?
StackExchange.Redis handles Lua script caching internally. Normally you don't have to care about loading Lua scripts manually to redis.
StackExchange.Redis automatically transmits the Lua script to redis on the first call to 'ScriptEvaluate'. For further calls of the same script only the hash is used:
var prepared = LuaScript.Prepare(script);
prepared.Evaluate(someDb); // loads the script to redis and calls it
var prepared2 = LuaScript.Prepare(script);
prepared2.Evaluate(someDb); // calls the script by it's hash
LoadedLuaScript might be useful for edge cases like loading without executing.
I also clarified the official doku.
Background information
The library tries to call the script by it's hash and if the script is missing it gets a NOSCRIPT error and then transmits the script.
See ScriptEvalMessage:GetMessages in https://github.com/StackExchange/StackExchange.Redis/blob/main/src/StackExchange.Redis/RedisDatabase.cs
The behavior is also discussed and explained in the official StackExchange.Redis github repo:
https://github.com/StackExchange/StackExchange.Redis/issues/1246
https://github.com/StackExchange/StackExchange.Redis/issues/1371

wireshark lua debug.getinfo is nil

I'm currently writing some dissector for Wireshark in Lua. The Lua code has become quite large. Because of that I'm splitting it up into multiple files (modules). I got that working. By the way my goal is that the user just needs to copy the files into the plugins directory so that the dissector is automatically loaded every time Wireshark is started.
Now, to gain access to the other files from the "main file" I need to do this:
package.prepend_path(".\plugins\3.3\modulesDir")
local mymodule= require "module"
This works fine, but it has some disadvantages. Most importantly if a user uses a different version of Wireshark I need to change the path in the Lua code. Same if it's a different directory (Linuy, Mac OS).
To get around this I did some research on how to get the path of the current Lua file and came up with this:
local moduleDir = debug.getinfo(2, "S").source:sub(2)
moduleDir = moduleDir:match("(.*[/\\])")
This works platform indepedently, so it looks to be the perfect solution for what I want. If I execute this using Wireshark > Tools > Lua > Evaluate it work perfectly fine.
BUT: If I do it in the Lua file (which is my dissector) then I get the error "attempt to index a nil value". I tried various different versions of this line but it always appears that the debug table is nil. I'm using Wireshark version 3.3.
Has anyone an idea how to get it running? Or a different approach to getting the directory where the Lua file is in? Thanks in advance.
If you are looking for "global configuration directory" then you can use Dir.global_config_path(). Here you have that init.lua is in this path, and here lua function dtails.
On Windows, I have my Lua files in the "Personal Lua Plugins" directory, which when you look at Wireshark's "Help -> About Wireshark -> Folders" dialog, is just %APPDATA%\Wireshark\plugins. So, perhaps you can just move your folder from path\to\plugins\3.3\modulesDir to just path\to\plugins\modulesDir?
I believe you will then only require:
package.prepend_path("modulesDir")
And this will allow your Lua dissector and modules to work not only with the Wireshark 3.3 development version, but also future releases as well. And if your dissector can't work with older versions of Wireshark for some reason, you can always do something like:
if get_version() < "3.3" then
return
end
Lastly, have a look at how Hadriel Kaplan solved this for his protobuf.lua dissector, where his required modules are in the "modules" directory. It's basically as I've described above. See: https://github.com/128technology/protobuf_dissector

execute lua string as lua code

I want to share lua modules with coworkers. In order to get the latest version of shared modules I want to store and fetch them with a web server.
My questions is:
Is it possible to load lua code directly from http request or string?
I want to achieve something like that :
module = [[
local sharedModule = {}
function sharedModule.greet(name) print("hello " .. name) end
return sharedModule
]]
greeter = require (module)
greeter.greet("john")
Maybe this is not the right thing to do. Is there a better approach than this one?
There's a whole section in Programming in Lua devoted to that. Your needs will be directly fulfilled with loadstring in Lua 5.1 and older, or load in Lua 5.2 and newer.
I would carefully verify the code you're actually executing, though. At the very least, version it (running a wrong version would most probably end up in all sorts of problems, if the code being run depends on the environment being in a certain state). Optimally checksum and sign the code, and verify the signature before doing anything. If your environment isn't protected, this is essentially a huge backdoor opening.
You could also use rings library to isolate the code you're running within the Lua environment itself. It might not be airtight security-wise, but should at least prevent the received code from crashing your application if/when it goes awry.

How do I know my lua script is running in Wireshark?

I'm about to write a dissector for Wireshark in Lua but wanted to test a simple hello world first. But how do know it is running? Can I see the debug print somewhere? This is the script:
-- hello.lua
print("Hello World!")
I run Wireshark 0.99.7 on Windows.
There's a note on the wireshark Lua page saying:
Please note: On Windows, you may not see any output when running Lua scripts in Wireshark. If the console window is enabled it will be opened after the lua engine is loaded. This does not affect TShark, since it is a console program.
Maybe that's what you're seeing (or not for that matter).
For later use, you'll probably be using wiresharks Lua API functions like these functions
You can use debug(). If lua is working, you should see it in the console, which can be found in tools->lua->console.

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.

Resources