Previous page

Next page

Locate page in Contents

Print this page

Connecting to Agent

Create a new text file named AgentExample1.pl and paste or type the following code into it:

#!/usr/bin/perl -w

#

use strict;

Let's now add the code that will establish a connection with Agent.

#Set $SSL_ON = 1 if you wish to use secure connection.

use constant SSL_ON => 0;

#Connection information.

#Change the IP address to your own server address.

use constant CONF_CONNECTION => {

ip => '192.168.0.37',

port => &SSL_ON ? 4434 : 4433,

class => &SSL_ON ? 'IO::Socket::SSL' : 'IO::Socket::INET'

};

eval "use ".&CONF_CONNECTION->{class};

die $@ if $@;

#Null-terminating character (packet separator).

use constant MSG_TERMINATOR => "\0";

local $/ = &MSG_TERMINATOR;

In the code above we create a class CONF_CONNECTION containing the Agent connection information. By default we are using the IO::Socket::INET module to communicate with Agent via plain TCP/IP. If you would like to communicate with Agent securely, set the $SSL_ON constant to 1. Agent is using port 4433 for plain TCP/IP connections and port 4434 for SSL connections. The MSG_TERMINATOR constant is a binary zero character which we'll be appending to every Agent request message (every Agent request must be null-terminated). The following code creates a socket thereby getting a connection to Agent.

#Create socket

print "Connecting to Agent...\n\n";

our $socket = &CONF_CONNECTION->{class}->new(

PeerAddr => &CONF_CONNECTION->{ip},

PeerPort=> &CONF_CONNECTION->{port},

Proto => 'tcp',

);

unless($socket) {

die "Connection refused: $!"

}

We can now read the Agent response from the socket as follows:

#Read the greeting message from Agent.

my $hello = $socket->getline;

chomp($hello);

print $hello;

Save the file now and run the program by typing perl AgentExample1.pl at the command prompt. You should see the output on your screen similar to the following:

<packet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="0" priority="0" version="4.0.0">

  <origin>vzclient148-4fce28dd-0cd3-1345-bb94-3192b940fb90</origin>

  <target>agent</target>

  <data>

    <ok/>

    <eid>4fce28dd-0cd3-1345-bb94-3192b940fb90</eid>

  </data>

</packet>

If you see the above message on the screen, it means that Agent is functioning properly, and that you are connected to it. If you don't see the message, make sure that Agent is running and that you can ping the server from your client computer.

Let's now examine the response that we received from Agent. As you can see, it is an XML document. In fact, this is the very first message that you receive from Agent every time you connect to it. It is basically a greeting message from Agent, which means that the initial connection has been established successfully. The eid element contains the Server ID of the Hardware Node that your program is now connected to.

Please send us your feedback on this help page