#!/usr/bin/perl ## Perl script to read the eDirectory network address attribute ## of a user or server object and output the TCP/IP address. ## Colin Pearce, December 2007 ## As a command line tool or to call from another script... ## useage: netaddr.pl ## This script must be called with the following arguments ## - LDAP server qualified DNS name or IP address ## - Base OU for the LDAP search, o=myorg ## - CN of the object look up the IP address of, username or server name ## In that order! $server = $ARGV[0]; $basedn = $ARGV[1]; $filter = "cn=$ARGV[2]"; ## To use as a stand-alone script set the variables locally... #$server = 'ldap.myorg.org'; # Your LDAP server #$basedn = 'o=Novell'; # LDAP search base #$filter = 'cn=myobject'; # CN of a user or server object use Net::LDAP; $ldap = Net::LDAP->new($server) or die "$@"; $mesg = $ldap->bind; # an anonymous bind ## If you need an authenticated bind, this is the form... #$mesg = $ldap->bind( "cn=user,o=Novell", password => "mypassword"); $mesg = $ldap->search( # perform a search base => "$basedn", scope => "sub", deref => "never", filter => "$filter", attrs => ['networkAddress'], ); $mesg->code && die $mesg->error; ## uncomment the following line to dump the whole search result #foreach $entry ($mesg->entries) { $entry->dump; } ## Process the output to give IP address in decimal dot notation foreach $entry ($mesg->entries) { foreach $attr ($entry->attributes) { foreach $val ($entry->get_value($attr)) { #printf "Line: %vd\n", $val; ($type,$rest) = split(/#/,$val); #printf "Type: %d # Rest: %vd\n", $type, $rest; ## We are only interested in TCP/IP address either type 1 (IP) or type 9 (TCP) if ($type==1 || $type==9) { ## IP or TCP address type, last 4 bytes *should* be the binary address $addr = substr($rest,-4); #printf "Type: %d Addr: %vd\n", $type, $addr; $ip_addr = sprintf("%vd\n", $addr); # variable available for later use... print $ip_addr; # print the result! } } } } $mesg = $ldap->unbind; # take down session