Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Getting data from Cisco Call Manager with Perl

by adamZ88 (Beadle)
on Feb 28, 2017 at 13:54 UTC ( [id://1183143]=perlquestion: print w/replies, xml ) Need Help??

adamZ88 has asked for the wisdom of the Perl Monks concerning the following question:

All I am looking for someone that can instruct me on how to communicate to Cisco Unified Communications Manager with Perl. The requirement is as follows: I have completed a script that via SNMP polls network switches for connected phones, now , I need to take the phone name "SEP(plus phones MAC" I.E SEPFFFFFFFFFFFF , and talks to CUCM to determine the phones Directory Number(s) programmed on it.

Here is my code thus far. Pardon my horrible variable naming convention, I was trying to create this script in a hurry. I now need some of the output of this script (the SEP Name) and query CUCM for more details about the phone. Help please?

#!/usr/bin/perl #Version 1 2/27/17 use SNMP_util; use NetAddr::IP; #use warnings; ####Variable Dec my $sCdpCacheCapabilities = "1.3.6.1.4.1.9.9.23.1.2.1.1.9"; my $sCommu +nityString = "notReallyGivingUmyCommunityString"; my $counter =0; $cd +pCacheDeviceId = "1.3.6.1.4.1.9.9.23.1.2.1.1.6"; $cdpCachePlatform = +"1.3.6.1.4.1.9.9.23.1.2.1.1.8"; $cdpInterfaceEntry = "1.3.6.1.4.1.9.9 +.23.1.1.1.1.6"; $cdpCacheAddress = "1.3.6.1.4.1.9.9.23.1.2.1.1.4"; ##### End Variable Declaration #####Open File Handle open (FILEREAD, "formerlyBridges2Input.csv"); while ($Host = <FILEREAD>){ chomp ($Host); (@cdpNeighborPackedCapabilities) = &snmpwalk("$sCommunityString\@$Ho +st","$sCdpCacheCapabilities"); foreach $PcdpCap (@cdpNeighborPackedCapabilities){ + #($index, $capHEX) = split( +/:/, $PcdpCap, 2); ($uni, $mee, $capHEX) = split (/[\.,:]/, $PcdpCap); + #($ind, $in2) = split(/./, + $index, 2); ($index) = join ('.' , $uni,$mee); my $sUnpackedCDPcap; map { $sUnpackedCDPcap .= sprintf("%02x",$_) } unpack "CCCCCC", $c +apHEX; if ($sUnpackedCDPcap == "00000490"){ $sIDcomp = "$cdpCacheDeviceId.$index"; $sPlaComp = "$cdpCachePlatform.$index"; $sInEntry = "$cdpInterfaceEntry.$uni"; $sAddComp = "$cdpCacheAddress.$index"; ###print "$sAddComp\n"; ($devName) = &snmpget("$sCommunityString\@$Host","$sIDcomp"); ($sDevPlatform) = &snmpget("$sCommunityString\@$Host","$sPlaComp") +; ($sPortName) = &snmpget("$sCommunityString\@$Host","$sInEntry"); ($ipAdd) = &snmpget("$sCommunityString\@$Host","$sAddComp"); # $Do = join '.', unpack "C*", pack "H*", $ipAdd; my $val4; map {$val4 .= sprintf("%02x",$_)} unpack "C*", $ipAdd; $neighIP = join '.', unpack "C*", pack "H*", $val4; print "$Host,$devName,$sDevPlatform,$sPortName,http://$neighIP/\n" +;} }}

Replies are listed 'Best First'.
Re: Getting data from Cisco Call Manager with Perl
by genio (Beadle) on Feb 28, 2017 at 14:19 UTC

    Hi! There are a great many things you can improve in the Perl code here.

    1. Please always use strict and warnings. These are there to help you find your errors.
    2. Always declare variables with 'my'. There are a few here that you forgot to declare
    3. When parsing CSV files, use the module for this: Text::CSV. It is much better at parsing CSV files than the naive split approach.
    4. Calling functions with & is bad practice for the vast majority of cases. That's mostly a hold-over from Perl 4 days. While there are cases where it's proper, it's not proper anywhere you've done it. Call functions simply with parens: some_function()
    5. When passing variables to a function, there's no need to surround those variables with quotes. call_func("string", $string)
    6. Do not use BAREWORD file handles.

    Since I don't have your module SNMP_util and I can't really test things, I won't try to rewrite your entire script, but here's a good starting point:

    #!/usr/bin/env perl use strict; use warnings; use NetAddr::IP; use SNMP_util; use Text::CSV (); my $csv = Text::CSV->new({binary => 1}) or die "Can't use Text::CSV: ".Text::CSV->error_diag(); open my $fh, "<:encoding(utf8)", "formerlyBridges2Input.csv" or die "Can't open CSV: $!"; while (my $row = $csv->getline($fh)) { # $row is now an array reference containing the # CSV's fields. # If the line is "a,b,c" then we will have: # $row->[0] "a" # $row->[1] "b" # $row->[2] "c" }

    Please take a look at that and try to work in your code using the CSV parser and show us what you get. If you have a specific problem, we'll be more than happy to help a bit more.

      Hello Genio,

      I have finally applied all of your suggestions to my script. They were very helpful! The only suggestion that I did not adopt was to use "Text::CSV" as my file is not really a csv file. It is a regular file that includes one switch per line. I feel that for this purpose I will not need it, but in the future I might. Thanks!

Re: Getting data from Cisco Call Manager with Perl
by marto (Cardinal) on Feb 28, 2017 at 14:15 UTC

    Before asking anyone to take a look at this please consider

    • adding use strict; use warnings;
    • define those variables
    • is SNMP_util something you've rolled your own or is this a cpan module?
    • Use open the three argument open (as per the docs), and report $! on failure (... or die "can't open file: $!";)
    • Consistent indentation wouldn't go a miss (Perl::Tidy)
    • Not a Cisco guy, but is there something on cpan to make this easier for you, e.g. Cisco::UCS?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1183143]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-24 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found