Setting Interface IPs temporarily through Script

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.
Post Reply
Misam Shah
Lance Naik
Posts: 32
Joined: Mon May 04, 2009 4:08 pm
Location: Hyderabad
Contact:

Setting Interface IPs temporarily through Script

Post by Misam Shah »

AOA All,
Is there any way to set interface IPs for a temporary period by running a script.

The scenario is that


I have a Server which has two interfaces, one of them is being used for local network and the other is for establishing connectivity through VPN, but in case of the unavailability of the service for connectivity I need to switch them to another medium (PTCL Broadband) which certainly has the different IPs. Now is there any way to change them for a temporary period through a script?.

Remember I don't want the users to give administrative level so that they can do it themselves.

Waiting for the reply. Thanks.

Best Regards,
Misam Shah
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Post by lambda »

yes, you can do it. all you need to do in the script is route delete default, ifconfig eth0 whatever, route add default gw newgwip.
Remember I don't want the users to give administrative level so that they can do it themselves.
"remember"? huh? where did you originally specify it that i have to "remember" it? please fix your tone. i'm not your underling, and i don't have to "remember" any of your requirements.

install sudo, and set it up so that users can only run your script with sudo, and nothing else.
Watch out for the Manners Taliban!
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
osama
Havaldaar
Posts: 117
Joined: Fri Aug 22, 2008 9:08 am

Post by osama »

lambda wrote:
Remember I don't want the users to give administrative level so that they can do it themselves.
"remember"? huh? where did you originally specify it that i have to "remember" it? please fix your tone. i'm not your underling, and i don't have to "remember" any of your requirements.
Forgive that little Doctor for the mistake
Misam Shah
Lance Naik
Posts: 32
Joined: Mon May 04, 2009 4:08 pm
Location: Hyderabad
Contact:

Post by Misam Shah »

osama wrote:
lambda wrote:
Remember I don't want the users to give administrative level so that they can do it themselves.
"remember"? huh? where did you originally specify it that i have to "remember" it? please fix your tone. i'm not your underling, and i don't have to "remember" any of your requirements.
Forgive that little Doctor for the mistake
Well I never meant that. I'll be careful in future for the Tone as u mentioned. :oops:
And thanks alot for your reply. One more thing dear that I am a begginer of Linux and working on Ubuntu and Fedora. Its quite tough need support from all of you experts, see m just a cadet.
Hope you'll not mind because I m new to the Linux Kakool.
:( Thanks
mudasir
Captain
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Location: Dubai
Contact:

Post by mudasir »

AOA,

You can see the followuing script for example.

Code: Select all


#!/bin/bash

### uncomment the following line for debugging
#set -x

### Defining variables
## Interface connected to internet
EXTDEV=eth0

### IP to check VPN connection
VPN_TEST=10.100.150.111

### IP to set for VPN
IPVPN=10.100.1.1

### VPN Gateway
VPN_GW=10.100.150.111

### Netmask for VPN
NM_VPN=255.255.0.0

### IP to Set PTCL
IPPTCL=192.168.1.150

### PTCL Gateway
PTCL_GW=192.168.1.1

### Netmask for PTCL
NM_PTCL=255.255.255.0

########################################################
### Dont change any thing
########################################################

if [ "$1" = "sw-ptcl" ]; then
ping -c5 $VPN_TEST >> /dev/null
if [ "$?" = "0" ]; then
ifdown $EXTDEV
ifconfig $EXTDEV $IPPTCL netmask $NM_PTCL up
route add default gw $PTCL_GW $EXTDEV
echo 1 > /proc/sys/net/ipv4/ip_forward
fi
else
if [ "$1" = "sw-vpn" ]; then
ifdown $EXTDEV
ifconfig $EXTDEV $IPVPN netmask $NM_VPN up
route add default gw $VPN_GW $EXTDEV
echo 1 > /proc/sys/net/ipv4/ip_forward
fi
fi

#############################################################
To run the script.

[root@ns1]; chmod +x <NAME_OF_SCRIPT>
### to switch to PTCL
[root@ns1]; <NAME_OF_SCRIPT> sw-ptcl

### to switch to VPN
[root@ns1]; <NAME_OF_SCRIPT> sw-vpn
Note: i have not tested this script and nor do i say that this script will work 100%. This can be taken as an example to set the needed parameters.
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
Post Reply