connection to the active server Infoblox - perl

I am a beginner and I have to take a solution using two Infoblox boxes.
Currently, a server is active (master) and the other is passive. if the first fails, the second takes over. I use Perl API, how do I know when I try to connect to a server ,if it is the active server or the passive server?
I would only make the connection to the active server,
I have thought about the method « active_position() » of Infoblox::Grid::Member but I dont know how to use it..
use strict;
use Infoblox;
my $grid_member = Infoblox::Grid::Member->new(gateway=> "xxx.xxx.xxx.xxx",ipv4addr=> "xxx.xxx.xxx.xxx",mask=> "xxx.xxx.xxx.xxx", name=> "ibiza.mydomain.com");
print 'grid : '. $grid_member . "\n";
my $active_server = $grid_member->active_position();
print $active_server . "\n";
exit;
And this returns
grid : Infoblox::Grid::Member=HASH(0xf10ca8)
0
What is this "0" ??
Thank you in advance

If your Grid Master is a HA pair then you don't need to worry about which one to connect to. You just connect to the VIP (virtual IP) of the HA pair which will always be the same address.
Example session test code:
#!/usr/bin/perl
use strict;
use Infoblox;
# Create a session to the Infoblox appliance
my $SESSION = Infoblox::Session->new(
master => "192.168.1.2",
username => "admin",
password => "***"
);
if ($SESSION->status_code()) {
my $result = $SESSION->status_code();
my $response = $SESSION->status_detail();
print "Error: $response ($result)\n";
} else {
print "Connection established\n";
print "Server Version: ".$SESSION->server_version()."\n";
}
Check the API docs on your appliance https://appianceip/api/doc there are lots and lots of examples embedded in the API docs.
Steve

Related

connect to localhost failed (Connection refused) no (more) retries

