-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheasyoc_install.sh
More file actions
executable file
·128 lines (112 loc) · 3.79 KB
/
easyoc_install.sh
File metadata and controls
executable file
·128 lines (112 loc) · 3.79 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/bash
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Check if running as root
if [ "$EUID" -eq 0 ]; then
echo "Please do not run this script as root"
exit 1
fi
# Function to validate input
validate_input() {
if [ -z "$1" ]; then
echo "Error: Input cannot be empty"
exit 1
fi
}
# Check for required commands and install if necessary
if ! command_exists openconnect; then
echo "Installing openconnect..."
if command_exists brew; then
brew install openconnect
elif command_exists apt-get; then
sudo apt-get update
sudo apt-get install -y openconnect
elif command_exists yum; then
sudo yum install -y openconnect
else
echo "Error: Could not install openconnect. Please install it manually."
exit 1
fi
fi
if ! command_exists oathtool; then
echo "Installing oathtool..."
if command_exists brew; then
brew install oath-toolkit
elif command_exists apt-get; then
sudo apt-get update
sudo apt-get install -y oathtool
elif command_exists yum; then
sudo yum install -y oathtool
else
echo "Error: Could not install oathtool. Please install it manually."
exit 1
fi
fi
# Get user input with validation
read -p "Enter alias for the VPN command (e.g., 'work'): " alias_name
validate_input "$alias_name"
read -p "Enter your VPN username: " vpn_username
validate_input "$vpn_username"
read -p "Enter VPN server URL: " vpn_url
validate_input "$vpn_url"
read -p "Do you use zsh? (y/n): " use_zsh
validate_input "$use_zsh"
# Check if config files already exist
totp_file="$HOME/.${alias_name}_easyoc_totp_google"
domain_file="$HOME/.${alias_name}_easyoc_domain"
if [ -f "$totp_file" ]; then
echo "Warning: TOTP file already exists. Do you want to overwrite it? (y/n)"
read -r overwrite
if [ "$overwrite" != "y" ]; then
echo "Installation aborted"
exit 1
fi
fi
if [ -f "$domain_file" ]; then
echo "Warning: Domain file already exists. Do you want to overwrite it? (y/n)"
read -r overwrite
if [ "$overwrite" != "y" ]; then
echo "Installation aborted"
exit 1
fi
fi
# Create necessary files with secure permissions
touch "$totp_file"
touch "$domain_file"
chmod 600 "$totp_file" "$domain_file"
# Create the VPN function
if [ "$use_zsh" = "y" ]; then
vpn_function="function ${alias_name}_vpn() {
read -s 'password?Enter password: '
local otp=\$(cat ~/.${alias_name}_easyoc_totp_google | xargs oathtool --totp -b)
echo -e \"\$password\n\$otp\" | sudo openconnect --useragent=AnyConnect --user ${vpn_username} --syslog --passwd-on-stdin --script 'vpn-slice $(cat ~/.${alias_name}_easyoc_domain)' ${vpn_url}
}"
# Add the function to .zshrc
echo "$vpn_function" >> ~/.zshrc
chmod 644 ~/.zshrc
else
vpn_function="function ${alias_name}_vpn() {
read -s -p 'Enter password: ' password
echo
local otp=\$(cat ~/.${alias_name}_easyoc_totp_google | xargs oathtool --totp -b)
echo -e \"\$password\n\$otp\" | sudo openconnect --useragent=AnyConnect --user ${vpn_username} --syslog --passwd-on-stdin --script 'vpn-slice $(cat ~/.${alias_name}_easyoc_domain)' ${vpn_url}
}"
# Add the function to .bash_profile
echo "$vpn_function" >> ~/.bash_profile
chmod 644 ~/.bash_profile
fi
echo "Installation completed successfully!"
echo "Please add your Google Authenticator token to ~/.${alias_name}_easyoc_totp_google"
echo "Restart your terminal and run '${alias_name}_vpn' to connect to the VPN"
echo "Use command example: echo \"YOUR_TOKEN_GOOGLE_AUTH\" > '~/.${alias_name}_easyoc_totp_google'"
# Clean up variables
unset alias_name
unset vpn_username
unset vpn_url
unset vpn_function
unset use_zsh
unset totp_file
unset domain_file
unset overwrite