开发者

Perl Telnet Session Handler

开发者 https://www.devze.com 2023-02-08 19:28 出处:网络
I have an application that makes connections to various equipment in our network on开发者_JS百科 demand, performs several commands, parses the output and reports back to the user via AJAX/JSON/Perl.In

I have an application that makes connections to various equipment in our network on开发者_JS百科 demand, performs several commands, parses the output and reports back to the user via AJAX/JSON/Perl. In Perl, I am using NET::Telnet / ::Cisco, along with the occasional SSH connection via spawning a child process and passing that to NET::Telnet.

I am looking to enhance the application by creating a sort of telnet session holder that will maintain connections after they are opened for a set period of time, or until they time out. The idea is to cut down on the reconnecting to these devices, and allowing other requests to use a telnet session that was created before (provided it's not in use and is still active).

I started writing a perl file using IO::Socket::UNIX, and am able to store the connections w/o issue, and basically another file will use the socket created to access or create new connections. The problem I am having is this: If two requests hit that socket at the same time, which ever one came in first will cause the second one to have to wait for the first to be done processing.

I started experimenting with using threads, but if I do that, I can't pass the NET::Telnet object back to the original/parent thread.

Does anyone have any ideas on how to accomplish this? Is there maybe an application that will act as a session holder that I can interface with?

UPDATE

I used POE per the suggestion from one commenter, and while this does partially what I'm looking for, it does not allow the 'server' to service multiple connections simultaneously.

Scenario: Two users click 'send' on the front end rather close together. User A's query reaches the server first, but they are trying to connect to a device that takes a long time to respond. Thusly, User B will have to wait until User A's connection is established before User B's request is even started. Essentially

, I need to be able to service simultaneous requests using a connection pool without creating delay for anyone else just because the guy that got there first is trying to connect to that slooooow device..

Below is my code which, if this is how I would do it, will be run in the background. The prints are for my debugging.

#!/usr/bin/perl -w

use strict;
use JSON;
use Net::Telnet;
use IO::Socket::UNIX qw( SOCK_STREAM SOMAXCONN );

my $socketPath = '/tmp/telnetproxy';
unlink($socketPath);

my $listener = IO::Socket::UNIX->new(
    Type   => SOCK_STREAM,
    Local  => $socketPath,
    Listen => SOMAXCONN) 
    or die ("Cannot create server socket: $!\n");

my $clientNum = 0;

our $json = JSON->new->allow_nonref;
our $conns = {};

print "Server Initiated...\n";

while (1) {

    print " - Inside while loop...\n";

    my $socket = $listener->accept();

    connectToDevice(++ $clientNum, $socket);

}

sub connectToDevice {
    my $connectionNum = shift;
    my $socket = shift;

    print " - Inside 'connectToDevice'\n";

    print " - Connection #$connectionNum started...\n";

    my $input;
    my $connId = 0;
    my (@argsRaw, $args, @argHold);
    my $numOfConnections = keys %$conns;

    my $deviceProperties = {
        ipAddress => undef,
        username  => undef,
        password  => undef,
        method    => 'telnet'
    };

    print " - waiting for input...\n";

    # Receive input for arguments.
    chomp( $input = <$socket> );

    print " - input received...\n";

    ## Turn string into a HASHREF
    $args = from_json($input);

    foreach (keys %$args) {
        print "\t$connectionNum: $_ => $args->{$_}\n";
        if (/^host$/i) { #---- Host IP given ($self->{_hostIp{
            if (verifyIp($args->{$_})) {
                $deviceProperties->{ipAddress} = $args->{$_};
            } else {

            }
        }
         elsif (/^method$/i) { # Ckt type... very important for how we ts
            $deviceProperties->{method} = $args->{$_};
        }
         elsif (/^(username|user|u)$/i) { # username to log in with
            $deviceProperties->{username} = $args->{$_};
        }
         elsif (/^(password|pass|p)$/i) { # password
            $deviceProperties->{password} = $args->{$_};
        }
    }

    print " - Num of connections: $numOfConnections\n";

    if ($numOfConnections > 0) {
        ## Look through existing connections
        ##  1) If we have an available connection, use it
        ##  2) If not, create a new connection.
        print " - Checking existing connections...\n";
        foreach my $connKey ( keys %$conns ) {
            if ($conns->{$connKey}->{host} eq $deviceProperties->{ipAddress} && $conns->{$connKey}->{locked} == 0 && testConnection($connKey)) {
                $connId = $connKey;
                print "\tconnection #$connKey... VALID, using it\n";
                last;
            } else {
                print "\tconnection #$connKey... not valid\n";
            }
        }
    } else {
        print " - No existing connections, creating a new one ...\n";
    }

    if ($connId == 0) {

        $connId = $connectionNum;

        $conns->{$connectionNum} = {
            host   => $deviceProperties->{ipAddress},
            locked => 1
        };

        $conns->{$connectionNum}->{conn} = connectToHost($deviceProperties, "blab_$connectionNum");
        print " - Created a new connection, a suitable existing connection was not found.\n";
    }

    print " - Waiting for command.. ";

    chomp( my $line = <$socket> );

    print "DONE\n";

    my @out = $conns->{$connId}->{conn}->cmd($line);

    print " - Sent '$line' to device\n";

    my $numOfLines = @out;

    print " - $numOfLines lines retrieved\n";

    $conns->{$connId}->{locked} = 0;

    print " - This run done....\n\n";

    return;
}

sub testConnection {
    my $connectionNum = shift;

    print " -- Testing connection $connectionNum:  ";

    my @out = $conns->{$connectionNum}->{conn}->cmd(String => '!', Timeout => 2);

    print "[";
    print @out;
    print "]";

    if (@out > 0) {
        print " ---- Good\n";
        return 1;
    } else {
        delete $conns->{$connectionNum};
        print " ---- No good\n";
        return 0;
    }
}


It sounds like you need to use ResourcePool to accomplish this. Just create the number of objects you need in the pool then use get and free to use them and drop them back in the pool.


I have moved to using POE server architecture, along with spawning sub/child-processes to handle individual connections. It's probably not the prettiest/best/most efficient way to do what I want, but it gets the job done and has a vary narrow purpose.

Thanks everyone to all their help.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号