Advertisement
jor_teron

NS-internet.sh

Sep 2nd, 2022 (edited)
1,462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.10 KB | Source Code | 0 0
  1. #/!/bin/bash
  2. # Provide internet to namespace, chroot, start ssh, rdp server.
  3.  
  4. #WLAN="wlp2s0" #laptop?
  5. #WLAN="wlxe84e0624e2a4" #HOME
  6. WLAN="wlx20e116000c9c" #Office Wifi
  7.  
  8. NS=ns0
  9. IFIN=ifin0
  10. IFOUT=ifout0
  11. chroot_dir="/home/debian" # Chroot Dir
  12. IP="10.10.10"
  13.  
  14. IFOUT_IP=$IP.101
  15. IFIN_IP=$IP.102
  16. #WLAN_IP=$IP.200
  17. #:::::::::::::::::::::::::::::::::::::::::::::::
  18. #set sudo session
  19. echo "password" | sudo -S echo "Password authenticated."
  20.  
  21. # Remove IFNAME / Flush var etc.
  22. sudo ip netns delete $NS 2>&1 &
  23. sudo ip link del $IFOUT 2>&1 &
  24. sudo umount $chroot_dir/proc 2>&1 &
  25. sudo umount $chroot_dir/dev 2>&1 &
  26. #::::::::::::::::::::::::::::::::::::::::::::::::
  27. # Add NS
  28. sudo ip netns add $NS
  29.  
  30. # Add peer veth
  31. sudo ip link add $IFOUT type veth peer name $IFIN
  32. #::::::::::::::::::::::::::::::::::::::::::::::::::
  33. # Set NS to IF IN
  34. sudo ip link set $IFIN netns $NS
  35. sudo ip netns exec $NS ip link set lo up
  36. #:::::::::::::::::::::::::::::::::::::::::::::::::
  37. #SET IP
  38. set_ip(){
  39. sudo ip link set $1 down
  40. sudo ip addr add $2/24 dev $1
  41. sudo ip link set $1 up
  42. }
  43. # Set IF IP
  44. set_ip $IFOUT $IFOUT_IP
  45. set_ip $WLAN $WLAN_IP
  46.  
  47. sudo ip netns exec $NS ip link set $IFIN down
  48. sudo ip netns exec $NS ip addr add $IFIN_IP/24 dev $IFIN
  49. #sudo ip netns exec $NS ip set $IFIN name eth0
  50. sudo ip netns exec $NS ip link set $IFIN up
  51. #::::::::::::::::::::::::::::::::::::::::::::::::::
  52. # Route rule
  53. sudo ip netns exec $NS ip route add default via $IFOUT_IP
  54. #sudo echo 1 > /proc/sys/net/ipv4/ip_forward
  55.  
  56. # Flush ip rules
  57. sudo iptables -P FORWARD DROP
  58. sudo iptables -F FORWARD
  59. sudo iptables -t nat -F
  60.  
  61. # Set iptables rules #forwarding
  62. sudo iptables -t nat -A POSTROUTING -s $IFIN_IP/24 -o $WLAN -j MASQUERADE
  63. sudo iptables -A FORWARD -i $WLAN -o $IFOUT -j ACCEPT
  64. sudo iptables -A FORWARD -o $WLAN -i $IFOUT -j ACCEPT
  65. #:::::::::::::::::::::::::::::::::::::::::::::::::::::::
  66. # Check
  67. ping_alive(){   echo -e "`sudo ip netns exec $1 fping $2` ( $3 )" ; }
  68.  
  69. ping_alive $NS $IFOUT_IP IF_OUT
  70. ping_alive $NS $IFIN_IP IF_IN
  71. #ping_alive $NS $WLAN_IP WIFI
  72. ping_alive $NS 1.1.1.1 Internet
  73. #::::::::::::::::::::::::::::::::::::::::::::::::::::::
  74. #Prepare & Chroot
  75. sudo mount -t proc /proc $chroot_dir/proc
  76. sudo mount --rbind /dev $chroot_dir/dev
  77. sudo mount -t devpts /dev/pts $chroot_dir/dev/pts
  78. sudo mount --rbind /home/user $chroot_dir/home/user
  79.  
  80. #display=':10'
  81. #export DISPLAY=:10
  82. #screen_resolution=$( xrandr | grep ' connected' | awk '{print $4}' ) ;
  83. #echo "Entering chroot environment."
  84. sudo ip netns exec $NS /usr/sbin/chroot $chroot_dir /etc/init.d/ssh start
  85. sudo ip netns exec $NS /usr/sbin/chroot $chroot_dir /etc/init.d/xrdp restart
  86. #sudo ip netns exec $NS /usr/sbin/chroot $chroot_dir /usr/bin/bash
  87.  
  88. #sudo ip netns exec $NS /usr/sbin/chroot $chroot_dir echo $DISPLAY
  89. #echo $screen_resolution ;
  90. #Xnest -ac -geometry $screen_resolution :10 > /dev/null 2>&1 &
  91. #/usr/bin/wmctrl -r Xnest -b toggle,fullscreen &
  92. #sudo ip netns exec $NS /usr/sbin/chroot $chroot_dir sudo -u user mate-session
  93.  
  94. #echo -e "nameserver    1.1.1.1\nnameserver 8.8.8.8" >> /etc/resolv.conf
  95. #echo "127.0.0.1    HOSTNAME" >> /etc/hosts
  96. #/etc/init.d/ssh start
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement