LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-29-2010, 04:15 AM   #1
ytd
Member
 
Registered: Jan 2009
Posts: 197

Rep: Reputation: 31
script


Hello everyone


I want to build a script that verifys the gateway and when it's down to change it automatically to let's say 172.16.1.1

Can anyone help me, please?
tyvm
 
Old 12-29-2010, 04:30 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hi,

I don't think you'll find any LQ user willing to do the work for you. You'll have to put in some of your time and show us what you already have. Then we'll take it from there to try and solve your issue. First of all do you have knowledge of Bash, Perl, Python or any other scripting language that you would prefer to use? If so, post what you figured out already.

Kind regards,

Eric
 
1 members found this post helpful.
Old 12-29-2010, 04:41 AM   #3
ytd
Member
 
Registered: Jan 2009
Posts: 197

Original Poster
Rep: Reputation: 31
I don't know any scripting language.
Al I know is that I can:
vi script.sh
chmod +x script.sh
chmod 777 script.sh
vi script.sh
!#/bin/bash
and then I write some arguments

if [ ! -f $ ]
then

else

then

ping 172.16.1.1
route add default gw 172.16.1.1

But I don't know the script language.
I'm not asking someone to do my work, in less than 15 seconds you can do this script, but I don't know and I don't know where do find. I don't know from where to start.
 
Old 12-29-2010, 04:53 AM   #4
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hello,

Fair enough! But if you say you want to build a script, then at least you need to understand 'the nature of the beast' and start learning. Walk before trying to run, no offense.

To check if a host is alive you can use ping -c 1 <IP>. If the result of the command is 0 (zero) then the host is alive, if it is 1 (one) then the host is not responding to your ping (watch out if you try this on a network where ICMP is blocked since it will always return failure).

Also if you're changing a default gateway you'll need to reload/restart networking. To avoid problems you'll need to delete the 'unavailable' gateway, set the new one and reload/restart your networking and if you have more then one NIC you'd better indicate it when setting a route.

If you indicate what distro you are using we'll know where to start.

In the meanwhile, if you feel like reading start with these links to learn Bash:
Bash Beginners Guide
Advanced Bash Scripting Guide

Kind regards,

Eric
 
Old 12-29-2010, 04:53 AM   #5
GlennsPref
Senior Member
 
Registered: Apr 2004
Location: Brisbane, Australia
Distribution: pclinuxos slackware64 tails kali
Posts: 3,372
Blog Entries: 33

Rep: Reputation: 217Reputation: 217Reputation: 217
Smile

Quote:
Originally Posted by ytd View Post
I don't know any scripting language.
Al I know is that I can:
vi script.sh
chmod +x script.sh
chmod 777 script.sh
vi script.sh
!#/bin/bash
and then I write some arguments

if [ ! -f $ ]
then

else

then

ping 172.16.1.1
route add default gw 172.16.1.1

But I don't know the script language.
I'm not asking someone to do my work, in less than 15 seconds you can do this script, but I don't know and I don't know where do find. I don't know from where to start.
feeder page to some bash tutes.

http://www.linuxconfig.org/Bash_scripting_Tutorial

hang on a sec!

Last edited by GlennsPref; 12-29-2010 at 04:56 AM. Reason: EricTRA got here befor me...
 
1 members found this post helpful.
Old 12-29-2010, 04:58 AM   #6
ytd
Member
 
Registered: Jan 2009
Posts: 197

Original Poster
Rep: Reputation: 31
Let's say ubuntu 32 bit. v 10.04

That command it's good because it stops after 1 ping/reply.

So in the script I say:

ping -c 1 172.16.1.1
then argument:
if what?
I know that I need to restart networking, it's np. I will test the script more times and I will adapt it but I first need to build up the script. Please help.
 
Old 12-29-2010, 05:06 AM   #7
GlennsPref
Senior Member
 
Registered: Apr 2004
Location: Brisbane, Australia
Distribution: pclinuxos slackware64 tails kali
Posts: 3,372
Blog Entries: 33

Rep: Reputation: 217Reputation: 217Reputation: 217
Wink

Is there any way you as a user knows when the network is down? (unless you invoke it, like refreshing a web-page)

Like knotify? (kde env)

if so you maybe able to add an instruction (read a script, aka macro) to restart the network service when ever it disconnects.

Never tried, but the method is there. and I know I can make an alert sound for whenever the network dis/connects.

See how you go, regards Glenn
 
Old 12-29-2010, 05:07 AM   #8
Paul G.
LQ Newbie
 
Registered: May 2006
Location: Auckland, New Zealand
Distribution: ArtistX 0.9
Posts: 7

Rep: Reputation: 0
Quote:
Originally Posted by ytd View Post
But I don't know the script language.
I'm not asking someone to do my work, in less than 15 seconds you can do this script, but I don't know and I don't know where do find. I don't know from where to start.
Google is your friend!

"How to write a script" +Linux

