WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: ip static for mac add in bootlocal.sh  (Read 3862 times)

Offline krlosg7

  • Newbie
  • *
  • Posts: 5
ip static for mac add in bootlocal.sh
« on: November 06, 2015, 04:51:56 AM »
Hi all,

I have write a small script that allows every tinyvcore to start up with an IP static. To have this IP address the script must read mac address machine after boot.

the script is in /opt/script.sh and  in my /opt/bootlocal.sh, I have this entry: /opt/script.sh &

Here is the script.

Code: [Select]
#!/bin/sh
pkill udhcpc
case 'cat /sys/class/net/eth0/address' in
52:54:00:12:34:56) IP='ifconfig eth0 192.168.1.1 netmask 255.255.255.0 up' ;;
52:54:00:12:34:57) IP='ifconfig eth0 192.168.1.2 netmask 255.255.255.0 up' ;;
52:54:00:12:34:58) IP='ifconfig eth0 192.168.1.3 netmask 255.255.255.0 up' ;;
52:54:00:12:34:59) IP='ifconfig eth0 192.168.1.4 netmask 255.255.255.0 up' ;;
*) IP='ifconfig eth0 192.168.1.5 netmask 255.255.255.0 up' ;;
esac
$IP

I reboot the machines. always every machine take the last IP.... how i can to fix it?

Any recommendation very welcome, Thanks in advance

Offline Misalf

  • Hero Member
  • *****
  • Posts: 1702
Re: ip static for mac add in bootlocal.sh
« Reply #1 on: November 06, 2015, 05:27:12 AM »
You're using single quotes. That doesn't work as it's handled as a string. Use backticks instead.
Change this
Code: [Select]
case 'cat /sys/class/net/eth0/address' in
to this
Code: [Select]
case `cat /sys/class/net/eth0/address` in
or
Code: [Select]
case $(cat /sys/class/net/eth0/address) in
so it's interpreted as a command.
Download a copy and keep it handy: Core book ;)

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: ip static for mac add in bootlocal.sh
« Reply #2 on: November 06, 2015, 08:44:18 AM »
Why don't you just use dhcp and make the assignments in your dhcp server/router?

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11618
Re: ip static for mac add in bootlocal.sh
« Reply #3 on: November 06, 2015, 09:36:41 AM »
Hi krlosg7
You might also want to consider simplifying that code snippet to something like:
Code: [Select]
#!/bin/sh
pkill udhcpc
case `cat /sys/class/net/eth0/address` in
52:54:00:12:34:56) SUBNET=1 ;;
52:54:00:12:34:57) SUBNET=2 ;;
52:54:00:12:34:58) SUBNET=3 ;;
52:54:00:12:34:59) SUBNET=4 ;;
*) SUBNET=5 ;;
esac
ifconfig eth0 192.168.1.$SUBNET netmask 255.255.255.0 up
Now the  case  statement is less busy and the intent is easier to follow.

Offline krlosg7

  • Newbie
  • *
  • Posts: 5
Re: ip static for mac add in bootlocal.sh
« Reply #4 on: November 06, 2015, 04:51:46 PM »
 the answer by Misalf and Rich work  ;D.

gerald_clark talk about dhcp server but i have tried and that not work.

this is my topology.

multiple tap interface...

Code: [Select]
tap1: 10.10.10.1 /24
--------------------\            \
                     \------------\-------qemu server-----------ISC DHCP server
                     / 10.10.10.252/       10.10.10.253       10.10.10.254                           
--------------------/  OVS       /            eth1                 eth1
tap2: 10.10.10.2 /24

I've been trying to get dhcp address by an ISC Server. I have one DHCP Server and one qemu server with multiple image tiny core.

Over the qemu server OVS switch connects tap interfaces.

Any help would be appreciated with  dhcp server/router idea of  gerald_clark. Thanks.

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: ip static for mac add in bootlocal.sh
« Reply #5 on: November 06, 2015, 11:20:40 PM »
Make sure you use a bridged connection in your virtual machine setup.