LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-09-2014, 11:34 AM   #1
dixon464
LQ Newbie
 
Registered: Sep 2014
Posts: 3

Rep: Reputation: Disabled
Trying to extract the IP address of the default gateway


Hope you can help.

I am doing a FD in applied networking and having trouble with this line

DG= netstat -rn | grep 0.0.0.0 | awk '{print $2}' | grep -v "0.0.0.0"

ping -c 3 $DG

I am trying to extract the DG from netstat so I can ping to check it responds to ping command.

the result looks right when echo'd to the screen but when i try to ping the DG it errors, i can only think the the data in the $DG variable contains the wrong data, it there a way to clean this up?

Error message =

Usage: ping [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
[-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos]
[-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option]
[-w deadline] [-W timeout] [hop1 ...] destination

I must be doing something daft

Hope you can help

Regards
Stuart

Last edited by dixon464; 12-09-2014 at 12:05 PM.
 
Old 12-09-2014, 12:14 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Where does this "grep | awk" thing come from? I see that all the time. awk is much more powerful than grep. No need to use both.

You forgot to capture the output of the commands:
Code:
 DG=$(netstat -rn |awk '$4 ~ /G/ {print $2}')
 
Old 12-09-2014, 12:25 PM   #3
dixon464
LQ Newbie
 
Registered: Sep 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you smallpond, works a treat.
 
Old 12-09-2014, 12:25 PM   #4
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,350

Rep: Reputation: 127Reputation: 127
When you assign a variable with result of command, use "$()"
So try:
Code:
 DG=$(netstat -rn | grep 0.0.0.0 | awk '{print $2}' | grep -v "0.0.0.0") ; ping $DG
---
Edit: Solved 10 minutes ago by smallpond, didn't notice that.

Last edited by pingu; 12-09-2014 at 12:26 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Making default gateway address permanent in SuSe 10 alienmagic SUSE / openSUSE 9 01-02-2008 10:29 AM
Solaris 9 how to change ip address and default gateway rust8y Solaris / OpenSolaris 6 10-15-2007 05:44 PM
How do I set the ip address for eth0, eth1 and the default gateway? abefroman SUSE / openSUSE 5 05-19-2005 04:15 AM
Universal default gateway address resolution otisthegbs Linux - Networking 2 07-09-2004 02:28 AM
How do I get iptables to redirect my default gateway address? Paul Woodhouse Linux - Networking 3 11-11-2003 08:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration