Project

General

Profile

Support #921

Updated by Daniel Curtis almost 7 years ago

I have a FreeBSD VM with some jails that use a bridged network interface that changes networks fairly often. Rather than manually changing all the IP addresses manually, I wrote a script that changes the IP address of the jail config based on the network the VM is attached to. 

 * Create a new shell script: 
 <pre> 
 vi /usr/local/bin/chjailip.sh 
 </pre> 
 #* And add the following: 
 <pre> 
 #!/bin/sh 
 # Script to change ezjail config IP address 

 # Get the current primary interface IP and change it to .99 
 nextcloudip=`ifconfig | grep 255.255.255.255 | grep -Eo '(\b[0-9]{1,3}\.){3}[0-9]{1,3}\b' | grep -v 255 | sed 's/\.[0-9]*$/\.99/'` 

 # Replace the newjail ezjail configs IP address with the newly updated IP 
 sed -i '' -e "s/newjail_ip=\"*.*\"/newjail_ip=\"$newjailip\"/" /usr/local/etc/ezjail/newjail 
 </pre> 

 * Make the script executable: 
 <pre> 
 chmod +x /usr/local/bin/chjailip.sh 
 </pre> 

 * Edit the crontab: 
 <pre> 
 crontab -e 
 </pre> 
 #* And add the script to run every reboot: 
 <pre> 
 @reboot /usr/local/bin/chjailip.sh 
 </pre> 

 h2. Resources 

 * https://stackoverflow.com/questions/5277156/using-sed-to-search-and-replace-an-ip-address-in-a-file 
 * https://unix.stackexchange.com/questions/397107/how-to-change-last-value-ip-address-with-sed/397109 

Back