Solved

How To Activate An IP ALIAS from Bash Script

Posted on 2009-04-01
6
1,136 Views
Last Modified: 2013-12-26
I am trying to bring an IP Alias online from a bash script and am having issues connecting to the IP once it has been "up'ed"  Here is the command I am using from the Bash Script

/sbin/ifconfig eth0:1 XXX.XXX.XXX.XXX/28 broadcast YYY.YYY.YYY.YYY up

In the above command X's and Y's are used to mask the real IPs.

Now, what I am wondering is if the problem is a missing "gateway", the IP Address assigned to eth0, and the one assigned to eth0:1 are on different subnets with different gateways.  So I am wondering if the issue here is a missing route (gateway), and if so how do I go about adding it.

I am not getting any errors, so my syntax is correct, I just cant connect to this IP once its been brought online.  Any ideas for me?   Thanks in advance.
0
Comment
Question by:purestealth
  • 2
  • 2
  • 2
6 Comments
 
LVL 7

Expert Comment

by:computerfixins
ID: 24040992
Well you can only have one default route on a linux box, but you can add static routes for multiple networks...

What's the output from the route command, look as expected?
ie:
#route
[root@cricket install_tools]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.64       *               255.255.255.224 U     0      0        0 ethnzmr
10.0.0.128      *               255.255.255.192 U     0      0        0 ethnzmr
192.168.0.0     *               255.255.255.0   U     0      0        0 eth3
13.13.13.0      *               255.255.255.0   U     0      0        0 eth13
172.16.100.0    *               255.255.252.0   U     0      0        0 eth2
10.0.0.0        *               255.255.0.0     U     0      0        0 bond0
default         172.16.101.1    0.0.0.0         UG    0      0        0 eth2


from linux route man page:

       route add -net 192.56.76.0 netmask 255.255.255.0 dev eth0
              adds  a  route to the network 192.56.76.x via "eth0". The Class C netmask modifier is not really necessary
              here because 192.* is a Class C IP address. The word "dev" can be omitted here.

       route add default gw mango-gw
              adds a default route (which will be used if no other route matches).  All packets using this route will be
              gatewayed  through "mango-gw". The device which will actually be used for that route depends on how we can
              reach "mango-gw" - the static route to "mango-gw" will have to be set up before.
0
 
LVL 13

Accepted Solution

by:
dhoffman_98 earned 400 total points
ID: 24041058
Well, for one thing, you don't need to use "broadcast" because the broadcast address is determined automatically based on the ip address and subnet. So what you want is probably something like this:
/sbin/ifconfig eth0:1 192.168.0.10 netmask 255.255.255.0
In this case, your alias ip address is 192.168.0.10 and the broadcast for that network is (automatically) 192.168.0.255.

But you are looking to do something totally different than simply creating an alias. You are trying to get a single NIC to play nice on more than one subnet.

Now, there are mixed opinions about whether that can be done or not, and if it IS done, how to handle routing.
Here's a page that simply says it can't be done: http://www.cyberciti.biz/faq/linux-creating-or-adding-new-network-alias-to-a-network-card-nic/

But here's another that gives information showing it CAN be done, and explains further about the routing:
http://andrew.org/index.php/archives/2006/11/04/cheap-but-useful-trick-with-alias-network-interface/

In particular, pay attention to the part where it says "Adding the alias interface automagically adds a route for the new interfaces IP subnet, but of course does not change your default route. If your new device is a router and you want to reach networks behind it, youll need to add specific routes to your system."

So in other words, if you want to script this, you'll also have to add a "route add" statement.

Assume your first subnet (the one with your default gateway) is 192.168.0.x/24 and you want to add an alias to another subnet. Let's say the other subnet is 192.168.1.x/24. Your machine will be on 192.168.1.1 and the gateway on that subnet is 192.168.1.254.

Create the alias:
/sbin/ifconfig eth0:1 192.168.1.1 netmask 255.255.255.0
Create the route:
route add  -net  192.168.1.0  netmask 255.255.255.0 gw 192.168.1.254 dev eth0


0
 

Author Comment

by:purestealth
ID: 24041217
dhoffman_98, awesome good stuff that worked perfectly thank-you.

The last part of my question is, when I down this interface, how do I remove that route?  Or does it get removed automatically when you down the interface?

I need to make sure its permanently removed such that when the interface is brought back up at a later date there is no record remaining of the previous "route add" command.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:purestealth
ID: 24049017
anyone watching this still?
0
 
LVL 7

Assisted Solution

by:computerfixins
computerfixins earned 100 total points
ID: 24049114
/sbin/route del -net 169.254.0.0/16
0
 
LVL 13

Expert Comment

by:dhoffman_98
ID: 24053658
Sorry, haven't been at my desk all day...

You can also use ifconfig to delete an interface, and depending on which flavor of linux you have, you may have an ifdown command to shut it down as well.

Here's a link to a page that goes into more detail about removing the alias interfaces.
http://www.cyberciti.biz/faq/linux-command-to-remove-virtual-interfaces-or-network-aliases/

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now