NAME
JUNOS::Access - Implement the Access Method superclass. All Access Method classes must subclass from JUNOS::Access.
SYNOPSIS
This example is extracted from Device.pm. It creates an Access object based on the access method type specified in $self (a reference to a hash table containing information such as login, password, access method type and destination hostname). Then it starts a session with the JUNOScript server at the destination host by calling the connect method in the access object. After the session is established, it goes on to perform the initial handshake with the JUNOScript server.
my $conn = new JUNOS::Access($self);
# Need better error handling here....
ref($conn) || die "Could not open connection";
# Record the connection; connect it, mark it
$self->{JUNOS_Conn} = $conn;
$conn->connect() || die "Could not connect";
$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->disconnect;
return undef;
}
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");
DESCRIPTION
This is an internal class used by JUNOS::Device only. Its constructor returns an access method class based on the access method specified by JUNOS::Device. If the access method 'telnet' is selected, an object of class JUNOS::Access::telnet will be returned. Once JUNOS::Device has the reference to the new access method object, it uses it to make connection, and exchange information with the destination.
All access method classes (e.g. JUNOS::Access::telnet) must subclass from JUNOS::Access.
CONSTRUCTOR
new($ARGS)
The constructor of JUNOS::Access simply looks at the access method type and creates and returns an object of class JUNOS::Access::<access_method> (e.g. JUNOS::Access::telnet). $CLASS is the prefix for the access method class, "$CLASS::$access". $ARGS is the reference to a hash table containing the type of the access method, this hash table is supplied by the application while calling the constructor of JUNOS::Device.
METHODS
connect(%ARGS)
This method is called to start a session with the destination host. Internally, this method simplies calls the start method which is always overloaded by the subclass.
ARGS is a hash table containing additional input parameters to establish the session. See the individual access method subclass to see if additional iput parameters are accepted.
disconnect()
shutdown the underlying mechanics and free/destory them. This method should be overloaded by the subclass. If not overloaded, it simplies kill the process that it started for the current session, which works for telnet and rsh.
eof()
Has the end-of-file been seen?
incoming()
Feed data back to the access method when JUNOS::Device finds something that it doesn't understand. This is to have the access method object parse the connection specific messages, such as 'Host not found'. These messages are specific to the access method type so the incoming method is always overridden by the subclass. If the underlying code used by the subclass already deals with errors in connect(), recv() or send(), then this can be a NOP method.
recv()
read the next chunk of data.
send($DATA)
send data to the JUNOScript server.
start(%ARGS)
To be overloaded by subclass to start the underlaying mechanics to open a session with the JUNOScript server at the destination.
ARGS is a hash table containing additional input parameters to establish the session. See the individual access method subclass to see if additional iput parameters are accepted.
start_command($ACCESS, @FLAGS, $WHO, $EXEC)
This method is called by the subclass of JUNOS::Access to start a telnet or rsh session.
start_command_sockets()
This method is called by the subclass to start a command using unix-domain socket pairs; it is currently not used.
The following hash keys are used by JUNOS::Device.
seen_xml
Whether the <xml> element has been received.
seen_eof
Whether eof has been received.
SEE ALSO
JUNOS::Device
JUNOS::Access::rsh
JUNOS::Access::ssh
JUNOS::Access::ssl
JUNOS::Access::telnet
AUTHOR
Juniper Junoscript Perl Team, send bug reports, hints, tips, and suggestions to support@juniper.net.
COPYRIGHT
Copyright (c) 2001 Juniper Networks, Inc. All rights reserved.