Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Alternate network configuration
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
FreeFly42
l33t
l33t


Joined: 03 Nov 2003
Posts: 848
Location: Houston, TX

PostPosted: Mon Apr 19, 2004 3:54 am    Post subject: Alternate network configuration Reply with quote

I often find myself switching my laptop between networks which have dhcp and embedded control networks where a hard-coded IP address is required. This is one of the few areas where Windows XP actually has a very nice feature--the ability to specify an alternate fixed IP configuration if dhcp fails. I've modified the /etc/init.d/net.eth0 script to allow for an alternate configuration when dhcp fails which I use everyday and really like. I'm posting my patch to /etc/init.d/net.eth0 below, it is based on baselayout 1.8.11.

This allows you to specify a fixed configuration in /etc/init.d/net for each device by defining the var ifconfig_alt_ethX as follows:
Code:
ifconfig_alt_eth0="172.16.86.22 netmask 255.255.240.0"


This is the patch:

Code:
*** net.eth0.orig   Sun Apr 18 16:06:28 2004
--- net.eth0   Sun Apr 18 16:08:56 2004
***************
*** 33,44 ****
  # setup_vars: setup variables based on $1 and content of /etc/conf.d/net
  # The following variables are set, which should be declared local by
  # the calling routine.
! #   status_IFACE   (up or '')
! #   vlans_IFACE      (space-separated list)
! #   ifconfig_IFACE   (array of ifconfig lines, replaces iface_IFACE)
! #   dhcpcd_IFACE   (command-line args for dhcpcd)
! #   routes_IFACE   (array of route lines)
! #   inet6_IFACE      (array of inet6 lines)
  setup_vars() {
     local i iface="${1//\./_}"
 
--- 33,45 ----
  # setup_vars: setup variables based on $1 and content of /etc/conf.d/net
  # The following variables are set, which should be declared local by
  # the calling routine.
! #   status_IFACE      (up or '')
! #   vlans_IFACE         (space-separated list)
! #   ifconfig_IFACE      (array of ifconfig lines, replaces iface_IFACE)
! #   dhcpcd_IFACE      (command-line args for dhcpcd)
! #   routes_IFACE      (array of route lines)
! #   inet6_IFACE         (array of inet6 lines)
! #   ifconfig_alt_IFACE   (alternate ifconfig)
  setup_vars() {
     local i iface="${1//\./_}"
 
***************
*** 48,53 ****
--- 49,55 ----
     eval dhcpcd_IFACE=\"\$\{dhcpcd_$iface\}\"
     eval routes_IFACE=( \"\$\{routes_$iface\[@\]\}\" )
     eval inet6_IFACE=( \"\$\{inet6_$iface\[@\]\}\" )
+    eval ifconfig_alt_IFACE=( \"\$\{ifconfig_alt_$iface\[@\]\}\" )
 
     # BACKWARD COMPATIBILITY: populate the ifconfig_IFACE array
     # if iface_IFACE is set (fex. iface_eth0 instead of ifconfig_eth0)
***************
*** 91,103 ****
        /sbin/ifconfig ${IFACE} &>${devnull}
        eend $? || return $?
     else
-       # Check that eth0 was not brought up by the kernel ...
        if [[ ${status_IFACE} == up ]]; then
           einfo "Keeping kernel configuration for ${IFACE}"
        else
           ebegin "Bringing ${IFACE} up via DHCP"
           /sbin/dhcpcd ${dhcpcd_IFACE} ${IFACE}
!          eend $? || return $?
        fi
        # Show address retrieved
        i=$(ifconfig ${IFACE} | grep -m1 -o 'inet addr:[^ ]*' | cut -d: -f2)
--- 93,126 ----
        /sbin/ifconfig ${IFACE} &>${devnull}
        eend $? || return $?
     else
        if [[ ${status_IFACE} == up ]]; then
           einfo "Keeping kernel configuration for ${IFACE}"
        else
           ebegin "Bringing ${IFACE} up via DHCP"
           /sbin/dhcpcd ${dhcpcd_IFACE} ${IFACE}
!          eend $? || {
!                 # if dhcp failed, try the alternate config
!                 # added by Kent Hulick
!                 if [ -n "${ifconfig_alt_IFACE}" ]
!                 then
!                     einfo "DHCP failed, Using alternate configuration for ${IFACE}"
!                     /sbin/ifconfig ${IFACE} ${ifconfig_alt_IFACE} >/dev/null || {
!                         retval=$?
!                         eend ${retval} "Failed to bring ${IFACE} up"
!                         return ${retval}
!                     }
!                     # ifconfig does not always return failure ..
!                     /sbin/ifconfig ${IFACE} &> /dev/null || {
!                         retval=$?
!                         eend ${retval} "Failed to bring ${IFACE} up"
!                         return ${retval}
!                     }
!                 else
!                     retval=$?
!                     eend ${retval} "Failed to bring ${IFACE} up"
!                     return ${retval}
!                 fi
!          }
        fi
        # Show address retrieved
        i=$(ifconfig ${IFACE} | grep -m1 -o 'inet addr:[^ ]*' | cut -d: -f2)

_________________
Kent

Planes are dangerous, get out of 'em quick
Back to top
View user's profile Send private message
jpreston
n00b
n00b


Joined: 22 Apr 2004
Posts: 18
Location: Columbus, OH

PostPosted: Mon Apr 26, 2004 2:02 pm    Post subject: Reply with quote

Awesome patch! You should post this in the bugs so they'll add it, I'm SURE someone other than us two could find this useful!

Josh.
Back to top
View user's profile Send private message
Bastux
Guru
Guru


Joined: 15 Dec 2002
Posts: 369
Location: France - Paris

PostPosted: Mon Apr 26, 2004 4:48 pm    Post subject: Reply with quote

very useful for laptop, thank you!!!
Back to top
View user's profile Send private message
FreeFly42
l33t
l33t


Joined: 03 Nov 2003
Posts: 848
Location: Houston, TX

PostPosted: Wed Apr 28, 2004 12:59 am    Post subject: Reply with quote

I already filed a bug report.
_________________
Kent

Planes are dangerous, get out of 'em quick
Back to top
View user's profile Send private message
cryos
Retired Dev
Retired Dev


Joined: 08 Mar 2003
Posts: 242
Location: US

PostPosted: Wed Apr 28, 2004 7:56 pm    Post subject: Reply with quote

This is a great patch - very useful!! Thanks for posting it here - I had been wondering if there was a way to accomplish this behaviour easily for my laptop too.
Back to top
View user's profile Send private message
bravecobra
Tux's lil' helper
Tux's lil' helper


Joined: 26 Dec 2002
Posts: 130
Location: Planet Earth (sometimes)

PostPosted: Mon May 03, 2004 8:03 am    Post subject: Reply with quote

I could be mistaking but could this be of use somehow?
Code:
emerge quickswitch

_________________
Brave Cobra
http://www.bravecobra.com
Back to top
View user's profile Send private message
FreeFly42
l33t
l33t


Joined: 03 Nov 2003
Posts: 848
Location: Houston, TX

PostPosted: Mon May 03, 2004 3:40 pm    Post subject: Reply with quote

Quickswitch is nice, but you still have to be root and still have to launch a program (switcher) or type a command to switch profiles. I wanted it to fall back to a default profile automatically if dhcp fails. Now my network card gets set up automatically without any intervention on my part on all the networks I work on.
_________________
Kent

Planes are dangerous, get out of 'em quick
Back to top
View user's profile Send private message
Thnikkaman
n00b
n00b


Joined: 30 Jun 2004
Posts: 63

PostPosted: Wed Aug 04, 2004 6:33 pm    Post subject: Reply with quote

I have tried applying this patch to /etc/init.d/net.eth0, but it is failing for me. I am doing it via this syntax:

Code:

patch /etc/init.d/net.eth0 altIfconfig.patch


Am I trying to patch the wrong file?
Back to top
View user's profile Send private message
UberLord
Retired Dev
Retired Dev


Joined: 18 Sep 2003
Posts: 6759
Location: Blighty

PostPosted: Thu Aug 05, 2004 8:54 am    Post subject: Reply with quote

The patch is now redundanct as net.eth0 has a similar feature

ifconfig_fallback_eth0=( "192.168.0.1 broadcast 192.168.0.255 netmask 255.255.255.0" )
_________________
Use dhcpcd for all your automated network configuration needs
Use dhcpcd-ui (GTK+/Qt) as your System Tray Network tool
Back to top
View user's profile Send private message
Thnikkaman
n00b
n00b


Joined: 30 Jun 2004
Posts: 63

PostPosted: Thu Aug 05, 2004 9:30 pm    Post subject: Reply with quote

so in /etc/conf.d/net, would I insert a line like:

Code:

iface_fallback_eth0="10.0.0.2 broadcast 10.0.0.255 netmask 255.255.255.0"
Back to top
View user's profile Send private message
UberLord
Retired Dev
Retired Dev


Joined: 18 Sep 2003
Posts: 6759
Location: Blighty

PostPosted: Fri Aug 06, 2004 8:40 am    Post subject: Reply with quote

No, to use the fallback feature you need to switch to the ifconfig_eth0 variables as the iface ones have been depreciated.

It's exactly the same as the iface setup, just prefix with ifconfig instead and put the string in rounded brackets.
_________________
Use dhcpcd for all your automated network configuration needs
Use dhcpcd-ui (GTK+/Qt) as your System Tray Network tool
Back to top
View user's profile Send private message
Thnikkaman
n00b
n00b


Joined: 30 Jun 2004
Posts: 63

PostPosted: Fri Aug 06, 2004 3:42 pm    Post subject: Reply with quote

I will try that. Thanks for your help.
Back to top
View user's profile Send private message
bravecobra
Tux's lil' helper
Tux's lil' helper


Joined: 26 Dec 2002
Posts: 130
Location: Planet Earth (sometimes)

PostPosted: Thu Oct 28, 2004 3:23 am    Post subject: Reply with quote

That works indeed. Tnx.

I also added
Code:
dhcpcd_eth0="-t 5"
in /etc/conf.d/net.
That makes the DHCP request timeout after 5 seconds. In other words, if an IP isn't recieved from a DHCP server after 5 seconds, it uses the fallback IP. That works perfectly for me :)
_________________
Brave Cobra
http://www.bravecobra.com
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum