-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgensshkey
More file actions
executable file
·28 lines (20 loc) · 814 Bytes
/
gensshkey
File metadata and controls
executable file
·28 lines (20 loc) · 814 Bytes
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
#!/usr/bin/env bash
# Ensure you have ssh-keygen installed
# Check if an SSH host is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 hostname_or_user@hostname"
exit 1
fi
SSH_HOST=$1 # SSH host provided as an argument
# Check if SSH_HOST contains '@'
if [[ $SSH_HOST == *"@"* ]]; then
SERVER_NAME=$(echo "$SSH_HOST" | cut -d '@' -f 2) # Extract server name after '@'
else
SERVER_NAME=$SSH_HOST # Use the SSH_HOST as the server name
fi
KEY_NAME=id_rsa_$SERVER_NAME # Key name based on server name
# Generate a new SSH key without a passphrase in the current directory
ssh-keygen -t rsa -b 4096 -N "" -f "$KEY_NAME"
# Manually copy the SSH key to the server
ssh "$SSH_HOST" "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys" < "$KEY_NAME".pub
echo "SSH key generated and manually copied to $SERVER_NAME"