使用 C 客户端Junos建立一个统一的 XML 协议会话
此示例说明了 Junos XML 协议 C 客户端应用程序如何使用 SSH 或 Telnet 协议与运行 Junos OS 的设备建立连接和 Junos XML 协议Junos OS。在以字符串开头的行中 execlp
,客户端应用程序将调用 ssh
命令。(如果适用, telnet
替换 命令。)对 routing-platform 例程的参数 execlp
指定 Junos XML 协议服务器设备的主机名或 IP 地址。参数 junoscript
是将连接转换为 Junos XML 协议会话的命令。
int ipipes[ 2 ], opipes[ 2 ]; pid_t pid; int rc; char buf[ BUFSIZ ]; if (pipe(ipipes) <0 || pipe(opipes) <0) err(1, "pipe failed"); pid = fork( ); if (pid <0) err(1, "fork failed"); if (pid == 0) { dup2(opipes[ 0 ], STDIN_FILENO); dup2(ipipes[ 1 ], STDOUT_FILENO); dup2(ipipes[ 1 ], STDERR_FILENO); close(ipipes[ 0 ]); /* close read end of pipe */ close(ipipes[ 1 ]); /* close write end of pipe */ close(opipes[ 0 ]); /* close read end of pipe */ close(opipes[ 1 ]); /* close write end of pipe */ execlp("ssh", "ssh", "-x", routing-platform , "junoscript", NULL); err (1, "unable to execute: ssh %s junoscript," device); } close(ipipes[ 1 ]); /* close write end of pipe */ close(opipes[ 0 ]); /* close read end of pipe */
if (write(opipes[ 1 ], initial_handshake, strlen(initial_handshake)) <0 ) err(1, "writing initial handshake failed"); rc=read(ipipes[ 0 ], buf, sizeof(buf)); if (rc <0) err(1, "read initial handshake failed");