I want to send an email using perl ,but when i execute the command as follows:
#./sendmail.sh "par1" "par2" "par3"
i got the error msg "connect to localhost failed (Connection refused) no (more) retries"
sendmail.sh:
/usr/bin/perl /code/sendmail.pl "$1" "$2" "$3";
sendmail.pl:
#!/usr/bin/perl -w
use Mail::Sendmail;
my $event1 = shift(#ARGV);
my $event2 = shift(#ARGV);
my $time = shift(#ARGV);
#my $info = shift(#ARGV);
my $datetime = `/bin/date "+20%y-%m-%d %H:%M:%S"`;
chomp $datetime;
$msg = "This is Monitor System speak:\n
The system discovers the events at $datetime.
Something may be abnormal, please check it. The detail is below:\n";
$msg = $msg."$event1 and $event2 at $time\n";
$msg = $msg."\n";
$msg = $msg."Any problem, check it from http://map_test.php\n\n\n";
$mail_subject = "Abnormal";
sendmail(
From => 'localhost',
To => 'test#mail.com',
Subject => $mail_subject,
Message => $msg,
);
Any help appreciated.
smtp stands for simple mail transfer protocol.
When you need to send an email your mail client needs to talk to an smtp server which will accept the message. Normally your internet service provider will provide an smtp host. If you look at your mail client it will need to have an smtp server configured to be able to send mail.
Ok so when you install the Mail::Sendmail module, it doesn't know what your smtp server will be. It is up to you to tell it. It provides a default of localhost which would often be true if your server is running a sendmail daemon.
The configuration of Mail::Sendmail is stored in a variable called
%Mail::Sendmail::mailcfg
You can change the value of the sendmail server using this snippet of code:
unshift #{$Mail::Sendmail::mailcfg{'smtp'}} , 'my.smtp.server';
You need to add this line of code to your script to set the smtp server.
It adds this server to an array which also includes localhost.
So if neither of the hosts work it will still print an error message about localhost which is slightly confusing.
If you use Data::Dumper to print the contents of the mailcfg variable it will look something like this:
#!/usr/bin/perl
use Mail::Sendmail;
use Data::Dumper;
unshift #{$Mail::Sendmail::mailcfg{'smtp'}} , 'my.smtp.server';
print Dumper(\%Mail::Sendmail::mailcfg);
Should return:
$VAR1 = {
'retries' => 1,
'smtp' => [
'my.smtp.server',
'localhost'
],
'delay' => 1,
'port' => 25,
'from' => '',
'debug' => 0,
'tz' => '',
'mime' => 1
};

Net::Telnet Error Handling

Im trying to print "Error IP USER PASS Failed!", on failure.
When i use my script
use Net::Telnet ();
my $t = new Net::Telnet Timeout => 30,
Prompt => '/[\$#%:><][\s\b]+$/';
$t->open($ARGV[0]);
my $user = $ARGV[1];
my $pass = $ARGV[2];
print "Logging In...\n";
print "Sending Username....\n";
$t->print($user);
$t->waitfor('/[:>%\$#]/');
print "Sending Password....\n";
$t->print($pass);
$t->waitfor('/[:>%\$#]/');
if ($t->errmsg){
print "errmsg: " . $t->errmsg . "\n";
} else {
print"success\n";
}
The issue:
I know the IP and username: admin and admin are working.
root#localhost:~/tel# perl t2.pl *.*.*.* admin admin
Logging In...
Sending Username....
Sending Password....
success
Now using a NON working user and pass i get the same "success"
root#localhost:~/tel# perl t2.pl 36.85.134.191 admin admi1
Logging In...
Sending Username....
Sending Password....
success
What am i doing wrong with the if statement, something is not working correctly.
You should probably enable debugging, but I guess that your expectation for the result of passwort matches not only the prompt (e.g. $ or #) but also the username prompt you get if the password was wrong (:).

using perl to get users of AD group

I've been trying to get this to print out all the members in "domain users". Problem is, it only prints out a small portion of them, then it just sorta stops. Not sure why. Can someone shed some light on the problem?
#!/usr/bin/perl
use Net::LDAP;
my $uid = "cn=account,cn=users,dc=domain,dc=local";
my $bindPass = "password";
my $ldapServer = "ldap://server.domain.local";
# connect to ldap server
$ldap = Net::LDAP -> new ($ldapServer) || die "Could not connect to server\n";
# bind to ldap server
$ldap -> bind($uid, password => $bindPass);
# search for group
$mesg = $ldap -> search(filter => "(&(cn=Domain Users))", base => "dc=domain,dc=local");
$entry = $mesg -> entry;
# #members = $entry -> get_value ('member;Range=0-*');
#the above entry when uncommented doesn't work either.
#members = $entry -> get_value ('member');
foreach $thing (#members) {
print "$thing\n";
}
From the Net::LDAP docs:
sizelimit => N
A sizelimit that restricts the maximum number of entries to be
returned as a result of the search. A value of 0, and the default,
means that no restriction is requested. Servers may enforce a maximum
number of entries to return.
It might very well be your AD server has a restriction configured. Try checking $mesg->error() after the search.
You might have more success if you use ldap://server.domain.local:3268/ as your URL. AD uses a "mini" ldap server on that port to talk to replicated servers (google "global catalog"); you won't see all attributes on that server, but maybe it's less restrictive as to the maximum number of entries.

How to receive a new email directly from email server in Perl

I have a an application sitting on a desktop and I want to run it directly through the trigger of an Email. Basically I want a script that would periodically fetch new mails from the server and trigger my script on receipt of a specific mail.
How should I proceed, or in other words which libraries in Perl can help?
I have gone through POP and IMAP manuals…I just don't know a library in Perl that could help me listen to a server.
Net::POP3 is a good place to start... this is an example from their manpage...
use Net::POP3;
$pop = Net::POP3->new('pop3host');
$pop = Net::POP3->new('pop3host', Timeout => 60);
if ($pop->login($username, $password) > 0) {
my $msgnums = $pop->list; # hashref of msgnum => size
foreach my $msgnum (keys %$msgnums) {
my $msg = $pop->get($msgnum);
print #$msg;
$pop->delete($msgnum);
}
}
$pop->quit;

Trouble using Telnet in perl?

I'm having some trouble send commands and receiving text/data from the telnet connection that I set up.
#!perl
#Telnet.pl
use Net::Telnet;
# Create a new instance of Net::Telnet,
my $telnetCon = new Net::Telnet (Timeout => 20,
Dump_Log => "dump.log",
Input_Log => "infile.log",
Output_log => "output.log",
Prompt => '/\$ $/') or die "Could not make connection.";
# Connect to the host of the users choice
$telnetCon->open('');
#get username and password from user
my $CXusername = '';
my $CXpassword = '';
my $task = '50104'; # Get this input from the search
# Recreate the login
# Wait for the login: message and then enter the username
$telnetCon->waitfor(match => '/login:/i');
# this method adds a \n to the end of the username, it mimics hitting the enter key after entering your username
$telnetCon->print($CXusername);
$telnetCon->waitfor(match => '/Password:/i');
# does the same as the previous command but for the password
$telnetCon->print($CXpassword);
#Wait for the login successful message
$telnetCon->waitfor(match => '/$/');
$telnetCon->cmd("viewtask 50104");
$telnetCon->cmd(" ");
$telnetCon->cmd(" ");
#output = $telnetCon->cmd("who");
print #output;
($output) = $telnetCon->waitfor(match => '/$/');
print "Output: ",$output;
if($searched =~ /MODIFIED files in Task $_[1] :(.*?)The/s){
# to Logout of the telnet connection
$telnetCon->print("exit");
#return the modified data
return $1;
}
Tell me if the question doesn't make sense, I'll try and reword it.
So this is the telnet view. I get stuck on the first image when i enter the $telnetCon->cmd("viewtask 50140"). I want to get to the second image and continue entering commands into my telnet session.
The Net::Telnet cmd method writes your command (with a \n appended) and waits for the prompt. this is incompatible with your program's waiting for input to complete output.
I think in your case you would want to use a combination of print and getlines and/or waitfor to get this to work