amuck-landowner

Simple variables

mitgib

New Member
Verified Provider
I admit I am not the best at writing scripts, I need to write a single file with 2 lines repeated 100 times, 200 lines total, and set 3 variables 

$vmid=vm{1..100}

$ip= a single IP from a list (./ip.txt)

$mac= a single MAC from a list (./mac-address.txt)

set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping  $vmid ip-address $ip
set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping  $vmid mac-address $mac

So far I can get $vmid set and write the output to a file, but how do I get the other 2 set is where I am hitting a wall

This is what I have tried, but it is putting the entire contents of each list into each line

Code:
#!/bin/sh
ip=`cat ./ip.txt`
mac=`cat ./mac-address.txt`
 for i in vm{1..100}; do
echo set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping $i ip-address $ip >> /root/vyos2
echo set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping $i mac-address $mac >> /root/vyos2
done;
 

tonyg

New Member
Maybe try it using the Bourne shell (sh) way of "arrays":

Code:
#!/bin/sh
for list in "ford mustang pinto galaxy" "chevy volt camaro corvette" "nissan 300ZX sentra maxima"
do
   set $list
   echo "$1 $2 $3"
done
 

HalfEatenPie

The Irrational One
Retired Staff
It's been a while since I've last written a script, so I don't know how helpful this would be.

Anyways it seems you're having an issue with importing the values of the text files as an array into the variable.  I'd check out this link: http://unix.stackexchange.com/questions/70934/bash-reading-txt-file-and-storing-in-array

or http://stackoverflow.com/questions/11393817/bash-read-lines-in-file-into-an-array

This is of course assuming your file (IPs and MACs) are all just one entry per new line and not using something else like a comma or some other form.
 

sv01

Slow but sure
#!/bin/sh
ip=( $(cat ip.txt) )
mac=( $(cat  mac-address.txt) )
vm=3;
for((i=0;i<vm;i++)); do
    echo set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping $i ip-address ${ip[$i]}
    echo set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping $i mac-address ${mac[$i]}
doneresult
 

Code:
set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping 0 ip-address IP1
set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping 0 mac-address MAC1
set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping 1 ip-address IP2
set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping 1 mac-address MAC2
set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping 2 ip-address IP3
set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping 2 mac-address MAC3
 
Last edited by a moderator:

mitgib

New Member
Verified Provider
#!/bin/sh
ip=( $(cat ip.txt) )
mac=( $(cat  mac-address.txt) )
vm=3;
for((i=0;i<vm;i++)); do
    echo set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping $i ip-address ${ip[$i]}
    echo set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping $i mac-address ${mac[$i]}
doneresult
 

set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping 0 ip-address IP1
set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping 0 mac-address MAC1
set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping 1 ip-address IP2
set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping 1 mac-address MAC2
set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping 2 ip-address IP3
set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping 2 mac-address MAC3

Worked perfect, only change I made was setting i=1 so it didn't start at 0 and then vm=100 

The way you are doing ip= and mac= was where I was stuck, so thanks!
 
Top
amuck-landowner