forked from romainjouin/infrastructure_as_code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_00_exchange_key_linux.sh
executable file
·43 lines (39 loc) · 1.18 KB
/
01_00_exchange_key_linux.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
#exchange_key.sh
if [ $# -eq 0 ]
then
echo "You must supply the distant account and computer : account@ip as 1st paramter"
else
# 1) create a key
ssh-keygen -t rsa -f ~/.ssh/id_rsa
# 2) send the key to the distant server
ssh-copy-id $1
# 3) create an agent on your bashrc
echo '
SSH_ENV="$HOME/.ssh/environment"
function start_agent {
echo "Initialising new SSH agent..."
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo succeeded
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
/usr/bin/ssh-add;
}
# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" > /dev/null
#ps ${SSH_AGENT_PID} doesnt work under cywgin
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_agent;
}
else
start_agent;
fi
' >> ~/.bashrc
# 4) activate the agent
source ~/.bashrc
# 5) tell the agent wich secret he should know to become a secret agent.
ssh-add ~/.ssh/id_rsa
# 6) enjoy a free connection to the distant host
echo "Key added to remote server, and now you can simply go like ssh user@host and log without password required"
#ssh $1
fi