1
+ #! /bin/sh -e
2
+
3
+ . ../common-script.sh
4
+
5
+ # Check if ~/.ssh/config exists, if not, create it
6
+ if [ ! -f ~ /.ssh/config ]; then
7
+ touch ~ /.ssh/config
8
+ chmod 600 ~ /.ssh/config
9
+ fi
10
+
11
+ # Function to show available hosts from ~/.ssh/config
12
+ show_available_hosts () {
13
+ printf " %b\n" " Available Systems:"
14
+ grep -E " ^Host " ~ /.ssh/config | awk ' {print $2}'
15
+ printf " %b\n" " -------------------"
16
+ }
17
+
18
+ # Function to ask for host details
19
+ ask_for_host_details () {
20
+ printf " %b\n" " Enter Host Alias: "
21
+ read -r host_alias
22
+ printf " %b\n" " Enter Remote Host (hostname or IP): "
23
+ read -r host
24
+ printf " %b\n" " Enter Remote User: "
25
+ read -r user
26
+ printf " %b\n" " Host $host_alias " >> ~ /.ssh/config
27
+ printf " %b\n" " HostName $host " >> ~ /.ssh/config
28
+ printf " %b\n" " User $user " >> ~ /.ssh/config
29
+ printf " %b\n" " IdentityFile ~/.ssh/id_rsa" >> ~ /.ssh/config
30
+ printf " %b\n" " StrictHostKeyChecking no" >> ~ /.ssh/config
31
+ printf " %b\n" " UserKnownHostsFile=/dev/null" >> ~ /.ssh/config
32
+ printf " %b\n" " Host $host_alias added successfully."
33
+ }
34
+
35
+ # Function to generate SSH key if not exists
36
+ generate_ssh_key () {
37
+ if [ ! -f ~ /.ssh/id_rsa ]; then
38
+ printf " %b\n" " SSH key not found, generating one..."
39
+ ssh-keygen -t rsa -b 4096 -f ~ /.ssh/id_rsa -N " " -C " $( whoami) @$( hostname) "
40
+ else
41
+ printf " %b\n" " SSH key already exists."
42
+ fi
43
+ }
44
+
45
+ # Function to share the SSH public key with the remote host
46
+ share_ssh_key () {
47
+ printf " %b\n" " Enter the alias of the host to copy the key to: "
48
+ read -r host_alias
49
+ printf " %b\n" " Copying SSH key to $host_alias ..."
50
+ ssh-copy-id " $host_alias "
51
+ printf " %b\n" " SSH key copied to $host_alias successfully."
52
+ }
53
+
54
+ # Function to disable password authentication and allow only SSH keys
55
+ # repeated twice as changes should take place when in commented state or modified state.
56
+ disable_password_auth () {
57
+ printf " %b\n" " Disabling SSH password authentication and enabling key-only login..."
58
+ printf " %b\n" " Enter the alias of the host: "
59
+ read -r host_alias
60
+ printf " %b\n"
61
+ ssh $host_alias "
62
+ $ESCALATION_TOOL -S sed -i 's/^#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config &&
63
+ $ESCALATION_TOOL -S sed -i 's/^PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config &&
64
+ $ESCALATION_TOOL -S sed -i 's/^#PubkeyAuthentication no/PubkeyAuthentication yes/' /etc/ssh/sshd_config &&
65
+ $ESCALATION_TOOL -S sed -i 's/^PubkeyAuthentication no/PubkeyAuthentication yes/' /etc/ssh/sshd_config &&
66
+ $ESCALATION_TOOL -S systemctl restart sshd
67
+ "
68
+ printf " %b\n" " PasswordAuthentication set to no and PubkeyAuthentication set to yes."
69
+ }
70
+
71
+ enable_password_auth () {
72
+ printf " %b\n" " Disabling SSH password authentication and enabling key-only login..."
73
+ printf " %b\n" " Enter the alias of the host: "
74
+ read -r host_alias
75
+ printf " \n"
76
+ ssh $host_alias "
77
+ $ESCALATION_TOOL -S sed -i 's/^#PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config &&
78
+ $ESCALATION_TOOL -S sed -i 's/^PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config &&
79
+ $ESCALATION_TOOL -S sed -i 's/^#PubkeyAuthentication yes/PubkeyAuthentication no/' /etc/ssh/sshd_config &&
80
+ $ESCALATION_TOOL -S sed -i 's/^PubkeyAuthentication yes/PubkeyAuthentication no/' /etc/ssh/sshd_config &&
81
+ $ESCALATION_TOOL -S systemctl restart sshd
82
+ "
83
+ printf " %b\n" " PasswordAuthentication set to yes and PubkeyAuthentication set to no."
84
+ }
85
+
86
+ # Function to check if password authentication is disabled
87
+ check_password_auth () {
88
+ printf " %b\n" " Enter the alias of the host: "
89
+ read -r host_alias
90
+ ssh $host_alias " grep '^PasswordAuthentication' /etc/ssh/sshd_config"
91
+ }
92
+
93
+ # Function to run a command on a remote server
94
+ run_remote_command () {
95
+ printf " %b\n" " Enter the alias of the host: "
96
+ read -r host_alias
97
+ printf " %b\n" " Enter the command to run: "
98
+ read -r remote_command
99
+ ssh $host_alias " $remote_command "
100
+ }
101
+
102
+ # Function to copy a file to a remote server
103
+ copy_file_to_remote () {
104
+ printf " %b\n" " Enter the local file path: "
105
+ read -r local_file
106
+ printf " %b\n" " Enter the alias of the host: "
107
+ read -r host_alias
108
+ printf " %b\n" " Enter the remote destination path: "
109
+ read -r remote_path
110
+ scp $local_file $host_alias :$remote_path
111
+ }
112
+
113
+ # Function to copy a directory to a remote server
114
+ copy_directory_to_remote () {
115
+ printf " %b\n" " Enter the local directory path: "
116
+ read -r local_dir
117
+ printf " %b\n" " Enter the alias of the host: "
118
+ read -r host_alias
119
+ printf " %b\n" " Enter the remote destination path: "
120
+ read -r remote_path
121
+ scp -r $local_dir $host_alias :$remote_path
122
+ }
123
+
124
+
125
+ # Function to move a file to a remote server (copy and delete local)
126
+ move_file_to_remote () {
127
+ printf " %b\n" " Enter the local file path: "
128
+ read -r local_file
129
+ printf " %b\n" " Enter the alias of the host: "
130
+ read -r host_alias
131
+ printf " %b\n" " Enter the remote destination path: "
132
+ read -r remote_path
133
+ scp $local_file $host_alias :$remote_path && rm $local_file
134
+ }
135
+
136
+ # Function to move a directory to a remote server (copy and delete local)
137
+ move_directory_to_remote () {
138
+ printf " %b\n" " Enter the local directory path: "
139
+ read -r local_dir
140
+ printf " %b\n" " Enter the alias of the host: "
141
+ read -r host_alias
142
+ printf " %b\n" " Enter the remote destination path: "
143
+ read -r remote_path
144
+ scp -r $local_dir $host_alias :$remote_path && rm -r $local_dir
145
+ }
146
+
147
+ # Function to remove a system from SSH configuration
148
+ remove_system () {
149
+ printf " %b\n" " Enter the alias of the host to remove: "
150
+ read -r host_alias
151
+ sed -i " /^Host $host_alias /,+3d" ~ /.ssh/config
152
+ printf " %b\n" " Removed $host_alias from SSH configuration."
153
+ }
154
+
155
+ # Function to view SSH configuration
156
+ view_ssh_config () {
157
+ printf " %b\n" " Enter the alias of the host to view (or press Enter to view all): "
158
+ read -r host_alias
159
+ if [ -z " $host_alias " ]; then
160
+ cat ~ /.ssh/config
161
+ else
162
+ grep -A 3 " ^Host $host_alias " ~ /.ssh/config
163
+ fi
164
+ }
165
+
166
+ # Function to backup files from remote host
167
+ backup_files () {
168
+ printf " %b\n" " Enter the alias of the host: "
169
+ read -r host_alias
170
+ printf " %b\n" " Enter the files or directories to backup on remote host: "
171
+ read -r remote_files
172
+ printf " %b\n" " Enter the local backup directory path: "
173
+ read -r local_backup_dir
174
+ scp -r $host_alias :$remote_files $local_backup_dir
175
+ }
176
+
177
+ # Function to sync directories with remote host
178
+ sync_directories () {
179
+ printf " %b\n" " Enter the local directory path: "
180
+ read -r local_dir
181
+ printf " %b\n" " Enter the alias of the host: "
182
+ read -r host_alias
183
+ printf " %b\n" " Enter the remote directory path: "
184
+ read -r remote_dir
185
+ rsync -avz $local_dir $host_alias :$remote_dir
186
+ }
187
+
188
+ # Function to check SSH key authentication status
189
+ check_ssh_key_authentication () {
190
+ printf " %b\n" " Enter the alias of the host: "
191
+ read -r host_alias
192
+ ssh $host_alias " grep '^PubkeyAuthentication' /etc/ssh/sshd_config"
193
+ }
194
+
195
+ # Function to show options for the user
196
+ show_menu () {
197
+ printf " %b\n" " Select an SSH operation:"
198
+ printf " %b\n" " 1. Add a new system"
199
+ printf " %b\n" " 2. Connect to a system"
200
+ printf " %b\n" " 3. Generate SSH key"
201
+ printf " %b\n" " 4. Share SSH key with remote host"
202
+ printf " %b\n" " 5. Disable password authentication on remote host"
203
+ printf " %b\n" " 6. Enable password authentication on remote host"
204
+ printf " %b\n" " 7. Check password authentication on remote host"
205
+ printf " %b\n" " 8. Check SSH key authentication status"
206
+ printf " %b\n" " 9. Run a command on remote host"
207
+ printf " %b\n" " 10. Copy a file to remote host"
208
+ printf " %b\n" " 11. Copy a directory to remote host"
209
+ printf " %b\n" " 12. Move a file to remote host (copy and delete local)"
210
+ printf " %b\n" " 13. Move a directory to remote host (copy and delete local)"
211
+ printf " %b\n" " 14. Remove a system from SSH configuration"
212
+ printf " %b\n" " 15. View SSH configuration"
213
+ printf " %b\n" " 16. Backup files from remote host"
214
+ printf " %b\n" " 17. Sync directories with remote host"
215
+ printf " %b\n" " 18. Exit"
216
+ printf " %b\n" " Enter your choice: "
217
+ }
218
+
219
+ # Function to execute the selected SSH operation
220
+ main () {
221
+ while true ; do
222
+ show_menu
223
+ read choice
224
+ case $choice in
225
+ 1) ask_for_host_details ;;
226
+ 2) show_available_hosts && printf " %b\n" " Enter the alias of the host to connect to: " && read -r host_alias; ssh $host_alias ;;
227
+ 3) generate_ssh_key ;;
228
+ 4) share_ssh_key ;;
229
+ 5) disable_password_auth ;;
230
+ 6) enable_password_auth ;;
231
+ 7) check_password_auth ;;
232
+ 8) check_ssh_key_authentication ;;
233
+ 9) run_remote_command ;;
234
+ 10) copy_file_to_remote ;;
235
+ 11) copy_directory_to_remote ;;
236
+ 12) move_file_to_remote ;;
237
+ 13) move_directory_to_remote ;;
238
+ 14) remove_system ;;
239
+ 15) view_ssh_config ;;
240
+ 16) backup_files ;;
241
+ 17) sync_directories ;;
242
+ 18) exit ;;
243
+ * ) printf " %b\n" " Invalid choice. Please try again." ;;
244
+ esac
245
+ done
246
+ }
247
+
248
+ checkEnv
249
+ checkEscalationTool
250
+ main
0 commit comments