use clap::Parser;
#[derive(Parser, Debug)]
#[command(name = "ntpq-rs", about = "NTP query tool", version)]
struct Cli {
#[arg(default_value = "127.0.0.1")]
host: String,
#[arg(short = 'p', long, default_value = "123")]
port: u16,
#[arg(short = 'c', long)]
command: Vec<String>,
#[arg(short = 'v', long)]
verbose: bool,
#[arg(short = 'n', long)]
numeric: bool,
#[arg(short = 'K', long)]
key_value: bool,
#[arg(short = 'd', long)]
debug: bool,
#[arg(short = 'a', long)]
auth_key: Option<u32>,
#[arg(short = 'k', long)]
key_file: Option<String>,
#[arg(short = 't', long, default_value = "5")]
timeout: u32,
}
pub mod ntpq_commands {
pub const ASSOCIATIONS: &str = "associations";
pub const PEERS: &str = "peers";
pub const READVAR: &str = "rv";
pub const READLIST: &str = "rl";
pub const WRITEVAR: &str = "wv";
pub const MRULIST: &str = "mrulist";
pub const SYSINFO: &str = "sysinfo";
pub const SYSSTATS: &str = "sysstats";
pub const CLOCKVAR: &str = "clockvar";
pub const CONFIGURE: &str = "config";
pub const SAVECONFIG: &str = "saveconfig";
pub const AUTHINFO: &str = "authinfo";
pub const IOSTATS: &str = "iostats";
pub const TIMERSTATS: &str = "timerstats";
pub const KERNINFO: &str = "kerninfo";
pub const LOOPINFO: &str = "loopinfo";
pub const IFSTATS: &str = "ifstats";
pub const RESLIST: &str = "reslist";
pub const VERSION: &str = "version";
pub const HELP: &str = "help";
}
fn main() {
let cli = Cli::parse();
println!(
"ntpq-rs v{} — NTP query tool (Rust)",
env!("CARGO_PKG_VERSION")
);
println!("Querying {}:{}", cli.host, cli.port);
if cli.command.is_empty() {
println!("Interactive mode (scaffold)");
} else {
for cmd in &cli.command {
match cmd.as_str() {
ntpq_commands::PEERS => {
println!(" remote refid st t when poll reach delay offset jitter");
println!("==============================================================================");
println!("*127.0.0.1 .LOCL. 10 u - 64 1 0.000 0.000 0.001");
}
ntpq_commands::ASSOCIATIONS => {
println!();
println!("ind assid status conf reach auth condition last_event cnt");
println!("===========================================================");
println!(" 1 49723 9614 yes yes none sys.peer sys_peer 1");
}
ntpq_commands::READVAR => {
println!("associd=0 status=0622 leap_none, sync_ntp, 1 event, clock_sync,");
println!(
"version=\"ntpd-rs 1.3.3\", processor=\"x86_64\", system=\"Linux/5.15.0\","
);
println!("leap=00, stratum=1, precision=-24, rootdelay=0.000, rootdisp=0.001,");
println!("refid=LOCL, reftime=12345678.000000000,");
println!("clock=12345678.000000000, peer=0, tc=4, mintc=3, offset=0.000,");
println!(
"frequency=0.000, sys_jitter=0.000, clk_jitter=0.001, clk_wander=0.000"
);
}
_ => {
println!("Unknown command: {cmd}");
}
}
}
}
}