How to make an Internet connect/disconnect script? (Solved)

Booting, installing, newbie
Message
Author
User avatar
neerajkolte
Posts: 516
Joined: Mon 10 Feb 2014, 07:05
Location: Pune, India.

How to make an Internet connect/disconnect script? (Solved)

#1 Post by neerajkolte »

Hi,
I use my droid's net on my Fatdog.
Native tool in fatdog works fine.
But I wanted to know what goes on underneth.

I searched internet found following commands...
To connect

Code: Select all

ifconfig usb0 up && dhcpcd usb0
To disconnect

Code: Select all

dhcpcd -x usb0
Are they proper way to connect and disconnect net.
I have tested a script with the connect command, it works.
I am confused about disconnect, should I add "ifconfig usb0 down" too.

And finally I would like to make a single script which when I clicked once connects to internet, on second click disconnects.
I think, I will have to use "if....fi". But I don't know how to, and what should it search for to detect if net is on.
Thanks for any help.

- Neeraj.
Last edited by neerajkolte on Sun 28 Dec 2014, 09:44, edited 1 time in total.

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

Re: Help needed in making a script

#2 Post by Moose On The Loose »

neerajkolte wrote:Hi,
I use my droid's net on my Fatdog.
Native tool in fatdog works fine.
But I wanted to know what goes on underneth.

I searched internet found following commands...
To connect

Code: Select all

ifconfig usb0 up && dhcpcd usb0
To disconnect

Code: Select all

dhcpcd -x usb0
Are they proper way to connect and disconnect net.
I have tested a script with the connect command, it works.
I am confused about disconnect, should I add "ifconfig usb0 down" too.

And finally I would like to make a single script which when I clicked once connects to internet, on second click disconnects.
I think, I will have to use "if....fi". But I don't know how to, and what should it search for to detect if net is on.
Thanks for any help.

- Neeraj.

Code: Select all

ip addr
lists the network connections you have. I have parsed it in the past to obtain the IP address and to know that the contact to the DHCP server has been made.

User avatar
neerajkolte
Posts: 516
Joined: Mon 10 Feb 2014, 07:05
Location: Pune, India.

#3 Post by neerajkolte »

Thanks for the reply.
I searched internet and got these 3 commands which will tell me my IP

Code: Select all

#ip addr show usb0
#ifconfig usb0 | grep 'inet addr:'
#hostname -I
I haven't tested them yet. I will do when I get home from work.


If I get this correctly, I have to do

Code: Select all

if
  Condition
;
then
      command
else
      command
fi
Am I right.
What should I enter as condition. How to tell the code to take output of IP finding commands as a condition. I have never done any scripting.

Thanks for your help.

- Neeraj.
"One of my most productive days was throwing away 1000 lines of code."
- Ken Thompson

“We tend to overestimate the effect of a technology in the short run and underestimate the effect in the long run.â€￾
- Amara’s Law.

User avatar
neerajkolte
Posts: 516
Joined: Mon 10 Feb 2014, 07:05
Location: Pune, India.

#4 Post by neerajkolte »

OK I found this script to check network connectivity (the comments are added by me to remember what it does)

Code: Select all

#!/bin/sh
### A script to check if internet is available or not ###
# -q : silent mode            --spider : don't get just check availability
# $? : shell return code             0 : shell "All OK" code
#
wget -q --tries=10 --timeout=20 --spider http://google.com
if [ $? -eq 0 ];
	then
		echo "online"
	else
		echo "offline"
fi
It worked ok.
So I edited it like

Code: Select all

#!/bin/sh
# -q : silent mode            --spider : don't get just check availability
# $? : shell return code             0 : shell "All OK" code
### A script to check if internet is available or not ###
#
wget -q --tries=10 --timeout=20 --spider http://google.com
if [ $? -eq 0 ];
	then
		dhcpcd -x usb0 && ifconfig usb0 down
	else
		ifconfig usb0 up && dhcpcd usb0
fi
This works.
But I would like to know how to do it with "ip addr", what @Moose On The Loose told me to use.
Would simple shell "All OK" return code be used like in this script.
Or do I have to grep 'inet ' line and do something with it?

Thanks in advance.

- Neeraj.
"One of my most productive days was throwing away 1000 lines of code."
- Ken Thompson

“We tend to overestimate the effect of a technology in the short run and underestimate the effect in the long run.â€￾
- Amara’s Law.

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

#5 Post by Moose On The Loose »

neerajkolte wrote:
.....
But I would like to know how to do it with "ip addr", what @Moose On The Loose told me to use.
Would simple shell "All OK" return code be used like in this script.
Or do I have to grep 'inet ' line and do something with it?

Thanks in advance.

- Neeraj.

Code: Select all

THING=$( ip addr )
echo "$THING"
notice that the variable THING contains the result from the "ip addr" command.

Code: Select all

echo "$THING" | grep "eth[0,1,2,3]:"
notice that grep will find "eth" followed by a digit and colon

