wip
This commit is contained in:
		
							
								
								
									
										83
									
								
								examples/scripts/network/network_rhai.rhai
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								examples/scripts/network/network_rhai.rhai
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,83 @@
 | 
			
		||||
// Example of using the network modules in SAL through Rhai
 | 
			
		||||
// Shows TCP port checking, HTTP URL validation, and SSH command execution
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// Function to print section header
 | 
			
		||||
fn section(title) {
 | 
			
		||||
    print("\n");
 | 
			
		||||
    print("==== " + title + " ====");
 | 
			
		||||
    print("\n");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// TCP connectivity checks
 | 
			
		||||
section("TCP Connectivity");
 | 
			
		||||
 | 
			
		||||
// Create a TCP connector
 | 
			
		||||
let tcp = net::new_tcp_connector();
 | 
			
		||||
 | 
			
		||||
// Check if a port is open
 | 
			
		||||
let host = "localhost";
 | 
			
		||||
let port = 22;
 | 
			
		||||
print(`Checking if port ${port} is open on ${host}...`);
 | 
			
		||||
let is_open = tcp.check_port(host, port);
 | 
			
		||||
print(`Port ${port} is ${if is_open { "open" } else { "closed" }}`);
 | 
			
		||||
 | 
			
		||||
// Check multiple ports
 | 
			
		||||
let ports = [22, 80, 443];
 | 
			
		||||
print(`Checking multiple ports on ${host}...`);
 | 
			
		||||
let port_results = tcp.check_ports(host, ports);
 | 
			
		||||
for result in port_results {
 | 
			
		||||
    print(`Port ${result.port} is ${if result.is_open { "open" } else { "closed" }}`);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// HTTP connectivity checks
 | 
			
		||||
section("HTTP Connectivity");
 | 
			
		||||
 | 
			
		||||
// Create an HTTP connector
 | 
			
		||||
let http = net::new_http_connector();
 | 
			
		||||
 | 
			
		||||
// Check if a URL is reachable
 | 
			
		||||
let url = "https://www.example.com";
 | 
			
		||||
print(`Checking if ${url} is reachable...`);
 | 
			
		||||
let is_reachable = http.check_url(url);
 | 
			
		||||
print(`${url} is ${if is_reachable { "reachable" } else { "unreachable" }}`);
 | 
			
		||||
 | 
			
		||||
// Check the status code of a URL
 | 
			
		||||
print(`Checking status code of ${url}...`);
 | 
			
		||||
let status = http.check_status(url);
 | 
			
		||||
if status != () {
 | 
			
		||||
    print(`Status code: ${status}`);
 | 
			
		||||
} else {
 | 
			
		||||
    print("Failed to get status code");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Get content from a URL
 | 
			
		||||
print(`Getting content from ${url}...`);
 | 
			
		||||
let content = http.get_content(url);
 | 
			
		||||
print(`Content length: ${content.len()} characters`);
 | 
			
		||||
print(`First 100 characters: ${content.substr(0, 100)}...`);
 | 
			
		||||
 | 
			
		||||
// Only attempt SSH if port 22 is open
 | 
			
		||||
if is_open {
 | 
			
		||||
    // SSH connectivity checks
 | 
			
		||||
    section("SSH Connectivity");
 | 
			
		||||
    
 | 
			
		||||
    // Create an SSH connection to localhost (if SSH server is running)
 | 
			
		||||
    print("Attempting to connect to SSH server on localhost...");
 | 
			
		||||
    
 | 
			
		||||
    // Using the builder pattern
 | 
			
		||||
    let ssh = net::new_ssh_builder()
 | 
			
		||||
        .host("localhost")
 | 
			
		||||
        .port(22)
 | 
			
		||||
        .user(if os::get_env("USER") != () { os::get_env("USER") } else { "root" })
 | 
			
		||||
        .timeout(10)
 | 
			
		||||
        .build();
 | 
			
		||||
    
 | 
			
		||||
    // Execute a simple command
 | 
			
		||||
    print("Executing 'uname -a' command...");
 | 
			
		||||
    let result = ssh.execute("uname -a");
 | 
			
		||||
    print(`Command exit code: ${result.code}`);
 | 
			
		||||
    print(`Command output: ${result.output}`);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
print("\nNetwork connectivity checks completed.");
 | 
			
		||||
		Reference in New Issue
	
	Block a user