How to get pppd inet address from shell command - linux

I'm not sure whether to post it here or at ServerFault.
Anyway, I'm trying to work around company's firewall to connect to some media sharing site using my phone's 3g network. I've come up with a simple ip route command which take pppd's inet address as it's parameter. But, I want to make it a little bit more automated by reading the inet address right from the script, not by passing it via command line parameter.
Here's the scenario, to make it more obvious:
The command invocation as of now: $jumpfirewall xxx.xxx.xxx.xxx
The command invocation I want: $jumpfirewall
Do you know some command or library that I can use to read it from command line?

Adapted from cyberciti:
/sbin/ifconfig ppp0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
The ifconfig ppp0 will get information for your primary PPP interface; the grep cuts it down to the line containing the IP address; the cut splits out everything after inet addr: up to bcast:, giving something like 1.2.3.4 Bcast:; and the awk call will print only the first (space-separated) field, leaving you with only the IP address.

pppd automatically calls a script in /etc/ppp/ip-up when a link is brought up. In this script, $4 is the local IP address of the PPP link. (On some distributions, /etc/ppp/ip-up is set to call the scripts in /etc/ppp/ip-up.d, with $PPP_LOCAL set to the IP address, so you can place your script there).
This way, you won't have to manually call the script - just bring up the PPP link and it'll be run automatically. There's a corresponding /etc/ppp/ip-down you can use to undo your route when the link goes down.

Related

How to get all the IP Interface names and addresses using /proc on Linux?

I know of ls /sys/class/net to get all the available IP interface names, and also cat /proc/net/fib_trie to get all the IP addresses, but how do I match between them?
My desired result is a list of IP interface names and the IP address assigned to every interface name, similar to the info showed by ifconfig but that can be applied on any Linux distribution.
for example:
enp4s0f1 5.6.7.1
enp6s0 2.2.2.1
My desired result is a list of IP interface names and the IP address assigned to every interface name, similar to the info showed by ifconfig but that can be applied on any Linux distribution.
Try this:
ip addr | grep inet | grep -v "inet6"
Using the ip system utility. You're not using /proc/ or /sys/, but it'll work on any distro with ip on it, which is most of them.
Update: to make it look a bit easier on the eye, use this:
ip addr | grep inet | grep -v "inet6" | awk '{print $2 " " $8}'

Why this function in bashrc Dosnt work?

I am trying to get this function to file .bashrc
This is my function :
function my_ip() {
echo "internal or external?"
read -r choise
if [ "$choise" == "internal" ] ;then
echo "please enter the name of the card that youare using at";
read -r card ;
ifconfig "$card" | grep 'inet addr' |cut -d':' -f2|cut -d ' ' -f1;
else
wget -qO- http://noc.co.il |grep "var VisitorCountry" | awk '{print$4}'|cut -d '"' -f4;
fi
}
My goal is that the function will quickly give me my IP by choosing internal or external
if I want to automatically displays the network card ("without the need to ask Which network card do you use(The computer automatically detects which network card the user uses and will put the name as a variable at the right commend for internal IP , how can I get him to identify what card the user use(WLAN0 WLAN1 etc.) ?
A system can have more than one IP address. Which one of them is the one you are looking for? You did not specify that.
There are multiple questions related to finding IP addresses with bash:
Find internal IP address
The accepted answer uses a deprecated option for hostname; the second answer is the more correct one IMHO.
In bash, is there a way to find the IP address for all interfaces?
Find IP address for my system
Bash script to find all IP addresses
If none of those float your boat, consider closing this question and posting a new one with more precise phrasing what you are looking for.

Is it possible to run command "route -n" specifically for a NIC

I am fairly new to linux I wanted to ask if its possible in linux commands to run a "route -n" command to retrieve information for a specific NIC. E.G route -n ether0. Because currently it shows me for all the NIC's but what if I want just for one?
Use the iproute2 ip command (rather than the antique route command), and provide a selector with your NIC:
# for ether0
ip -o route list dev ether0
(I've added -o since your tags indicate that you're using this for scripting purposes; ensuring that each result lives on its own line is appropriate in this case).
This is a significant improvement on the simple route | grep ether0 approach, as it shows routing entries which can end up sending traffic through a NIC but don't name that NIC explicitly.
As the default for iproute2 is to avoid leaning on the resolver, no local flag equivalent to -n is necessary; instead, if you did want to use the resolver, you would need to add -r.
Do you want see route for ether0, you can use below command
ip -o route show dev ether0
or
ip route show | grep ether0
or
route -n | grep ether0
or
netstat -nr | grep ether0
To see NIC for ether0, you can use ifconfig command and Gateway NIC you can use arp -a commands
ifconfig ether0
for see GW and direct connected NICS you can use below command
arp -a

Get only the source MAC address from tcpdump

I am trying to get the source MAC address of every packet being dumped on the network, excluding any packets involving the host machine. I expect that in order to accomplish this I should get the data from tcpdump with the host's network interface in promiscuous mode.
Note that I am not interested in getting the full header or even the link level header. The -e option is not what I want. I just want the source MAC address for each packet, and nothing more.
This is what I am currently doing right now:
sudo tcpdump -I -elt -i wlan0 not host 127.0.0.1 2>> /dev/null | sed 's/ .*//'
with 127.0.0.1 replaced with the actual IP address of the local network interface.
This works great in some networks, where the source MAC address is the first piece of information that is output by tcpdump. Unfortunately, this has not always been my experience. It seems that the output to tcpdump is protocol-dependent and so on some networks it varies.
I suppose I could rewrite my sed command so that it outputs the first item that matches the regex for a MAC address:
(?:[0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}
but I am unsure if the first MAC address in the line will always be the source MAC address.
If there's no way to have tcpdump output the source MAC address directly, is there some way I could have it output the raw bits from the link level header? From there I should be able to piece together the source MAC address.
by using tshark you can do it like that:
example:
tshark -i eth0 -e eth.src -Tfields

Is it possible to detect local IP address and store it as a variable in linux script

I discovered only last week with help on here that I could have the OS Architecture (32/64) stored in a variable like this:
arch=$(getconf LONG_BIT)
I am hoping that I can use a similar method for local IP address:
ipaddress=$(????)
So that any occurrences of $ipaddress will be replaced with "192.168.1.100" or whatever the local IP might be
Unfortunately, it is not possible to extract the IP address of the system from the sysconf sub-system.
It is possible to have several IP addresses associated with the interfaces on a Linux system so there is no way to determine which IP address is the 'correct' one.
You could use the following shell snippet to list the active IP addresses on the system:
ifconfig | grep 'inet addr' | awk '{print $2}' | cut -d ':' -f2
On my system this lists the following IP addresses:
192.168.1.12
127.0.0.1
If you see similar output, it means you've only got a single active interface along with the local loopback, so you can add |grep -v 127 to the previous snippet to filter out the local loopback address.

Resources