create-qemu-bridged-tuntap.sh 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. bridgename=br0
  4. tapinterface=tap0
  5. outinterface=eth1
  6. if [ "x$1" != "x" ] ; then
  7. outinterface="$1"
  8. fi
  9. ifaces=`awk -F: '{print $1}' /proc/net/dev | tail -n +3`
  10. iffound="0"
  11. for i in $ifaces
  12. do
  13. if [ "$outinterface" == "$i" ] ; then
  14. iffound="1"
  15. fi
  16. done
  17. if [ "$iffound" == "0" ] ; then
  18. echo
  19. echo "Can't find the output interface."
  20. echo
  21. exit 1
  22. fi
  23. outifaceip=`ifconfig | grep -A1 $outinterface | tail -1 | awk -F: '{print $2}' | awk '{print $1}'`
  24. outifaceiptokens=`echo $outifaceip | awk -F \. '{print NF}'`
  25. if [ "$outifaceiptokens" != "4" ] ; then
  26. echo
  27. echo "The selected output interface $outinterface doesn't seem to have a valid IP address."
  28. echo
  29. exit 1
  30. fi
  31. hostaddress="192.168.1.1"
  32. guestaddress="192.168.1.95"
  33. sudo tunctl -t $tapinterface
  34. sudo brctl addbr $bridgename
  35. sudo brctl addif $bridgename $tapinterface
  36. sudo ip link set $bridgename up
  37. sudo ip addr add $hostaddress/24 dev $bridgename
  38. sudo route add -host $guestaddress dev $bridgename
  39. sudo parprouted eth1 $bridgename
  40. sudo ~/scripts/ip-router.sh $outinterface