Lua
Lua
Get Diagnostic JSON from the Last SSH Method Call
See more SSH Examples
Demonstrates the Chilkat Ssh.GetLastJsonData method, which copies structured diagnostic information from the most recently called method into a JsonObject. The only argument is the JsonObject that receives the information.
Background: This gives machine-readable diagnostics as an alternative to parsing
LastErrorText, which is useful for logging or automated error handling. Many methods produce little or no additional JSON, so expect an object that is sometimes nearly empty. Authentication passwords and private-key passphrases are never included, so the output is safe to log.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Demonstrates the Ssh.GetLastJsonData method, which copies structured diagnostic information
-- from the most recently called method into a JsonObject. The only argument is the JsonObject
-- that receives the information.
local ssh = chilkat.Ssh.new()
local sshPort = 22
local success = ssh:connect("ssh.example.com", sshPort)
if not success then
print(ssh.last_error_text)
return
end
-- Normally you would not hard-code the password in source. You should instead obtain it
-- from an interactive prompt, environment variable, or a secrets vault.
local password = "mySshPassword"
success = ssh:authenticate_pw("mySshLogin", password)
if not success then
print(ssh.last_error_text)
return
end
local output = ssh:quick_command("uname -a", "utf-8")
if not ssh.last_method_success then
print(ssh.last_error_text)
return
end
print(output)
-- Retrieve diagnostic details about the call that was just made. Many methods produce little
-- or no additional JSON. Passwords and private-key passphrases are never included.
local json = chilkat.JsonObject.new()
ssh:get_last_json_data(json)
json.emit_compact = false
print(json:emit())
ssh:disconnect()