LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 04-01-2018, 04:28 PM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 185

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.

Code:
#!/bin/bash
lanip=`ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'`
publicip=`dig +short myip.opendns.com @resolver1.opendns.com`
if [[ $publicip == $lanip ]]
then
echo "connected directly to the web"
else
echo "behind a router"
fi
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
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 3,441
Blog Entries: 3

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

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

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


Reply


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

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

All times are GMT -5. The time now is 01:55 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration