-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh_auto_config.sh
More file actions
87 lines (79 loc) · 2.4 KB
/
ssh_auto_config.sh
File metadata and controls
87 lines (79 loc) · 2.4 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
#!/bin/bash
#
# Project: Linux Backup Script
# File: ssh_auto_config.sh
# Version: 1.0.0.37
# Date: 2026/04/05
# License: GNU GPL v3.0
# Developed by: Junon M. (2008-2026)
# Description: Backup Script with detailed logs.
#
public_key_access_only="1"
username_hostname="user@server.local"
# example
# exec_remote_cmd "ls -la /home/jr/Desktop/"
exec_remote_cmd() {
local cmd=$1
ssh -t ${username_hostname} "sudo bash -c \"${cmd}\""
}
make_public_key_and_send_to_remote() {
# Generate SSH keys if they don't exist
if [ ! -f ~/.ssh/id_rsa ]; then
ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
fi
# Copy the public key to the server
ssh-copy-id "${username_hostname}"
}
# Configure SSH to allow password authentication
ssh_config_with_password() {
local path="/etc/ssh/sshd_config"
ssh -t "${username_hostname}" "sudo -s <<EOF
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/g' ${path}
sed -i 's/#PasswordAuthentication no/PasswordAuthentication yes/g' ${path}
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' ${path}
sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication no/g' ${path}
sed -i 's/#PubkeyAuthentication no/PubkeyAuthentication no/g' ${path}
sed -i 's/PubkeyAuthentication yes/PubkeyAuthentication no/g' ${path}
echo
echo 'Begin remote ${path} file'
echo
cat '${path}'
echo
echo 'End remote ${path} file'
echo
service ssh restart
EOF"
}
# Configure SSH to allow public key authentication
ssh_config_with_public_key() {
local path="/etc/ssh/sshd_config"
make_public_key_and_send_to_remote
ssh -t "${username_hostname}" "sudo -s <<EOF
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/g' ${path}
sed -i 's/#PasswordAuthentication no/PasswordAuthentication no/g' ${path}
sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' ${path}
sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/g' ${path}
sed -i 's/#PubkeyAuthentication no/PubkeyAuthentication yes/g' ${path}
sed -i 's/PubkeyAuthentication no/PubkeyAuthentication yes/g' ${path}
echo
echo 'Begin remote ${path} file'
echo
cat '${path}'
echo
echo 'End remote ${path} file'
echo
service ssh restart
EOF"
}
if [ "$public_key_access_only" == "1" ]; then
ssh_config_with_public_key
echo
echo "Configured for public key access (no password, more secure)"
else
ssh_config_with_password
echo
echo "Successfully configured for password access"
fi
echo
echo "Press [ENTER] to exit..."
read