• Announcements

    • MannDude

      Current state of vpsBoard   02/04/2017

      Dear vpsBoard members and guests:

      Over the last year or two vpsBoard activity and traffic has dwindled. I have had a change of career and interests, and as such am no longer an active member of the web hosting industry.

      Due to time constraints and new interests I no longer wish to continue to maintain vpsBoard. The web site will remain only as an archive to preserve and showcase some of the great material, guides, and industry news that has been generated by members, some of which I remain in contact to this very day and now regard as personal friends.

      I want to thank all of our members who helped make vpsBoard the fastest growing industry forum. In it's prime it was an active and ripe source of activity, news, guides and just general off-topic banter and fun.

      I wish all members and guests the very best, whether it be with your business or your personal projects.

      -MannDude
Sign in to follow this  
Followers 0
mitgib

Simple variables

5 posts in this topic

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

#!/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;

Share this post


Link to post
Share on other sites

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

#!/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

 

Share this post


Link to post
Share on other sites

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.

 

Share this post


Link to post
Share on other sites
#!/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]}
done

result
 

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

 

 

Edited by sv01
2 people like this

Share this post


Link to post
Share on other sites
#!/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]}
done

result
 

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!

Share this post


Link to post
Share on other sites
Sign in to follow this  
Followers 0