Skip to content

Commit

Permalink
Update bashvm-port-forwarding.sh
Browse files Browse the repository at this point in the history
- better variable handling
  • Loading branch information
babywhale321 authored May 28, 2024
1 parent 814050d commit 04890f3
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions bashvm-port-forwarding.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ fi
if [ -f $unused_port_log ];then
unused_port=$(tail -n 1 /var/log/bashvm/unused_ports.log)

if [ ! -z $unused_port ];then
if [ ! -z "$unused_port" ];then

#unused port will become the port
start_port=$(($unused_port - 22))
# Remove unused port from unused log file
sed -i '/'$unused_port'/d' /var/log/bashvm/unused_ports.log
sed -i '/'"$unused_port"'/d' /var/log/bashvm/unused_ports.log
fi

fi
Expand Down Expand Up @@ -70,17 +70,17 @@ echo "$nat_script" >> /etc/libvirt/hooks/qemu

# Reserve a port for SSH
ssh_port=$(($start_port - 1))
echo ' /sbin/iptables -D FORWARD -o '$int_name' -p tcp -d '$nat_ip' --dport 22 -j ACCEPT' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -t nat -D PREROUTING -p tcp --dport '$ssh_port' -j DNAT --to '$nat_ip':22' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -D FORWARD -o '$int_name' -p udp -d '$nat_ip' --dport 22 -j ACCEPT' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -t nat -D PREROUTING -p udp --dport '$ssh_port' -j DNAT --to '$nat_ip':22' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -D FORWARD -o '"$int_name"' -p tcp -d '"$nat_ip"' --dport 22 -j ACCEPT' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -t nat -D PREROUTING -p tcp --dport '$ssh_port' -j DNAT --to '"$nat_ip"':22' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -D FORWARD -o '"$int_name"' -p udp -d '"$nat_ip"' --dport 22 -j ACCEPT' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -t nat -D PREROUTING -p udp --dport '$ssh_port' -j DNAT --to '"$nat_ip"':22' >> /etc/libvirt/hooks/qemu
# Port forward rules to loop until it reaches end port
for ((port=start_port; port<=end_port; port++)); do

echo ' /sbin/iptables -D FORWARD -o '$int_name' -p tcp -d '$nat_ip' --dport '$port' -j ACCEPT' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -t nat -D PREROUTING -p tcp --dport '$port' -j DNAT --to '$nat_ip':'$port'' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -D FORWARD -o '$int_name' -p udp -d '$nat_ip' --dport '$port' -j ACCEPT' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -t nat -D PREROUTING -p udp --dport '$port' -j DNAT --to '$nat_ip':'$port'' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -D FORWARD -o '"$int_name"' -p tcp -d '"$nat_ip"' --dport '$port' -j ACCEPT' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -t nat -D PREROUTING -p tcp --dport '$port' -j DNAT --to '"$nat_ip"':'$port'' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -D FORWARD -o '"$int_name"' -p udp -d '"$nat_ip"' --dport '$port' -j ACCEPT' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -t nat -D PREROUTING -p udp --dport '$port' -j DNAT --to '"$nat_ip"':'$port'' >> /etc/libvirt/hooks/qemu
done

# Keep out of loop
Expand All @@ -89,17 +89,17 @@ middle_script=' fi
echo "$middle_script" >> /etc/libvirt/hooks/qemu

# Reserve for SSH
echo ' /sbin/iptables -I FORWARD -o '$int_name' -p tcp -d '$nat_ip' --dport 22 -j ACCEPT' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -t nat -I PREROUTING -p tcp --dport '$ssh_port' -j DNAT --to '$nat_ip':22' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -I FORWARD -o '$int_name' -p udp -d '$nat_ip' --dport 22 -j ACCEPT' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -t nat -I PREROUTING -p udp --dport '$ssh_port' -j DNAT --to '$nat_ip':22' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -I FORWARD -o '"$int_name"' -p tcp -d '"$nat_ip"' --dport 22 -j ACCEPT' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -t nat -I PREROUTING -p tcp --dport '$ssh_port' -j DNAT --to '"$nat_ip"':22' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -I FORWARD -o '"$int_name"' -p udp -d '"$nat_ip"' --dport 22 -j ACCEPT' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -t nat -I PREROUTING -p udp --dport '$ssh_port' -j DNAT --to '"$nat_ip"':22' >> /etc/libvirt/hooks/qemu
# port forward rules to loop until it reaches end port
for ((port=start_port; port<=end_port; port++)); do

echo ' /sbin/iptables -I FORWARD -o '$int_name' -p tcp -d '$nat_ip' --dport '$port' -j ACCEPT' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -t nat -I PREROUTING -p tcp --dport '$port' -j DNAT --to '$nat_ip':'$port'' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -I FORWARD -o '$int_name' -p udp -d '$nat_ip' --dport '$port' -j ACCEPT' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -t nat -I PREROUTING -p udp --dport '$port' -j DNAT --to '$nat_ip':'$port'' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -I FORWARD -o '"$int_name"' -p tcp -d '"$nat_ip"' --dport '$port' -j ACCEPT' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -t nat -I PREROUTING -p tcp --dport '$port' -j DNAT --to '"$nat_ip"':'$port'' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -I FORWARD -o '"$int_name"' -p udp -d '"$nat_ip"' --dport '$port' -j ACCEPT' >> /etc/libvirt/hooks/qemu
echo ' /sbin/iptables -t nat -I PREROUTING -p udp --dport '$port' -j DNAT --to '"$nat_ip"':'$port'' >> /etc/libvirt/hooks/qemu
done

# Keep out of loop
Expand All @@ -117,18 +117,18 @@ chmod +x /etc/libvirt/hooks/qemu
vm_on=$(virsh list --all | grep -E '^\s+[0-9]+' | wc -l)

# If only 1 then restart libvirtd
if [ $vm_on == 1 ]; then
if [ "$vm_on" == 1 ]; then
systemctl restart libvirtd
fi

echo ""
echo "========== Info for $vm_name ==========" | tee -a /var/log/bashvm/$vm_name.info.txt
echo "" | tee -a /var/log/bashvm/$vm_name.info.txt
echo "SSH port: $ssh_port" | tee -a /var/log/bashvm/$vm_name.info.txt
echo "Ports: $start_port to $end_port" | tee -a /var/log/bashvm/$vm_name.info.txt
echo "" | tee -a /var/log/bashvm/$vm_name.info.txt
echo "====================================================" | tee -a /var/log/bashvm/$vm_name.info.txt
echo "========== Info for $vm_name ==========" | tee -a /var/log/bashvm/"$vm_name".info.txt
echo "" | tee -a /var/log/bashvm/"$vm_name".info.txt
echo "SSH port: $ssh_port" | tee -a /var/log/bashvm/"$vm_name".info.txt
echo "Ports: $start_port to $end_port" | tee -a /var/log/bashvm/"$vm_name".info.txt
echo "" | tee -a /var/log/bashvm/"$vm_name".info.txt
echo "====================================================" | tee -a /var/log/bashvm/"$vm_name".info.txt
echo ""
chmod 600 /var/log/bashvm/$vm_name.info.txt
chmod 600 /var/log/bashvm/"$vm_name".info.txt
echo "Info for $vm_name has been saved to /var/log/bashvm/$vm_name.info.txt"
echo "You will need to stop then start the vm for the changes to take effect"

0 comments on commit 04890f3

Please sign in to comment.