-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_ssh_keys_on_remote.sh
More file actions
executable file
·52 lines (43 loc) · 1.34 KB
/
setup_ssh_keys_on_remote.sh
File metadata and controls
executable file
·52 lines (43 loc) · 1.34 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
#!/bin/bash
echo "Welcome to my dope SSH server setup script!"
# Function to generate SSH key
generate_ssh_key() {
echo "Enter a name for the key file"
read -r keyName
identityFileLocation="$HOME/.ssh/$keyName"
ssh-keygen -t rsa -N "" -f "$identityFileLocation"
}
# Function to send public key to remote host's authorized_keys
send_public_key() {
echo "Enter the Username for the remote host"
read -r remoteUser
echo "Enter the IP Address for the remote host"
read -r remoteIpAddress
cat "$HOME/.ssh/$keyName.pub" | ssh "$remoteUser@$remoteIpAddress" "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
}
# Function to set up local config file
setup_local_config_file() {
configFile="$HOME/.ssh/config"
{
echo ""
echo "Host $keyName $remoteIpAddress"
echo -e "\tHostName $remoteIpAddress"
echo -e "\tIdentityFile $identityFileLocation"
echo -e "\tUser $remoteUser"
echo ""
} >>"$configFile"
}
# Function to test SSH connection with key authentication
test_ssh_connection() {
echo "If connection below succeeds, ssh key authentication was successful"
ssh "$keyName" "echo ssh key authentication is successful"
}
# Main function
main() {
generate_ssh_key
send_public_key
setup_local_config_file
test_ssh_connection
}
# Execute the main function
main