Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 04-01-2018, 04:28 PM   #1
Registered: Aug 2014
Distribution: Devuan
Posts: 145

Rep: Reputation: Disabled
Connected directly to the web or behind a router

I was looking for a solution on the web , but then a light popup on my head and i found the solution for those that may need it .
A simple bash script that identifies if your current connection is behind a router or directly exposed to the web .

Basically , the scripts asks opendns for your public ip address , and then compares if your lan ip matches your public ip .
If it matches then you are connected directly to the web , else you are behind a router connected to the web.

lanip=`ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'`
publicip=`dig +short`
if [[ $publicip == $lanip ]]
echo "connected directly to the web"
echo "behind a router"
Note , i just leaved this code here in case anyone needs it .

Last edited by pedropt; 04-01-2018 at 06:48 PM.
Old 04-03-2018, 06:57 AM   #2
Senior Member
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 3,272
Blog Entries: 3

Rep: Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445Reputation: 1445
You can use plain sed for that.

ip addr | sed -n '/state UP/ { n; n; s/^.*inet //; s/\/.*$//; p; q; }'
Or use plain awk for that too.

ip addr | awk '/state UP/ { getline; getline; sub(/\/.*$/, "", $2); print $2; }'
But all that assumes that you have only one external address or interface.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Two computers on the same network, but also directly connected VolumetricSteve Linux - Networking 1 04-26-2017 02:21 AM
[SOLVED] Two computers connected directly, one us router for the other - problem config mitusf Slackware 10 06-27-2012 01:49 PM
PC connected to netgear router can't see web Streagan2 Linux - Networking 3 09-29-2006 04:24 AM
access web on a laptop connected to a web-connected desktop bluesmanu Linux - Networking 2 04-16-2006 12:38 PM
NEC Superscript 1800N connected directly to the router wapworld Linux - Hardware 0 06-15-2005 09:54 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:14 AM.

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