code wiki / _hdl_build / nx_ssh_cmd.nx

nx_ssh_cmd.nx source

↩ module page · 29 lines · 1549 B

1// nx_ssh_cmd.nx -- generic one-shot remote command over the sovereign SSH client. Reads the command text 2// from /tmp/nxcmd and the password from /tmp/nxpw (both staged + deleted by the caller). Lets us inspect / 3// drive the NAS without rebuilding per query. Read-only or write depending on the command supplied. 4// license_tier: ORIGINAL 5import "nx_syscalls.nx" 6import "nx_ssh_lib.nx" 7 8func slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != 0 as u8 { n = n + 1 } return n } 9 10func main() -> i64 { 11 let pwbox: *i64 = sys_mmap(16) as *i64 12 let pw: *u8 = sys_read_file("/tmp/nxpw" as *u8, pwbox) 13 if (pw as i64) == 0 { ssh_puts("no /tmp/nxpw\n" as *u8); return 1 } 14 var pwlen: i64 = pwbox[0] 15 while pwlen > 0 { if pw[pwlen-1] == 10 as u8 { pwlen = pwlen - 1 } else { if pw[pwlen-1] == 13 as u8 { pwlen = pwlen - 1 } else { break } } } 16 17 let cmdbox: *i64 = sys_mmap(16) as *i64 18 let cmd: *u8 = sys_read_file("/tmp/nxcmd" as *u8, cmdbox) 19 if (cmd as i64) == 0 { ssh_puts("no /tmp/nxcmd\n" as *u8); return 1 } 20 var cmdlen: i64 = cmdbox[0] 21 while cmdlen > 0 { if cmd[cmdlen-1] == 10 as u8 { cmdlen = cmdlen - 1 } else { break } } 22 23 let st: *SshState = sys_mmap(SSH_STATE_BYTES) as *SshState 24 if ssh_open_session(st, (192 << 24) | (168 << 16) | (8 << 8) | 227) != 0 { ssh_puts("session fail\n" as *u8); return 2 } 25 if ssh_userauth_password(st, "elderwesto" as *u8, 10, pw, pwlen) != 1 { ssh_puts("auth fail\n" as *u8); sys_close(st.fd); return 3 } 26 ssh_exec(st, cmd, cmdlen) 27 sys_close(st.fd) 28 return 0 29}