In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Let's start with a bit of groundwork, a network engineer. I liked to visit Zhaopin recruitment two years ago and found that there was always a "perl/php/python in scripting language" on recruitment. Forget what I thought at that time, I chose perl. Because the world is now dominated by python, many people advocate that python says that perl is dead, and they hesitated for two years. But after getting started in the past two days, I found that the charm of perl is endless, especially for web workers who need to check status and deal with a lot of text messages. The regular expression function of perl is powerful and suitable.
In recent months, perl's learning and program have finally finished reading and are ready to start writing code. The first trial script is to read the excel table, and the second script is to use perl to read the cisco neighbor protocol information of the core switch, then extract the IP address in the protocol text, traverse all the switches in the network and read their cdp information until the network topology map information is formed.
During the development of the script, either the Telnet::Cisco module or later simply referred to the handling of the CISCO module directly used its parent TELNET module (cisco module did not write the waitfor method that can be used to determine whether the login is smooth or not, in order to use the waitfor method of the Telnet module to guess the switch password, , laziness is a virtue. ), it is smooth to develop methods to read show cdp neighbor detail information (these methods will be blogged later).
But when the designer traversed the company-wide network, it hit a snag.
At first, consider the switch name as the key,IP address of the hash as the value. The discovery and traversal of the whole network is achieved through a simple for loop traversing the key (more on this later). However, because of the characteristics of hash itself, repeated access always occurs between several switches in a certain branch. The same is true when array variables are added later. After thinking hard for a day and a half, I rehearsed the design repeatedly on the manuscript paper, and finally found the right algorithm (although it may not be the most efficient, but the method is correct, such as the god has a better way to welcome the recommendation! ). Let's get to the point:
Let's first take a look at the network structure:
The recommended network model of Cisco campus network is mainly divided into core layer, aggregation layer and access layer. In practical application, the large-scale campus network, due to various conditions (capital, environment, etc.) constraints, there are secondary convergence layer, access layer and access layer in series and other structures.
Therefore, like the network of the author's company, it belongs to the complex tree structure.
Second, a brief introduction to the algorithm design process:
At the beginning, the author thought about the traversal algorithm of the network structure, it was relatively simple.
(1) the initial method is to declare that three hash--dataHash,readHash,cmpHash,dataHash are stored in the pair of switch name and IP address obtained by cdp information processing, first store dataHash information in readHash, then traverse the IP of readHash, read the cdp information of each switch to form hash and assign value to dataHash. After a round of traversal, assign the readHash to the cmpHash. Then compare cmpHash and dataHash, select key-value pairs that cmpHash does not have, then assign values to readHash and repeat the above steps until the entire network topology is traversed.
Later found that the simple use of Hash in this method, always access to the last layer structure, will be stuck in several neighboring switches looping access, can not jump to the upper layer structure. This problem occurs, first of all, because the Hash itself traversing key is disordered, coupled with the author's thinking is wrong, and finally failed. In addition, the author simply used "=" in the process of Hash assignment, and later tested and verified that "=" only assigned the memory address to each other, in fact, the three Hash are the same Hash. Finally, the author compiles a method to change the three Hash from mutual assignment to "copy".
(2) after the improvement, the array mechanism is introduced, that is, the readHash is simply replaced with an array, but the effect is not good.
(3) improve again and replace cmpHash with array, but the effect is not good.
(4) the above three attempts took a day and a night, but in desperation, the author decided to use draft paper to design the algorithm and then convert it into perl. The repeated drills in the draft paper also gave the author time to really think about it. The author realized that for the complex tree structure, the judgment conditions are also very complex, which can not be completed by one or two loops + comparisons. At the same time, taking into account the characteristics of the Perl array, the final formation of the following algorithm design:
The specific code in the loop section is as follows:
LABELA: while (1) {$find_cdp_ip = pop @ {$switchlist {$uplinkip}};% data = (); eval {% data = cisco_top::cisco_cdp_hash (\ $find_cdp_ip);}; if ($@) {die "Error using MethodName method. Error: $@\ n ";} data_write (\% data); LABELB:foreach my $key2 (keys (% data)) {next LABELB if ($data {$key2}-> {ip} = ~ / $uplinkip/); push @ {$switchlist {$find_cdp_ip}}," $data {$key2}-> {ip} ";} push @ old_array, $uplinkip; $uplinkip = $find_cdp_ip LABELC: {if (exists $switchlist {$uplinkip}-> [0]) {next LABELA;} $uplinkip = pop @ old_array; if (! exists $switchlist {$root_host}-> [0]) {last LABELA;} redo LABELC;}}
In the algorithm, the loop is carried out after judging the condition, and the LABLE loop control is adopted.
Eval {.} if ($@) {.} involves error trapping in the author's homemade module.
Time limit will be added later.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.