First item found, How to Write a Script in Linux

Suggested link from there, How to Write in Shell Script

Between these two short how-tos you should be well on the way.

When you run into a difficulty, post your script here and ask for advice/help and you are more likely to receive a sympathetic response.

I suggest that you start with writing a simple script, get it working, then move on to a more difficult one. Small steps are normally easier than large ones.

Paul
 
Old 12-29-2010, 05:15 AM   #9
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Quote:
Originally Posted by ytd View Post
Let's say ubuntu 32 bit. v 10.04

That command it's good because it stops after 1 ping/reply.

So in the script I say:

ping -c 1 172.16.1.1
then argument:
if what?
I know that I need to restart networking, it's np. I will test the script more times and I will adapt it but I first need to build up the script. Please help.
Hi,

OK, so you got the first part. To check the result of the last command executed you check the value of variable $?. So:
Code:
ping -c 172.16.1.1
if [[ $? -eq 1 ]];
then
echo "Gateway not reachable, changing..."
command1
command2
fi
The above if test checks if the exit status of the ping command was 1. If it is that means that the ping got no response, otherwise it would be 0 for success. The echo command prints out to screen and following command1 and command2 will be executed.

So now you'll need to fill in the commands to change the gateway (delete old one, add new one) and restart networking.

Kind regards,

Eric
 
Old 12-29-2010, 10:16 AM   #10
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,143

Rep: Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877
Quote:
Originally Posted by GlennsPref View Post
Is there any way you as a user knows when the network is down? (unless you invoke it, like refreshing a web-page)

Like knotify? (kde env)

if so you maybe able to add an instruction (read a script, aka macro) to restart the network service when ever it disconnects.

Never tried, but the method is there. and I know I can make an alert sound for whenever the network dis/connects.

See how you go, regards Glenn
it would be probably smart to demon-ize the script by adding a while loop and sleeping for a minute.
 
Old 01-06-2011, 02:10 AM   #11
ytd
Member
 
Registered: Jan 2009
Posts: 197

Original Poster
Rep: Reputation: 31
Ok so it would be something like this:


#!/bin/bash
ping -c 1 172.16.1.1
if [[ $? -eq 0 ]];
then
echo "Gateway is up and running"

if [[ $? -eq 1 ]];
then
echo "Gateway not reachable, changing..."
route add default gw 172.16.1.1
/etc/init.d/networking restart
fi

And I have one more question.
If I want to expand or replace the name of a variable with its value, I will use what? " " or ' ' or ` ` or what?

Last edited by ytd; 01-06-2011 at 02:53 AM.
 
Old 01-06-2011, 02:34 AM   #12
ytd
Member
 
Registered: Jan 2009
Posts: 197

Original Poster
Rep: Reputation: 31
I just did a quick test, didn't know how to do it but I've found out.



#!/bin/bash
VAR=7
echo "$VAR"

and then


#!/bin/bash
VAR=7
echo '$VAR'

and seems like with " " you can expand or replace the name of a variable with its value.

I'm still waiting for an answer, ty.
 
Old 01-06-2011, 03:41 AM   #13
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Quote:
Originally Posted by ytd View Post
Ok so it would be something like this:


#!/bin/bash
ping -c 1 172.16.1.1
if [[ $? -eq 0 ]];
then
echo "Gateway is up and running"

if [[ $? -eq 1 ]];
then
echo "Gateway not reachable, changing..."
route add default gw 172.16.1.1
/etc/init.d/networking restart
fi

And I have one more question.
If I want to expand or replace the name of a variable with its value, I will use what? " " or ' ' or ` ` or what?
Hello,

The above structure will give you an error since you started two 'if' and there's only one 'fi'. Better to include it in one 'if' like this:
Code:
#!/bin/bash
ping -c 1 172.16.1.1
if [[ $? -eq 0 ]];
then
     echo "Gateway is up and running";
else
echo "Gateway not reachable, changing..."
route add default gw 172.16.1.1
/etc/init.d/networking restart
fi
The above if test checks if the ping command was successful (0) and does nothing else then print a message. If, on the other hand, the exit status is not 0, then it'll print out the message, add the default gateway and restart networking. But....

What happens with the route that points to the 'unreachable' gateway? Did you think about that? Try it out, see where you go and what errors you get, if any.

Great you found out about the quotation, single and double, in Bash. That's the best way to learn, trial and error.

Kind regards,

Eric
 
  


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
How to execute a ssh script on Linux server from Windows through a bat script? wanna13e Programming 13 10-23-2009 02:41 AM
Need help with script to organise files into folders as part of DVD backup script jasybee2000 Linux - Newbie 5 06-15-2009 07:29 PM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 08:40 AM
Shell Script: want to insert values in database when update script runs ring Programming 2 10-25-2007 10:48 PM
linux 9 and java script error - premature end of script header sibil Linux - Newbie 0 01-06-2004 04:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:29 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