# use to talk to the Juniper box.
my $conn = new JUNOS::Access($self);
# Need better error handling here....
unless (ref($conn)){
$self->report_error("Could not open connection");
return;
}
eval "use " . ref($conn);
# Record the connection; connect it, mark it
$self->{JUNOS_Conn} = $conn;
unless ($conn->connect()) {
$self->report_error("Could not connect");
return;
}
$self->{JUNOS_Connected} = 1;
# Kick off the XML parser
$self->parse_start();
trace("Trace", "starting connect::\n");
# We need to receive the server side of the initial handshake first
# (at least the <?xml?> part), so that we can avoid sending our
# handshake to the ssh processes initial prompts (password/etc).
# So we wait til we see the start of the real XML data flow....
until ($self->{JUNOS_Active}) {
my $in = $conn->recv();
my $waiting = 'waiting for xml';
if( $conn->{seen_xml} ) { $waiting = 'found xml'; }
trace("IO", "during connect - ($waiting) input:\n\t$in\n" );
if ($conn->{seen_xml}) {
# After we've seen xml, parse anything
} elsif ($in =~ /<\s*\?/) {
$in =~ s/^[\d\D]*(<\s*\?)/$1/;
$conn->{seen_xml} = 1;
} else {
if (not $conn->incoming($in) or $conn->eof) {
$self->report_error("initial handshake with JUNOScript server failed");
$self->disconnect;
return;
}
next;
}
if ($conn->eof) {
$self->parse_done($in);
last;
} else {
$self->parse_more($in);
}
}
# Send our half of the initial handshake
my $xml_decl = '<?xml version="1.0" encoding="us-ascii"?>';
my $junoscript = '<junoscript version="1.0" os="perl-api">';
$conn->send($xml_decl . "\n" . $junoscript . "\n");
if (!$conn->authenticate) {
$self->report_error("Authentication error");
exit(1);
}
return $self;
}
#
# Disconnect from the JUNOScript server. Destroy the parser and
# disconnect (and close) the connection.
# User program should first call 'request_end_session'
#
sub disconnect
{
my($self) = @_;
$self->clear_errors() if caller() ne __PACKAGE__;
my $conn = $self->{JUNOS_Conn};
$conn->disconnect if ($conn and $self->{JUNOS_Connected});
$self->parse_destroy();
$self->{JUNOS_Conn} = $self->{JUNOS_Connected} = undef;
1;
}
#
# Automatic form of destory
#
sub DESTROY
{
my($self) = @_;
$self->disconnect() if $self->{JUNOS_Connected};
=2= |