LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Help needed for simple IP reset script (https://www.linuxquestions.org/questions/linux-networking-3/help-needed-for-simple-ip-reset-script-4175416994/)

Joeblah 07-16-2012 10:31 AM

Help needed for simple IP reset script
 
Hello,
Alright so I am more of a Windows guy still trying to learn Linux.
I need to create a simple script to reset the IP and check it.

These are what I was hoping to run, just when I tried to create my own script, it fails.

I know this is not right but I was trying, can someone can please help me with creating this properly. I was wondering if it is also possible just to display an IFCONFIG on eth0?

Code:

#!/bin/bash
clear
#
/etc/init.d/networking restart
#
ifconfig


zhjim 07-16-2012 10:36 AM

You don't need to call echo in front of ifconfig.
Just run ifconfig and it presents it output magicaly ;)

What are you trying to achive? Comparing the two outputs before and after?

Just a little hint. GEt familiar with the ip command. It's the new way of doing things.

Joeblah 07-16-2012 10:46 AM

Okay, sorry I should of explained a little better.
It's a little Eee PC machine I use for work to test connectivity through ports and firewalls at remote sites. However I did not originally set up the machines so not everything that we would like is installed. Example, vi will not open an editor, had to use gedit.

They were given static addresses to connect through a 3002 VPN. Some locations like Hotels do not allow you access until you accept their TOA. Now I had to manually do this with a customer and it was annoying to constantly have him type and hit the Up arrow for commands.
So I changed it to DHCP but I need to be able to reset the IP of eth0 between a privately given IP from the hotel and the one the 3002 VPN wants to give it (172.31.1.2).

I would also like to try and display the IP so that we can confirm what it is currently at. It's difficult to try and correct this when I am unsure of what I am doing still.

I did try at first without the Echo but I failed to even see a terminal appear or anything.

zhjim 07-16-2012 10:53 AM

Okay. First thing is that linux is case sensitive. As you have Echo in your line 4 which gives
Code:

./trial.sh: line 4: Echo: command not found
just use echo with all lower chars.

Another thing is that you don't need to use echo in front of commands they print what they print to the output. echo is for output of message for the user like
Code:

echo 'Restarting network'
/etc/init.d/networking restart
echo 'Showing IP settings'
ip addr


Joeblah 07-16-2012 11:15 AM

Okay, So the only thing I am having an issue with is trying to print this for the user to see what is going on. It happens in a flash and I remember how to do it in a batch file but not a shell script

Code:

#!/bin/bash
clear
#
echo 'Restarting Network'
/etc/init.d/networking restart
echo 'Displaying IP Address'
ip addr show eth0

zhjim,
I want to thank you for the taking the time to help me with this. I know its trivial for you but it is also what makes me want to get into linux when I know people are willing to help through all the entry level questions.

zhjim 07-16-2012 11:27 AM

Try this one
Code:

#!/bin/bash
clear
#
echo 'Restarting Network'
/etc/init.d/networking restart
echo 'Displaying IP Address'
ip addr show eth0 | grep inet | awk '{print $2 }'
read

'|' is the pipe symbol and forwards output to next commands input
grep searches for a string. I guess you only want the ip address
awk is for printing stuff nice and other things. We want the second field which holds the ip.
read just waits for input. This makes sure the window stays open.

You get the ipv4 as well as the ipv6 address. Suite to your likings.



Quote:

Originally Posted by Joeblah (Post 4729641)
zhjim,
I want to thank you for the taking the time to help me with this. I know its trivial for you but it is also what makes me want to get into linux when I know people are willing to help through all the entry level questions.

You're welcome. We all started somewhere. I'd say on linuxquestions you'll find a lot of helpful people as long as you adopt some simple rules. Just like in any forum. Be nice, be as explicit as it can be, and have a crate of beer in reach ;)
If you feel so you can click the "Was that post helpfull" to fill my karma a bit.


All times are GMT -5. The time now is 09:55 PM.