Code: Select all

 
echo "$THING" | grep "eth[0,1,2,3]:" | grep ",UP,"
notice we can look for the string ",UP,"

Code: Select all

if (echo "$THING" | grep -q "eth[0,1,2,3]:" | grep ",UP,") ; then
  echo "network is up"
else
  echo "no network"
  fi
[/quote]

"grep -q" does the search but doesn't output.
It sets the success or fail condition
if tests for success

User avatar
neerajkolte
Posts: 516
Joined: Mon 10 Feb 2014, 07:05
Location: Pune, India.

#6 Post by neerajkolte »

Thanks Moose for the code and it's detailed explanation.
But when I made script like this

Code: Select all

#!/bin/sh
THING=$( ip addr )
if (echo "$THING" | grep -q "usb[0,1,2,3]:" | grep -q ",UP,") ; then
echo "network is up"
else
echo "no network"
fi
It would return "no network" even when net was up.
so I removed the -q, so that I could see what is happening. It outputted "usb0:" line and "network is up".
So I thought may be as first grep is quieted with -q, the second grep couldn't get anything.
So I used -q on second grep like..

Code: Select all

#!/bin/sh
THING=$( ip addr )
if (echo "$THING" | grep "usb[0,1,2,3]:" | grep -q ",UP,") ; then
echo "network is up"
else
echo "no network"
fi
This works perfectly.
I will study more.
Now I am looking at xmessage to output on screen Connected and Disconnected when I click on script.

This scripting thing is fun.
But I am not getting any time to read. I will try reading bit by bit each day.

Again thanks for all your help.

- Neeraj.
"One of my most productive days was throwing away 1000 lines of code."
- Ken Thompson

“We tend to overestimate the effect of a technology in the short run and underestimate the effect in the long run.â€￾
- Amara’s Law.

User avatar
mikeb
Posts: 11297
Joined: Thu 23 Nov 2006, 13:56

#7 Post by mikeb »

So you are code grabbing..... in that case this is a loop i use to wait for the precense of a net connection and get the ip of the machine for networking.... just as example of other ways to get similar results....

Code: Select all

while [ "$SUBNET" = "" ]; do
	SUBNET=$(ifconfig | grep inet | grep -v 127.0.0.1 | awk '{print $2}' | cut -d ':' -f 2)
	sleep 20
done
Just uses ifconfig but more example of extracting information.

Your script is doing what ifplugd does in slax... it monitors an interface , gets an ip and reconnects if the connection is lost...eg cable is unplugged or wifi drops...in that case its a c binary.

mike

User avatar
neerajkolte
Posts: 516
Joined: Mon 10 Feb 2014, 07:05
Location: Pune, India.

#8 Post by neerajkolte »

Thanks Mike,
I will look at that code.

- Neeraj.
"One of my most productive days was throwing away 1000 lines of code."
- Ken Thompson

“We tend to overestimate the effect of a technology in the short run and underestimate the effect in the long run.â€￾
- Amara’s Law.

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

#9 Post by Moose On The Loose »

neerajkolte wrote:Thanks Moose for the code and it's detailed explanation.
But when I made script like this

Code: Select all

#!/bin/sh
THING=$( ip addr )
if (echo "$THING" | grep -q "usb[0,1,2,3]:" | grep -q ",UP,") ; then
echo "network is up"
else
echo "no network"
fi

:oops: :oops: :oops: :oops: :oops:
I put the "-q" in the wrong place

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

#10 Post by Moose On The Loose »

mikeb wrote:So you are code grabbing..... in that case this is a loop i use to wait for the precense of a net connection and get the ip of the machine for networking.... just as example of other ways to get similar results....

Code: Select all

while [ "$SUBNET" = "" ]; do
	SUBNET=$(ifconfig | grep inet | grep -v 127.0.0.1 | awk '{print $2}' | cut -d ':' -f 2)
	sleep 20
done
Just uses ifconfig but more example of extracting information.

Your script is doing what ifplugd does in slax... it monitors an interface , gets an ip and reconnects if the connection is lost...eg cable is unplugged or wifi drops...in that case its a c binary.

mike
I don't see that the awk part of that does anything that matters to the function.

Code: Select all

ifconfig | grep inet | grep -v 127.0.0.1 | cut -d ":" -f 2| cut -d '.' -f 1
or the like looks like enough for the tests needed here.


I typically just put a

Code: Select all

sleep 1
COUNT=$(( $COUNT + 1 ))
if (( COUNT > COUNTLIMIT )) ; then
  while at the user
  perhaps exit
  fi
in to prevent looping forever without giving up if it is hopeless

User avatar
mikeb
Posts: 11297
Joined: Thu 23 Nov 2006, 13:56

#11 Post by mikeb »

I don't see that the awk part of that does anything that matters to the function
.
no its just for the op to play with rather than a specific answer.

The code in question grabs the the subnet so that mpscan can use it to scan to detect live NFS shares to populate the NFS display. It also works as a keep alive to keep the wireless router happy.

mike

