-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwg-netns-shell
executable file
·43 lines (35 loc) · 1.43 KB
/
wg-netns-shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash
#
# Author: Max Idermark
# GitHub Repo: https://github.com/Accelerox/wireguard-namespace
#
# This script starts a new bash shell in the specified namespace.
# The new bash shell displays the unique identifier name in the prompt.
# The user who ran the script will be used in the new bash session.
#
# Usage: sudo ./wg-netns-shell.sh <identifier>
# Check if the script is run with sudo
if [[ $EUID -ne 0 ]]; then
echo "This script must be run with sudo"
exit 1
fi
if [[ $# -ne 1 ]]; then
echo "Usage: $0 <identifier>"
exit 1
fi
identifier=$1
user_name=$(logname)
# Check if the namespace exists
if ! ip netns list | grep -q -w "${identifier}"; then
echo "Namespace '${identifier}' not found"
exit 1
fi
# Set the custom PS1 with colors and font as an environment variable
custom_ps1='\[\e]0;\u@'"${identifier}"': \w\a\]${debian_chroot:+(\$debian_chroot)}\[\033[01;32m\]\u@'"${identifier}"'\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
# Write the PS1 variable to a temporary file
echo "export PS1='${custom_ps1}'" > /tmp/ps1.sh
# Start a new bash shell in the specified namespace with the unique identifier in the prompt
# Use the user who ran the script in the new bash session
ip netns exec ${identifier} runuser -u ${user_name} -- bash --rcfile /tmp/ps1.sh
# Remove the temporary file
rm /tmp/ps1.sh