Establish a Junos XML Protocol Session Using C Client Applications
This example illustrates how a Junos XML protocol C client application
uses the SSH or Telnet protocol to establish a connection and Junos
XML protocol session with a device running Junos OS. In the line that
begins with the string execlp, the client
application invokes the ssh command. (Substitute the telnet command if appropriate.) The routing-platform argument to the execlp routine specifies
the hostname or IP address of the Junos XML protocol server device.
The junoscript argument is the command
that converts the connection to a Junos XML protocol session.
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");