diff --git a/vmware/Vagrantfile b/vmware/Vagrantfile new file mode 100644 index 0000000..f3475e3 --- /dev/null +++ b/vmware/Vagrantfile @@ -0,0 +1,80 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +# All Vagrant configuration is done below. The "2" in Vagrant.configure +# configures the configuration version (we support older styles for +# backwards compatibility). Please don't change it unless you know what +# you're doing. + +#$script = <<-SCRIPT +#/usr/bin/sed -ie '/sw1/d' /etc/hosts +#SCRIPT + +Vagrant.configure("2") do |config| + config.vm.provider "vmware_desktop" do |v| + v.vmx["memsize"] = "3000" + v.vmx["numvcpus"] = "2" + end + + config.vm.box = "generic/debian9" +# config.vm.box = "bento/ubuntu-20.04" + + config.vm.define "sw1" do |sw1| + sw1.vm.hostname = "sw1" + sw1.vm.network "private_network",bridge: "bridge1" + + # build entry for /etc/hosts + sw1.trigger.after :up do |trigger| + trigger.info = "Build entry for /etc/hosts" + trigger.run = {path: './list_hosts.sh', args: ['add', sw1.vm.hostname]} + end + # remove entry for /etc/hosts + sw1.trigger.before :destroy do |trigger| + trigger.info = "Remove entry for /etc/hosts" + trigger.run = {path: './list_hosts.sh', args: ['rm', sw1.vm.hostname]} + end + end + + config.vm.define "sw2" do |sw2| + sw2.vm.hostname = "sw2" + sw2.vm.network "private_network",bridge: "brdige1" + sw2.vm.network "private_network",bridge: "bridge2" + + # build entry for /etc/hosts + sw2.trigger.after :up do |trigger| + trigger.info = "Build entry for /etc/hosts" + trigger.run = {path: './list_hosts.sh', args: ['add', sw2.vm.hostname]} + end + # remove entry for /etc/hosts + sw2.trigger.before :destroy do |trigger| + trigger.info = "Remove entry for /etc/hosts" + trigger.run = {path: './list_hosts.sh', args: ['rm', sw2.vm.hostname]} + end + end + + config.vm.define "sw3" do |sw3| + sw3.vm.hostname = "sw3" + sw3.vm.network "private_network",bridge: "bridge2" + + # build entry for /etc/hosts + sw3.trigger.after :up do |trigger| + trigger.info = "Build entry for /etc/hosts" + trigger.run = {path: './list_hosts.sh', args: ['add', sw3.vm.hostname]} + end + # remove entry for /etc/hosts + sw3.trigger.before :destroy do |trigger| + trigger.info = "Remove entry for /etc/hosts" + trigger.run = {path: './list_hosts.sh', args: ['rm', sw3.vm.hostname]} + end + end + +# config.vm.provision "shell", inline: $script + + config.vm.provision "shell", inline: <<-SHELL + echo -e "vagrant\nvagrant" | passwd root + echo "PermitRootLogin yes" >> /etc/ssh/sshd_config + sed -in 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config + service ssh restart + SHELL + +end diff --git a/vmware/list_hosts.sh b/vmware/list_hosts.sh new file mode 100644 index 0000000..7092f63 --- /dev/null +++ b/vmware/list_hosts.sh @@ -0,0 +1,13 @@ +#!/bin/bash +touch hosts + +if [[ $1 == 'add' ]] +then + ip=$(vagrant ssh $2 -c "ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'") + echo "$ip $2" >> hosts +fi + +if [[ $1 == 'rm' ]] +then + sed -ie "/ $2\$/d" hosts +fi