-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharif.sh
49 lines (42 loc) · 1.17 KB
/
sharif.sh
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
#! /bin/bash
filename="id_rsa"
path="$HOME/.ssh"
if [ $1 ]
then
hostname=$1
if [ $2 ]
then
username=$2
else
username="$USER"
fi
else
# Read the host and username to store public key (the host/username accepting passwordless ssh from this computer)
echo "What host you want to access with passwordless SSH from this computer?"
read hostname
echo "What is your username on $hostname? ($USER?)"
read username
if [ ! $username ]
then
username="$USER"
fi
fi
# Generate rsa files
if [ -f $path/$filename ]
then
echo "RSA key exists on $path/$filename, using existing file"
else
ssh-keygen -t rsa -f "$path/$filename"
echo RSA key pair generated
fi
echo "We need to log into $hostname as $username to set up your public key (hopefully last time you'll use password from this computer)"
cat "$path/$filename.pub" | ssh "$hostname" -l "$username" '[ -d .ssh ] || mkdir .ssh; cat >> .ssh/authorized_keys; chmod 700 ~/.ssh; chmod 600 ~/.ssh/authorized_keys'
status=$?
if [ $status -eq 0 ]
then
echo "Set up complete, try to ssh to $host now"
exit 0
else
echo "an error has occured"
exit 255
fi