This commit is contained in:
despiegk 2025-08-05 16:39:00 +02:00
parent 4cd4e04028
commit 1770ac561e
5 changed files with 14 additions and 13 deletions

View File

@ -1,6 +1,7 @@
// Example of using the network modules in SAL through Rhai // Example of using the network modules in SAL through Rhai
// Shows TCP port checking, HTTP URL validation, and SSH command execution // Shows TCP port checking, HTTP URL validation, and SSH command execution
// Function to print section header // Function to print section header
fn section(title) { fn section(title) {
print("\n"); print("\n");
@ -19,14 +20,14 @@ let host = "localhost";
let port = 22; let port = 22;
print(`Checking if port ${port} is open on ${host}...`); print(`Checking if port ${port} is open on ${host}...`);
let is_open = tcp.check_port(host, port); let is_open = tcp.check_port(host, port);
print(`Port ${port} is ${is_open ? "open" : "closed"}`); print(`Port ${port} is ${if is_open { "open" } else { "closed" }}`);
// Check multiple ports // Check multiple ports
let ports = [22, 80, 443]; let ports = [22, 80, 443];
print(`Checking multiple ports on ${host}...`); print(`Checking multiple ports on ${host}...`);
let port_results = tcp.check_ports(host, ports); let port_results = tcp.check_ports(host, ports);
for result in port_results { for result in port_results {
print(`Port ${result.port} is ${result.is_open ? "open" : "closed"}`); print(`Port ${result.port} is ${if result.is_open { "open" } else { "closed" }}`);
} }
// HTTP connectivity checks // HTTP connectivity checks
@ -39,7 +40,7 @@ let http = net::new_http_connector();
let url = "https://www.example.com"; let url = "https://www.example.com";
print(`Checking if ${url} is reachable...`); print(`Checking if ${url} is reachable...`);
let is_reachable = http.check_url(url); let is_reachable = http.check_url(url);
print(`${url} is ${is_reachable ? "reachable" : "unreachable"}`); print(`${url} is ${if is_reachable { "reachable" } else { "unreachable" }}`);
// Check the status code of a URL // Check the status code of a URL
print(`Checking status code of ${url}...`); print(`Checking status code of ${url}...`);
@ -68,7 +69,7 @@ if is_open {
let ssh = net::new_ssh_builder() let ssh = net::new_ssh_builder()
.host("localhost") .host("localhost")
.port(22) .port(22)
.user(os::get_env("USER") || "root") .user(if os::get_env("USER") != () { os::get_env("USER") } else { "root" })
.timeout(10) .timeout(10)
.build(); .build();

View File

@ -1,7 +1,7 @@
print("Running a basic command using run().do()..."); print("Running a basic command using run().execute()...");
// Execute a simple command // Execute a simple command
let result = run("echo Hello from run_basic!").do(); let result = run("echo Hello from run_basic!").execute();
// Print the command result // Print the command result
print(`Command: echo Hello from run_basic!`); print(`Command: echo Hello from run_basic!`);
@ -13,6 +13,6 @@ print(`Stderr:\n${result.stderr}`);
// Example of a command that might fail (if 'nonexistent_command' doesn't exist) // Example of a command that might fail (if 'nonexistent_command' doesn't exist)
// This will halt execution by default because ignore_error() is not used. // This will halt execution by default because ignore_error() is not used.
// print("Running a command that will fail (and should halt)..."); // print("Running a command that will fail (and should halt)...");
// let fail_result = run("nonexistent_command").do(); // This line will cause the script to halt if the command doesn't exist // let fail_result = run("nonexistent_command").execute(); // This line will cause the script to halt if the command doesn't exist
print("Basic run() example finished."); print("Basic run() example finished.");

View File

@ -2,7 +2,7 @@ print("Running a command that will fail, but ignoring the error...");
// Run a command that exits with a non-zero code (will fail) // Run a command that exits with a non-zero code (will fail)
// Using .ignore_error() prevents the script from halting // Using .ignore_error() prevents the script from halting
let result = run("exit 1").ignore_error().do(); let result = run("exit 1").ignore_error().execute();
print(`Command finished.`); print(`Command finished.`);
print(`Success: ${result.success}`); // This should be false print(`Success: ${result.success}`); // This should be false
@ -22,7 +22,7 @@ print("\nScript continued execution after the potentially failing command.");
// Example of a command that might fail due to OS error (e.g., command not found) // Example of a command that might fail due to OS error (e.g., command not found)
// This *might* still halt depending on how the underlying Rust function handles it, // This *might* still halt depending on how the underlying Rust function handles it,
// as ignore_error() primarily prevents halting on *command* non-zero exit codes. // as ignore_error() primarily prevents halting on *command* non-zero exit codes.
// let os_error_result = run("nonexistent_command_123").ignore_error().do(); // let os_error_result = run("nonexistent_command_123").ignore_error().execute();
// print(`OS Error Command Success: ${os_error_result.success}`); // print(`OS Error Command Success: ${os_error_result.success}`);
// print(`OS Error Command Exit Code: ${os_error_result.code}`); // print(`OS Error Command Exit Code: ${os_error_result.code}`);

View File

@ -1,4 +1,4 @@
print("Running a command using run().log().do()..."); print("Running a command using run().log().execute()...");
// The .log() method will print the command string to the console before execution. // The .log() method will print the command string to the console before execution.
// This is useful for debugging or tracing which commands are being run. // This is useful for debugging or tracing which commands are being run.

View File

@ -1,8 +1,8 @@
print("Running a command using run().silent().do()...\n"); print("Running a command using run().silent().execute()...\n");
// This command will print to standard output and standard error // This command will print to standard output and standard error
// However, because .silent() is used, the output will not appear in the console directly // However, because .silent() is used, the output will not appear in the console directly
let result = run("echo 'This should be silent stdout.'; echo 'This should be silent stderr.' >&2; exit 0").silent().do(); let result = run("echo 'This should be silent stdout.'; echo 'This should be silent stderr.' >&2; exit 0").silent().execute();
// The output is still captured in the CommandResult // The output is still captured in the CommandResult
print(`Command finished.`); print(`Command finished.`);
@ -12,7 +12,7 @@ print(`Captured Stdout:\\n${result.stdout}`);
print(`Captured Stderr:\\n${result.stderr}`); print(`Captured Stderr:\\n${result.stderr}`);
// Example of a silent command that fails (but won't halt because we only suppress output) // Example of a silent command that fails (but won't halt because we only suppress output)
// let fail_result = run("echo 'This is silent failure stderr.' >&2; exit 1").silent().do(); // let fail_result = run("echo 'This is silent failure stderr.' >&2; exit 1").silent().execute();
// print(`Failed command finished (silent):`); // print(`Failed command finished (silent):`);
// print(`Success: ${fail_result.success}`); // print(`Success: ${fail_result.success}`);
// print(`Exit Code: ${fail_result.code}`); // print(`Exit Code: ${fail_result.code}`);