User avatar
neerajkolte
Posts: 516
Joined: Mon 10 Feb 2014, 07:05
Location: Pune, India.

#12 Post by neerajkolte »

Sorry mike and moose, will test later.
Down with fever.
Pulled something in back too while solving a breakdown at work.
Got to take rest.
Thanks.

- Neeraj.
"One of my most productive days was throwing away 1000 lines of code."
- Ken Thompson

“We tend to overestimate the effect of a technology in the short run and underestimate the effect in the long run.â€￾
- Amara’s Law.

User avatar
mikeb
Posts: 11297
Joined: Thu 23 Nov 2006, 13:56

#13 Post by mikeb »

No apologies needed...and by the sounds of it you need to take it easy

mike

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

#14 Post by Moose On The Loose »

mikeb wrote:
I don't see that the awk part of that does anything that matters to the function
.
no its just for the op to play with rather than a specific answer.

The code in question grabs the the subnet so that mpscan can use it to scan to detect live NFS shares to populate the NFS display. It also works as a keep alive to keep the wireless router happy.

mike
That explains why I could see that it did anything :)

I also have used a script like this to create a web page on the fly that contains the IP address of the machine and fire up hiawatha with it serving up that page and also put an HTML document into something that can be seen on pnethood that had a link to it. This way, a person (lets say wife) looking at my machine can see the HTML on the Windows box and via SAMBA then get to the served up web page.

User avatar
mikeb
Posts: 11297
Joined: Thu 23 Nov 2006, 13:56

#15 Post by mikeb »

okay dokay

though could she not just open the file directly or does samba not work like that?

mike

slavvo67
Posts: 1610
Joined: Sat 13 Oct 2012, 02:07
Location: The other Mr. 305

#16 Post by slavvo67 »

So, here I am wondering why the above isn't working for me. Then, the lights came on. I'm connecting wireless (WLAN). Here's my amendment:

#!/bin/sh
THING=$( ip addr )
if (echo "$THING" | grep "eth[0,1,2,3]:" | grep -q ",UP,") ; then
echo "ETH is up"
elif (echo "$THING" | grep "wlan[0,1,2,3]:" | grep -q ",UP,") ; then
echo "WLAN is up"
else
echo "No Internet"
fi

User avatar
mikeb
Posts: 11297
Joined: Thu 23 Nov 2006, 13:56

#17 Post by mikeb »

so you woke up in yer cornflakes and smelt the coffee :D

I get that a lot and I don't eat breakfast cereals...

User avatar
neerajkolte
Posts: 516
Joined: Mon 10 Feb 2014, 07:05
Location: Pune, India.

#18 Post by neerajkolte »

mikeb wrote:

Code: Select all

while [ "$SUBNET" = "" ]; do
	SUBNET=$(ifconfig | grep inet | grep -v 127.0.0.1 | awk '{print $2}' | cut -d ':' -f 2)
	sleep 20
done
Just uses ifconfig but more example of extracting information.
Your script is doing what ifplugd does in slax... it monitors an interface , gets an ip and reconnects if the connection is lost...eg cable is unplugged or wifi drops...in that case its a c binary.
mike
Hi mike I made a script with this and ran it in term.
It waits for few seconds and outputs nothing, then gives next # prompt.
I thought awk will give me some output on command line.
The ifconfig | grep inet | grep -v 127.0.0.1 line when ran individually does gets my IP.
What does the "cut -d ':' -f 2" does?
Thanks.

- Neeraj.
"One of my most productive days was throwing away 1000 lines of code."
- Ken Thompson

“We tend to overestimate the effect of a technology in the short run and underestimate the effect in the long run.â€￾
- Amara’s Law.

User avatar
mikeb
Posts: 11297
Joined: Thu 23 Nov 2006, 13:56

#19 Post by mikeb »

its part of a larger script...just dumps the subnet into the variable SUBNET .... the cut is to remove the last digit because the first 3 denote the subnet and th elast the specific machine...since I am scanning for whatever machines are present I need the subnet to built the command line for mpscan which uses

mpscan -p 111 -t 1 $SUBNET.2 - $SUBNET.254 2>/dev/null | awk '{print $2}' > /tmp/NFS_servers

should make more sense now....

again its just an example of manipulating network info using common tools.

mike

User avatar
neerajkolte
Posts: 516
Joined: Mon 10 Feb 2014, 07:05
Location: Pune, India.

#20 Post by neerajkolte »

Ohh OK thanks Mike.

Thanks slavvo67 for reminding me.
Hopefully I will be getting wired net this new year.
Three of my neighbors in my apartment building and me are going to team up and share an internet connection. It's 15Mbps line which will cost us 24$ per month, thats 6$ for each person.
I hope they give us the promised speed.

Thanks.

- Neeraj.
"One of my most productive days was throwing away 1000 lines of code."
- Ken Thompson

“We tend to overestimate the effect of a technology in the short run and underestimate the effect in the long run.â€￾
- Amara’s Law.

Post Reply