Advertisement
Guest User

Untitled

a guest
Jul 6th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.58 KB | None | 0 0
  1.  
  2. Requirements
  3. ##############
  4. Username and password are declared in the top of the script
  5. Any switches to automatically traverse are listed at the top of the script
  6.  
  7. Hunting a MAC address
  8. (returns a full report of the destination port found)
  9. cisco_switch_shmac.pl <switch to start from> address <mac address>
  10. #####################################################################
  11.  
  12. perl cisco_switch_shmac.pl es-swcore1 address 0009.0fc6.b299
  13.  
  14. ! es-swcore1 Lookup For ; CAM address 0009.0fc6.b299
  15. 634 0009.0fc6.b299 DYNAMIC Gi2/0/26
  16. ! es-swcore1 Exit Via ; CDP Gi2/0/26
  17. bs-swcore1 Gig 2/0/26 175 S I WS-C3750G Gig 1/0/25
  18.  
  19. ! es-swcore1 Gi2/0/26 adjacent to bs-swcore1 Gi1/0/25
  20.  
  21. ! bs-swcore1 Lookup For ; CAM address 0009.0fc6.b299
  22. 634 0009.0fc6.b299 DYNAMIC Gi1/0/26
  23. ! bs-swcore1 Exit Via ; CDP Gi1/0/26
  24. ho-swcore1 Gig 1/0/26 146 S I WS-C2960S Gig 1/0/25
  25.  
  26. ! bs-swcore1 Gi1/0/26 adjacent to ho-swcore1 Gi1/0/25
  27.  
  28. ! ho-swcore1 Lookup For ; CAM address 0009.0fc6.b299
  29. 634 0009.0fc6.b299 DYNAMIC Gi1/0/20
  30. ! ho-swcore1 Exit Via ; CDP Gi1/0/20
  31. ho-swcore2 Gig 1/0/20 151 S I WS-C2960S Gig 1/0/20
  32.  
  33. ! ho-swcore1 Gi1/0/20 adjacent to ho-swcore2 Gi1/0/20
  34.  
  35. ! ho-swcore2 Lookup For ; CAM address 0009.0fc6.b299
  36. 634 0009.0fc6.b299 DYNAMIC Gi1/0/23
  37. ! ho-swcore2 Exit Via ; CDP Gi1/0/23
  38. ho-swaccess2 Gig 1/0/23 166 S I WS-C2950T Gig 0/1
  39.  
  40. ! ho-swcore2 Gi1/0/23 adjacent to ho-swaccess2 Gi0/1
  41.  
  42. ! ho-swaccess2 Lookup For ; CAM address 0009.0fc6.b299
  43. 634 0009.0fc6.b299 DYNAMIC Fa0/2
  44. ! ho-swaccess2 Exit Via ; CDP Fa0/2
  45.  
  46. Port Status
  47. -----------------
  48. Interface Status Protocol Description
  49. Fa0/2 up up XXXX
  50.  
  51. Port Config
  52. -----------------
  53. Building configuration...
  54.  
  55. Current configuration : 314 bytes
  56. !
  57. interface FastEthernet0/2
  58. description XXXX
  59. switchport trunk native vlan 514
  60. switchport trunk allowed vlan 514,609,634
  61. switchport mode trunk
  62. logging event trunk-status
  63. storm-control broadcast level 5.00
  64. spanning-tree link-type point-to-point
  65. spanning-tree cost 399
  66. hold-queue 2048 in
  67. hold-queue 2048 out
  68. end
  69.  
  70.  
  71. Port Stats
  72. -----------------
  73. FastEthernet0/2 is up, line protocol is up (connected)
  74. Hardware is Fast Ethernet, address is 0007.85e0.ac02 (bia 0007.85e0.ac02)
  75. Description: XXXX
  76. MTU 1500 bytes, BW 100000 Kbit, DLY 100 usec,
  77. reliability 255/255, txload 1/255, rxload 1/255
  78. Encapsulation ARPA, loopback not set
  79. Keepalive set (10 sec)
  80. Full-duplex, 100Mb/s, media type is 100BaseTX
  81. input flow-control is unsupported output flow-control is unsupported
  82. ARP type: ARPA, ARP Timeout 04:00:00
  83. Last input 00:00:44, output 00:00:00, output hang never
  84. Last clearing of "show interface" counters never
  85. Input queue: 0/2048/0/0 (size/max/drops/flushes); Total output drops: 0
  86. Queueing strategy: fifo
  87. Output queue: 0/2048 (size/max)
  88. 5 minute input rate 9000 bits/sec, 11 packets/sec
  89. 5 minute output rate 2000 bits/sec, 3 packets/sec
  90. 228476171 packets input, 4229837618 bytes, 594674 no buffer
  91. Received 51266937 broadcasts (24649438 multicast)
  92. 0 runts, 0 giants, 0 throttles
  93. 0 input errors, 0 CRC, 0 frame, 0 overrun, 594674 ignored
  94. 0 watchdog, 24649438 multicast, 0 pause input
  95. 0 input packets with dribble condition detected
  96. 243802492 packets output, 143849715 bytes, 0 underruns
  97. 0 output errors, 0 collisions, 2 interface resets
  98. 0 babbles, 0 late collision, 0 deferred
  99. 0 lost carrier, 0 no carrier, 0 PAUSE output
  100. 0 output buffer failures, 0 output buffers swapped out
  101.  
  102. Switching Stats
  103. -----------------
  104. FastEthernet0/2
  105. Switching path Pkts In Chars In Pkts Out Chars Out
  106. Processor 228476209 4229840805 243802502 143850716
  107. Route cache 0 0 0 0
  108. Total 228476209 4229840805 243802502 143850716
  109.  
  110. Trunking
  111. -----------------
  112. Port Mode Encapsulation Status Native vlan
  113. Fa0/2 on 802.1q trunking 514
  114.  
  115. Port Vlans allowed on trunk
  116. Fa0/2 514,609,634
  117.  
  118. Port Vlans allowed and active in management domain
  119. Fa0/2 514,609,634
  120.  
  121. Port Vlans in spanning tree forwarding state and not pruned
  122. Fa0/2 514,609,634
  123.  
  124. Spanning Tree
  125. -----------------
  126. Mst Instance Role Sts Cost Prio.Nbr Type
  127. ---------------- ---- --- --------- -------- --------------------------------
  128. MST00 Desg FWD 399 128.2 P2p Bound(PVST)
  129. MST01 Boun FWD 399 128.2 P2p Bound(PVST)
  130.  
  131. Storm-Control Broadcast
  132. -----------------
  133. Interface Filter State Trap State Upper Lower Current Traps Sent
  134. --------- ------------- ------------- ------- ------- ------- ----------
  135. Fa0/2 Forwarding inactive 5.00% 5.00% 0.00% 0
  136.  
  137. Storm-Control Multicast
  138. -----------------
  139. Interface Filter State Trap State Upper Lower Current Traps Sent
  140. --------- ------------- ------------- ------- ------- ------- ----------
  141. Fa0/2 inactive inactive 100.00% 100.00% N/A 0
  142.  
  143.  
  144.  
  145. ---------------------
  146.  
  147.  
  148.  
  149. Hunting a Vlan
  150. (returns a summary report of all the destination ports found)
  151. cisco_switch_shmac.pl <switch to start from> vlan <vlan id>
  152. #####################################################################
  153.  
  154. cisco_switch_shmac.pl es-swcore1 vlan 306
  155.  
  156. ! es-swcore1 Lookup For ; CAM vlan 306
  157. 306 0013.5ff7.e41b DYNAMIC Gi2/0/10
  158. 306 fcfb.fb93.02c2 DYNAMIC Gi1/0/28
  159.  
  160. ! es-swcore1 Lookup For ; CAM address 0013.5ff7.e41b vlan 306
  161. 306 0013.5ff7.e41b DYNAMIC Gi2/0/10
  162. ! es-swcore1 Exit Via ; CDP Gi2/0/10
  163.  
  164. Port Status
  165. -----------------
  166. Interface Status Protocol Description
  167. Gi2/0/10 up up es-corp-gp3 CUSTOMER bearer
  168.  
  169. ! es-swcore1 Lookup For ; CAM address fcfb.fb93.02c2 vlan 306
  170. 306 fcfb.fb93.02c2 DYNAMIC Gi1/0/28
  171. ! es-swcore1 Exit Via ; CDP Gi1/0/28
  172. cs-swcore1 Gig 1/0/28 134 S I WS-C2960- Gig 0/1
  173.  
  174. ! es-swcore1 Gi1/0/28 adjacent to cs-swcore1 Gi0/1
  175.  
  176. ! cs-swcore1 Lookup For ; CAM address fcfb.fb93.02c2
  177. 306 fcfb.fb93.02c2 DYNAMIC Fa0/12
  178. ! cs-swcore1 Exit Via ; CDP Fa0/12
  179.  
  180. Port Status
  181. -----------------
  182. Interface Status Protocol Description
  183. Fa0/12 up up XXXXXXXXXXXXXXX
  184.  
  185. Summary of ports found
  186. ----------------------
  187. es-swcore1 Gi2/0/10 up up es-corp-gp3 CUSTOMER bearer
  188. cs-swcore1 Fa0/12 up up XXXXXXXXXXXXXXX
  189.  
  190.  
  191.  
  192. -----------------------------
  193.  
  194. cisco_switch_shmac.pl
  195.  
  196. #!/usr/bin/perl
  197. #
  198. # SCRIPT TO FIND A MAC ADDRESS OR VLAN ON THE NETWORK
  199. #
  200. # MATT CARTER
  201. #
  202. ##########################################################
  203. $|=1;
  204. use Net::Telnet;
  205. ##########################################################
  206.  
  207. # cisco username and password
  208. $cisco_username = 'user';
  209. $cisco_password = 'pass';
  210.  
  211. # declare infrastructure switches to step through via CDP here
  212. # done this way to avoid hitting CDP learned customer switches
  213. %devicetypes = (
  214. 'es-swcore1', 'es-sw',
  215. 'cs-swcore1', 'cs-sw',
  216. 'bs-swcore1', 'bs-sw',
  217. 'ho-swcore1', 'ho-sw',
  218. 'ho-swcore2', 'ho-sw',
  219. 'ho-swaccess2', 'ho-sw',
  220. );
  221.  
  222. # nothing to modify below here
  223. ##########################################################
  224.  
  225. $device = shift;
  226. chomp($device);
  227. $type = shift;
  228. chomp($type);
  229. $object = shift;
  230. chomp($object);
  231.  
  232. main();
  233.  
  234. sub main {
  235. if (!$device || !$type || !$object) {
  236. print "$0 <device> <address|vlan> <object>";
  237. }
  238. if ($type eq "address" ) {
  239. address_hunt($device,$type,$object);
  240. $portdetail = 1;
  241. }
  242. if ($type eq "vlan" ) {
  243. vlan_hunt($device,$type,$object);
  244. print "\nSummary of ports found\n";
  245. print "----------------------\n";
  246. print @offenders;
  247. }
  248. }
  249.  
  250. sub vlan_hunt {
  251. my ($device,$type,$vlan) = @_;
  252. print "\n! $device Lookup For ; CAM $type $vlan\n";
  253. telnet_open($device);
  254. sleep 2;
  255. my $command = "sh mac address-table vlan $vlan | i DYNAMIC";
  256. my (@results) = $telnet->cmd($command);
  257. telnet_close($device);
  258. foreach my $result (@results) {
  259. print $result;
  260. }
  261. foreach my $result (@results) {
  262. $result =~ s/^\s+//g;
  263. my @line = split(/\s+/, $result);
  264. my $dmac = $line[1];
  265. address_hunt($device,"address",$dmac,$vlan);
  266. }
  267. }
  268.  
  269. sub address_hunt {
  270. my ($device,$type,$mac,$vlan) = @_;
  271. if ($vlan) {
  272. print "\n! $device Lookup For ; CAM $type $mac vlan $vlan\n";
  273. }
  274. else {
  275. print "\n! $device Lookup For ; CAM $type $mac\n";
  276. }
  277. telnet_open($device);
  278. sleep 2;
  279. my $command;
  280. if ($vlan) {
  281. $command = "sh mac address-table address $mac | i ^ $vlan";
  282. }
  283. elsif ($poison) {
  284. $command = "sh mac address-table address $mac | e $poison\$";
  285. }
  286. else {
  287. $command = "sh mac address-table address $mac";
  288. }
  289. #print "Command: $command\n";
  290. my (@results) = $telnet->cmd($command);
  291. telnet_close($device);
  292. foreach my $result (@results) {
  293. if ($result =~ /$mac/) {
  294. print $result;
  295. }
  296. }
  297. foreach my $result (@results) {
  298. if ($result =~ /$mac/) {
  299. $result =~ s/^\s//g;
  300. $result =~ s/^\s+//g;
  301. my ($vlan,$smac,$state,$port) = split(/\s+/, $result);
  302. #print "PORT $port\n";
  303. my $proc = "$device:$port:$mac\n";
  304. if (!$proced{$proc}) {
  305. cdp_hunt($device,$port,$type,$mac);
  306. }
  307. $proced{$proc}++;
  308. }
  309. }
  310. }
  311.  
  312. sub cdp_hunt {
  313. my ($device,$port,$type,$mac) = @_;
  314. print "! $device Exit Via ; CDP $port\n";
  315. my $found;
  316. telnet_open($device);
  317. sleep 2;
  318. my $command = "sh cdp neigh $port";
  319. my (@results) = $telnet->cmd($command);
  320. foreach my $result (@results) {
  321. #print $result;
  322. my $result2 = $result;
  323. $result2 =~ s/^\s//g;
  324. $result2 =~ s/^\s+//g;
  325. $result2 =~ s/Gig /Gi/g;
  326. my (@line) = split(/\s+/, $result2);
  327. $ddevice = $line[0];
  328. $dport = $line[-1];
  329. $sport = $line[1];
  330. if ($sport eq $port && $devicetypes{$ddevice}) {
  331. print $result;
  332. $found++;
  333. #print "ADJACENCY $ddevice $dport\n";
  334. print "\n! $device $port adjacent to $ddevice $dport\n";
  335. telnet_close($device);
  336. $poison = $dport;
  337. address_hunt($ddevice,$type,$mac);
  338. }
  339. }
  340. if (!$found) {
  341. print "\nPort Status\n";
  342. print "-----------------\n";
  343. my $command = "sh int $port desc";
  344. sleep 2;
  345. my (@results) = $telnet->cmd($command);
  346. foreach my $result (@results) {
  347. print $result;
  348. if ($result =~ /$port/) {
  349. my $offender = "$device $result";
  350. push (@offenders, $offender);
  351. }
  352. }
  353. if ($portdetail) {
  354. stats($port);
  355. }
  356. telnet_close($device);
  357. }
  358. }
  359. sub stats {
  360. my ($port) = @_;
  361. print "\nPort Config\n";
  362. print "-----------------\n";
  363. my $command = "sh run int $port";
  364. sleep 2;
  365. my (@results) = $telnet->cmd($command);
  366. foreach my $result (@results) {
  367. print $result;
  368. }
  369. print "\nPort Stats\n";
  370. print "-----------------\n";
  371. my $command = "sh int $port";
  372. sleep 2;
  373. my (@results) = $telnet->cmd($command);
  374. foreach my $result (@results) {
  375. print $result;
  376. }
  377. print "\nSwitching Stats\n";
  378. print "-----------------\n";
  379. my $command = "sh int $port stats";
  380. sleep 2;
  381. my (@results) = $telnet->cmd($command);
  382. foreach my $result (@results) {
  383. print $result;
  384. }
  385. print "\nTrunking\n";
  386. print "-----------------";
  387. my $command = "sh int $port trunk";
  388. sleep 2;
  389. my (@results) = $telnet->cmd($command);
  390. foreach my $result (@results) {
  391. print $result;
  392. }
  393. print "\nSpanning Tree\n";
  394. print "-----------------";
  395. my $command = "sh spanning-tree int $port";
  396. sleep 2;
  397. my (@results) = $telnet->cmd($command);
  398. foreach my $result (@results) {
  399. print $result;
  400. }
  401. print "\nStorm-Control Broadcast\n";
  402. print "-----------------\n";
  403. my $command = "sh storm $port broad";
  404. sleep 2;
  405. my (@results) = $telnet->cmd($command);
  406. foreach my $result (@results) {
  407. print $result;
  408. }
  409. print "\nStorm-Control Multicast\n";
  410. print "-----------------\n";
  411. my $command = "sh storm $port multi";
  412. sleep 2;
  413. my (@results) = $telnet->cmd($command);
  414. foreach my $result (@results) {
  415. print $result;
  416. }
  417. my $porta = substr($port,0,2);
  418. my $portb = substr($port,2);
  419. #print "\nLog Buffer\n";
  420. #print "-----------------\n";
  421. #my $command = "sh log | i $porta".'.*'."$portb ";
  422. #sleep 2;
  423. #my (@results) = $telnet->cmd($command);
  424. #foreach my $result (@results) {
  425. # print $result;
  426. #}
  427. }
  428.  
  429. sub telnet_open {
  430. my ($device) = (@_);
  431. $telnet = new Net::Telnet ( Timeout=>60,
  432. Errmode=>'die',
  433. #Dump_log=>'/tmp/dump.log',
  434. #Input_log=>'/tmp/input.log',
  435. Prompt => "/$device(\>|\#)/");
  436. my $prev = $telnet->max_buffer_length(16777216);
  437. my $telnet_ok = $telnet->open($device);
  438. $telnet_ok = $telnet->login($cisco_username, $cisco_password);
  439. $telnet->cmd('enable');
  440. $telnet->cmd('term length 0');
  441. }
  442.  
  443. sub telnet_close {
  444. $telnet->close;
  445. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement