-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect
More file actions
executable file
·173 lines (148 loc) · 4.57 KB
/
connect
File metadata and controls
executable file
·173 lines (148 loc) · 4.57 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env bash
#
# Connect to a network server and mount a shared drive.
# If no drive letter is specified, unmount the drive.
# Use sudo to run the script with superuser rights.
# Note: Used Bash features that are available in Bash 5.3 and later.
#
# Important! This script uses with launcher.py to securely pass
# sensitive data (username and password) via stdin.
#
# Author: Anton Chernov
# Date: Feb 3, 2026
# License: MIT
#
# Version 1.0.0 - initial release.
# Version 1.1.0 - improved passing sensitive data to subprocess
# (replaced environment variables with stdin).
# Version 1.2.0 - without change in functionality, but with code clean up.
#
# Constants
VERSION="1.2.0"
GREEN="\033[0;32m"
YELLOW="\033[1;33m"
RED="\033[0;31m"
NO_COLOR="\033[0m"
# Global variables
use_color=1
logfile=""
# Print usage information
usage() {
local name=${0##*/}
cat <<-EOF
Usage: $name [-h | -v]
$name [-u <username>] [-t <target path>] [-d <drive letter>] [-l <logfile path>]
Connect to a network server and mount a shared drive.
Or unmount the drive if no drive letter is specified.
Options:
-h print this message and exit
-v print version information and exit
-t <target path> specify the target mount path (default: /mnt/server)
-l <logfile path> specify the path to the logfile (default: ./connection.log)
-d <drive letter> specify the drive letter to mount (e.g., g, h, i).
If omitted, the script will unmount the drive.
EOF
}
# Function to get the current timestamp in a human-readable format
get_timestamp() {
printf '%(%Y-%m-%d %H:%M:%S)T' -1
}
# Function to print log messages
log_print() {
local color=$1
local level=$2
local message=$3
if [ -n "$color" ]; then
printf "%b %s - %s\n" \
"${color}[${level}]${NO_COLOR}" "${ get_timestamp; }" "$message" >&2
else
printf "[%s] %s - %s\n" "$level" "${ get_timestamp; }" "$message" >&2
fi
printf "[%s] %s - %s\n" \
"$level" "${ get_timestamp; }" "$message" >> "$logfile" 2>/dev/null
}
# Function to print error messages and exit
failure() {
log_print "$RED" "FAIL" "$@"
exit -1
}
# Function to verify the script's environment
verification() {
local target_dir="$1"
# Is an output a terminal?
if [ ! -t 1 ]; then
use_color=0
fi
# Check logfile path
if [ -d "$logfile" ]; then
logfile="${logfile}/connection.log"
elif [ -z "$logfile" ] || [ "$logfile" = "." ]; then
logfile="$(pwd)/connection.log"
fi
# Check superuser rights
# if [[ -z "$SUDO_USER" ]]; then
if [[ $EUID -ne 0 ]]; then
failure "The script was run without superuser rights."
fi
# Check if target directory exists
if [[ ! -d "$target_dir" ]]; then
mkdir "$target_dir">/dev/null || \
failure "Unable to create target directory '$target_dir'."
fi
}
# Main function
main() {
# Read sensitive data from stdin
read -r SERVER_USER
read -r SERVER_PASS
# Local variables
local cmd=
local drive=
local action=
local target='/mnt/server'
local address='192.168.42.100'
local OPTIND OPTARG opt
while getopts 'u:t:d:l:hv' opt; do
case "$opt" in
t) target=$OPTARG;;
d) drive=${OPTARG,,};; # to lowercase
l) logfile=$OPTARG;;
h) usage; exit 0;;
v) echo "$VERSION"; exit 0;;
*) usage >&2; exit -2;;
esac
done
# Verify the script's environment
verification "$target"
if [[ -z "$drive" ]]; then
cmd="umount $target"
action="Unmounting network drive from '$target' directory"
else
case $drive in
'd'|'e'|'g'|'h'|'i')
action="Mounting drive '$drive:' to '$target'"
cmd="mount -t cifs //$address/$drive$ $target \
-o username=$SERVER_USER,password=$SERVER_PASS"
;;
*) failure "Invalid drive letter specified: '$drive'.";;
esac
fi
echo 'command:' "$cmd"
# Execute command and capture output
local output
output=${ eval "$cmd" 2>&1 ;}
# Old way (with subshell):
# output=$(eval "$cmd" 2>&1)
local exit_code=$?
if [ $exit_code -eq 0 ]; then
log_print "$GREEN" "DONE" "$action succeeded."
else
log_print "$RED" "FAIL" "$action failed. Exit code: $exit_code"
if [ -n "$output" ]; then
log_print "$YELLOW" "INFO" "Command output: $output"
fi
exit $exit_code
fi
}
# Run the main function with all script arguments
main "$@"