Whoops, here's the right one. YOu might first like to try printing the list for removing before actually rpoceeding with the "ip route remove".
my @RouteList = $Terminal->cmd("ip route print"); foreach (@RouteList) { chomp ; s/^\s*// ; my @RouteArray = split(/ +/,$_,7); if (($RouteArray[1] =~ /^\s*S$/) and ($RouteArray[4] =~ m/^165\.(165|146)/) and ($RouteArray[6] =~ m/National\s*Gateway/i)) { $Terminal->cmd("ip route remove " . $RouteArray[0]); } } I tried the following #!/usr/bin/perl while (<>) { chomp ; s/^\s*// ; my @arr = split(/ +/,$_,7); #print "\$arr[5] is <$arr[5]>\n"; print "Successfull match for route entry : $arr[0]\n" if (($arr[1] =~ /^\s*S$/) and ($arr[4] =~ m/^165\.(165|146)/) and ($arr[6] =~ m/National\s*Gateway/i)) ; } on your entire route entries(including the invalid static ones), and the result I get is E:\>chris.pl file1.txt Successfull match for route entry : 264 Successfull match for route entry : 265 Successfull match for route entry : 266 Successfull match for route entry : 267 Successfull match for route entry : 268 Successfull match for route entry : 269 Successfull match for route entry : 270 Successfull match for route entry : 271 Successfull match for route entry : 272 Successfull match for route entry : 273 Successfull match for route entry : 274 Successfull match for route entry : 275 Successfull match for route entry : 276 Successfull match for route entry : 277 Successfull match for route entry : 278 Successfull match for route entry : 279 Successfull match for route entry : 280 Successfull match for route entry : 281 Successfull match for route entry : 282 Successfull match for route entry : 283 Successfull match for route entry : 284 Successfull match for route entry : 285 Successfull match for route entry : 286 Successfull match for route entry : 287 Successfull match for route entry : 288 Successfull match for route entry : 289 Successfull match for route entry : 290 Successfull match for route entry : 291 Successfull match for route entry : 292 Successfull match for route entry : 293 Successfull match for route entry : 294 Successfull match for route entry : 295 Successfull match for route entry : 296 Successfull match for route entry : 297 Successfull match for route entry : 298 Successfull match for route entry : 299 Successfull match for route entry : 300 Successfull match for route entry : 301 Successfull match for route entry : 302 Successfull match for route entry : 303 Successfull match for route entry : 304 Successfull match for route entry : 305 Successfull match for route entry : 306 Successfull match for route entry : 307 Successfull match for route entry : 308 Successfull match for route entry : 309 Successfull match for route entry : 310 Successfull match for route entry : 311 Successfull match for route entry : 312 Successfull match for route entry : 313 Successfull match for route entry : 314 Successfull match for route entry : 315 Successfull match for route entry : 316 Successfull match for route entry : 317 Successfull match for route entry : 318 Successfull match for route entry : 319 Successfull match for route entry : 320 Successfull match for route entry : 321 Successfull match for route entry : 322 Successfull match for route entry : 323 Successfull match for route entry : 324 Successfull match for route entry : 325 Successfull match for route entry : 326 Successfull match for route entry : 327 Successfull match for route entry : 328 Successfull match for route entry : 329 Successfull match for route entry : 330 Successfull match for route entry : 331 Successfull match for route entry : 332 Successfull match for route entry : 333 Successfull match for route entry : 334 Successfull match for route entry : 335 Successfull match for route entry : 336 Successfull match for route entry : 337 Successfull match for route entry : 338 Successfull match for route entry : 339 Successfull match for route entry : 340 Successfull match for route entry : 341 Successfull match for route entry : 342 Successfull match for route entry : 343 Successfull match for route entry : 344 Successfull match for route entry : 345 Successfull match for route entry : 346 Successfull match for route entry : 347 Successfull match for route entry : 348 Successfull match for route entry : 349 Successfull match for route entry : 350 Successfull match for route entry : 351 Successfull match for route entry : 352 Successfull match for route entry : 353 Successfull match for route entry : 354 Successfull match for route entry : 355 Successfull match for route entry : 356 Successfull match for route entry : 357 Successfull match for route entry : 358 Successfull match for route entry : 359 Successfull match for route entry : 360 Successfull match for route entry : 361 Successfull match for route entry : 362 Successfull match for route entry : 363 Successfull match for route entry : 364 Successfull match for route entry : 365 Successfull match for route entry : 366 Successfull match for route entry : 367 Successfull match for route entry : 368 Successfull match for route entry : 369 Successfull match for route entry : 370 Successfull match for route entry : 371 Successfull match for route entry : 372 Successfull match for route entry : 373 Successfull match for route entry : 374 Successfull match for route entry : 375 Successfull match for route entry : 376 Successfull match for route entry : 377 Successfull match for route entry : 378 Successfull match for route entry : 379 Successfull match for route entry : 380 Successfull match for route entry : 381 Successfull match for route entry : 382 Successfull match for route entry : 383 Successfull match for route entry : 384 Successfull match for route entry : 385 Successfull match for route entry : 386 Successfull match for route entry : 387 Successfull match for route entry : 388 Successfull match for route entry : 389 Successfull match for route entry : 390 Successfull match for route entry : 391 Successfull match for route entry : 392 Successfull match for route entry : 393 Successfull match for route entry : 394 Successfull match for route entry : 395 Successfull match for route entry : 396 Successfull match for route entry : 397 Successfull match for route entry : 398 Successfull match for route entry : 399 Successfull match for route entry : 400 Successfull match for route entry : 401 Successfull match for route entry : 402 Successfull match for route entry : 403 Successfull match for route entry : 404 Successfull match for route entry : 405 Successfull match for route entry : 406 Successfull match for route entry : 407 Successfull match for route entry : 408 Successfull match for route entry : 409 Successfull match for route entry : 410 Successfull match for route entry : 411 Successfull match for route entry : 412 Successfull match for route entry : 413 Successfull match for route entry : 414 Successfull match for route entry : 415 Successfull match for route entry : 416 Successfull match for route entry : 417 Successfull match for route entry : 418 Successfull match for route entry : 419 Successfull match for route entry : 420 Successfull match for route entry : 421 Successfull match for route entry : 422 Successfull match for route entry : 423 Successfull match for route entry : 424 Successfull match for route entry : 425 Successfull match for route entry : 426 Successfull match for route entry : 427 Successfull match for route entry : 428 Successfull match for route entry : 429 Successfull match for route entry : 430 Successfull match for route entry : 431 Successfull match for route entry : 432 Successfull match for route entry : 433 Successfull match for route entry : 434 Successfull match for route entry : 435 Successfull match for route entry : 436 Successfull match for route entry : 437 Successfull match for route entry : 438 Successfull match for route entry : 439 Successfull match for route entry : 440 Successfull match for route entry : 441 Successfull match for route entry : 442 Successfull match for route entry : 443 Successfull match for route entry : 444 Successfull match for route entry : 445 Successfull match for route entry : 446 Successfull match for route entry : 447 Successfull match for route entry : 448 Successfull match for route entry : 449 Successfull match for route entry : 450 Successfull match for route entry : 451 Successfull match for route entry : 452 Successfull match for route entry : 453 Successfull match for route entry : 454 Successfull match for route entry : 455 Successfull match for route entry : 456 Successfull match for route entry : 457 Successfull match for route entry : 458 Successfull match for route entry : 459 Successfull match for route entry : 460 Successfull match for route entry : 461 Successfull match for route entry : 462 Successfull match for route entry : 463 Successfull match for route entry : 464 Successfull match for route entry : 465 Successfull match for route entry : 466 Successfull match for route entry : 467 Successfull match for route entry : 468 Successfull match for route entry : 469 Successfull match for route entry : 470 Successfull match for route entry : 471 Successfull match for route entry : 472 Successfull match for route entry : 473 Successfull match for route entry : 474 Successfull match for route entry : 475 Successfull match for route entry : 476 Successfull match for route entry : 477 Successfull match for route entry : 478 Successfull match for route entry : 479 Successfull match for route entry : 480 Successfull match for route entry : 481 Successfull match for route entry : 482 Successfull match for route entry : 483 Successfull match for route entry : 484 Successfull match for route entry : 485 Successfull match for route entry : 486 Successfull match for route entry : 487 Successfull match for route entry : 488 Successfull match for route entry : 489 Successfull match for route entry : 490 Successfull match for route entry : 491 Successfull match for route entry : 492 Successfull match for route entry : 493 Successfull match for route entry : 494 Successfull match for route entry : 495 Successfull match for route entry : 496 Successfull match for route entry : 497 Successfull match for route entry : 498 Successfull match for route entry : 499 Successfull match for route entry : 500 Successfull match for route entry : 501 Successfull match for route entry : 502 Successfull match for route entry : 503 Successfull match for route entry : 504 Successfull match for route entry : 505 Successfull match for route entry : 506 Successfull match for route entry : 507 Successfull match for route entry : 508 Successfull match for route entry : 509 Successfull match for route entry : 510 Successfull match for route entry : 511 Successfull match for route entry : 512 Successfull match for route entry : 513 Successfull match for route entry : 514 Successfull match for route entry : 515 Successfull match for route entry : 516 Successfull match for route entry : 517 Successfull match for route entry : 518 Successfull match for route entry : 519 Successfull match for route entry : 520 Successfull match for route entry : 521 Successfull match for route entry : 522 Successfull match for route entry : 523 Successfull match for route entry : 524 Successfull match for route entry : 525 Successfull match for route entry : 526 Successfull match for route entry : 527 Successfull match for route entry : 528 Successfull match for route entry : 529 Successfull match for route entry : 530 Successfull match for route entry : 531 Successfull match for route entry : 532 Successfull match for route entry : 533 Successfull match for route entry : 534 Successfull match for route entry : 535 I guess this should do the trick for you. What I suspect was happening earlier was that in some of your route entries, there were spaces at the start and in some, there werent any. So, the split was making $RouteArray[n] different for dfferent entries. Thats why I have introduced an additional s/^\s*//; to get rid of the spaces |-----Original Message----- |From: Manav Mathur [mailto:[EMAIL PROTECTED] |Sent: Wednesday, January 19, 2005 1:29 PM |To: Manav Mathur; Chris Knipe; beginners@perl.org |Subject: RE: regex needed | | | |Ok Chris. I think I know why thats happening. Its because in some of your |route entries, there's a space at the beginning whereas for some there aint |such thing. | |try the following | |#!/usr/bin/perl |my @RouteList = $Terminal->cmd("ip route print"); |foreach (@RouteList) { | s/^\s*// ; | chomp ; | my @RouteArray = split(/ +/,$_,8); | if (($RouteArray[2] =~ /^\s*S$/) and ($RouteArray[5] =~ |m/^165\.(165|146)/) and ($RouteArray[7] =~ m/National\s*Gateway/i)) { | $Terminal->cmd("ip route remove " . $RouteArray[1]); | } | } | | | ||-----Original Message----- ||From: Manav Mathur [mailto:[EMAIL PROTECTED] ||Sent: Wednesday, January 19, 2005 12:58 PM ||To: Chris Knipe; beginners@perl.org ||Subject: RE: regex needed || || || ||Chris , cant get to you. || ||1) Use $RouteArray[2] instead of $RouteArray['2'] ||2) POst me the 11th route entry || |||-----Original Message----- |||From: Chris Knipe [mailto:[EMAIL PROTECTED] |||Sent: Tuesday, January 18, 2005 7:55 PM |||To: beginners@perl.org |||Subject: Re: regex needed ||| ||| |||Hi Randy, ||| |||Ok thank you very much. This works :) The problem now, is that I have |||never before in my life worked with hashes... ||| |||I have now looped the route list, and it is saved in the hash. Do I loop |||the hash again now (dumping all data) and then put in |||comparisions? I'm not |||100% on how to work with this further now to check which route is on the |||interface that I actually want to delete... ||| |||I want to achieve something like ||| |||if (($data{interface} eq "National Gateway") && ($data{gateway} =~ |||m/165\.(165|146)\.0\.[0-8]/)) { ||| #work with $data{rule}; |||} ||| |||-- |||Chris. ||| ||| ||| ||| ||| ||| ||| |||----- Original Message ----- |||From: "Randy W. Sims" <[EMAIL PROTECTED]> |||To: "Chris Knipe" <[EMAIL PROTECTED]> |||Cc: <beginners@perl.org> |||Sent: Tuesday, January 18, 2005 4:04 PM |||Subject: Re: regex needed ||| ||| |||> Chris Knipe wrote: |||>> Lo everyone, |||>> |||>> Can someone please give me a regex to parse the following... |Flags: X - |||>> disabled, I - invalid, D - dynamic, J - rejected, |||>> C - connect, S - static, r - rip, o - ospf, b - bgp |||>> # DST-ADDRESS G GATEWAY DISTANCE INTERFACE |||>> 0 S 0.0.0.0/0 r 198.19.0.2 1 SERVER-CORE |||>> 1 S 196.0.0.0/8 r 165.146.192.1 1 National Gateway |||>> 2 DC 198.19.1.0/24 r 0.0.0.0 0 WIRELESS-CORE |||>> 3 Ib 198.19.0.0/24 u 0.0.0.0 200 (unknown) |||>> 4 DC 198.19.0.0/24 r 0.0.0.0 0 SERVER-CORE |||>> 5 DC 192.168.1.0/24 r 0.0.0.0 0 INTERNAL-CORE |||>> 6 DC 165.146.192.1/32 r 0.0.0.0 0 National Gateway |||>> [EMAIL PROTECTED] > |||>> |||>> |||>> I want the regex to return the rule number for all route |||entries that is |||>> static (S Flag) on Interface "National Gateway". For added |||security, I'd |||>> like it if the regex will also only return true if the gateway |||address is |||>> part of 165.165.0.0/8 or 165.146.0.0/8 |||>> |||>> Basically, I need to use the rule number in another command to |||delete the |||>> route entry... So I presume I'll need to extract it via the regex |||> |||> If it's a fixed width data format, its better (easier for you, |||faster for |||> perl) to break up the string with C<substr>: |||> |||> #!/usr/bin/perl |||> |||> use strict; |||> use warnings; |||> |||> |||> while (defined( my $line = <DATA> )) { |||> chomp( $line ); |||> |||> my %data; |||> $data{rule} = substr( $line, 0, 2 ); |||> $data{flags} = substr( $line, 3, 2 ); |||> $data{dest_ip} = substr( $line, 6, 18 ); |||> $data{g} = substr( $line, 25, 1 ); |||> $data{gateway} = substr( $line, 27, 16 ); |||> $data{distance} = substr( $line, 43, 8 ); |||> $data{interface} = substr( $line, 52 ); |||> |||> # cleanup, stripping spaces |||> $data{$_} =~ s/^\s+|\s+$//g |||> for keys %data; |||> |||> # reformat or reinterpret data |||> $data{flags} = [ split( //, $data{flags} ) ]; |||> |||> use Data::Dumper; |||> print Dumper( \%data ); |||> } |||> |||> |||> |||> __END__ |||> 0 S 0.0.0.0/0 r 198.19.0.2 1 SERVER-CORE |||> 1 S 196.0.0.0/8 r 165.146.192.1 1 National Gateway |||> 2 DC 198.19.1.0/24 r 0.0.0.0 0 WIRELESS-CORE |||> 3 Ib 198.19.0.0/24 u 0.0.0.0 200 (unknown) |||> 4 DC 198.19.0.0/24 r 0.0.0.0 0 SERVER-CORE |||> 5 DC 192.168.1.0/24 r 0.0.0.0 0 INTERNAL-CORE |||> 6 DC 165.146.192.1/32 r 0.0.0.0 0 National Gateway |||> |||> ||| ||| |||-- |||To unsubscribe, e-mail: [EMAIL PROTECTED] |||For additional commands, e-mail: [EMAIL PROTECTED] |||<http://learn.perl.org/> <http://learn.perl.org/first-response> ||| ||| || || ||********************************************************* ||Disclaimer: || ||This message (including any attachments) contains ||confidential information intended for a specific ||individual and purpose, and is protected by law. ||If you are not the intended recipient, you should ||delete this message and are hereby notified that ||any disclosure, copying, or distribution of this ||message, or the taking of any action based on it, ||is strictly prohibited. || ||********************************************************* ||Visit us at http://www.mahindrabt.com || || ||-- ||To unsubscribe, e-mail: [EMAIL PROTECTED] ||For additional commands, e-mail: [EMAIL PROTECTED] ||<http://learn.perl.org/> <http://learn.perl.org/first-response> || || | | |********************************************************* |Disclaimer: | |This message (including any attachments) contains |confidential information intended for a specific |individual and purpose, and is protected by law. |If you are not the intended recipient, you should |delete this message and are hereby notified that |any disclosure, copying, or distribution of this |message, or the taking of any action based on it, |is strictly prohibited. | |********************************************************* |Visit us at http://www.mahindrabt.com | | |-- |To unsubscribe, e-mail: [EMAIL PROTECTED] |For additional commands, e-mail: [EMAIL PROTECTED] |<http://learn.perl.org/> <http://learn.perl.org/first-response> | | ********************************************************* Disclaimer: This message (including any attachments) contains confidential information intended for a specific individual and purpose, and is protected by law. If you are not the intended recipient, you should delete this message and are hereby notified that any disclosure, copying, or distribution of this message, or the taking of any action based on it, is strictly prohibited. ********************************************************* Visit us at http://www.mahindrabt.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>