Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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.
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.
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.
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.
#!/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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.