How do i get the default gateway in LINUX given the destination?

前端 未结 15 2151
清酒与你
清酒与你 2020-12-13 07:02

I\'m trying to get the default gateway, using the destination 0.0.0.0

I used this command: netstat -rn | grep 0.0.0.0

And it return

15条回答
  •  一个人的身影
    2020-12-13 07:20

    This simple perl script will do it for you.

    #!/usr/bin/perl
    
    $ns = `netstat -nr`;
    
    $ns =~ m/0.0.0.0\s+([0-9]+.[0-9]+.[0-9]+.[0-9]+)/g;
    
    print $1
    

    Basically, we run netstat, save it to $ns. Then find the line that starts off with 0.0.0.0. Then the parentheses in the regex saves everything inside it into $1. After that, simply print it out.

    If it was called null-gw.pl, just run it on the command like:

    perl null-gw.pl
    

    or if you need it inside a bash expression:

    echo $(perl null-gw.pl)
    

    Good luck.

提交回复
热议问题