Running ssh-agent from a shell scriptprobems using ssh from cronssh fails to execute remote command when run...
Use comma instead of & in table
Is divide-by-zero a security vulnerability?
Is there a frame of reference in which I was born before I was conceived?
If a druid in Wild Shape swallows a creature whole, then turns back to her normal form, what happens?
Accessing something inside the object when you don't know the key
How can atoms be electrically neutral when there is a difference in the positions of the charges?
Navigating through USB drive with "cd /myOtherFolder" causes "no such directory" error
How to count words in a line
How to properly claim credit for peer review?
Can I become debt free or should I file for bankruptcy? How do I manage my debt and finances?
Replacement ford fiesta radiator has extra hose
How to count occurrences of Friday 13th
Why do members of Congress in committee hearings ask witnesses the same question multiple times?
I am on the US no-fly list. What can I do in order to be allowed on flights which go through US airspace?
What is a term for a function that when called repeatedly, has the same effect as calling once?
Linear regression when Y is bounded and discrete
Magento 2: Override XML file from vendor folder to app folder doesn't work/update
Compare four integers, return word based on maximum
What's the difference between a cart and a wagon?
What is better: yes / no radio, or simple checkbox?
What to do when being responsible for data protection in your lab, yet advice is ignored?
Second-rate spelling
How to avoid being sexist when trying to employ someone to function in a very sexist environment?
Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?
Running ssh-agent from a shell script
probems using ssh from cronssh fails to execute remote command when run from cron bash script - works from CLISSH-agent & SSH-add at rebootHow to add SSH key to PuTTY Agent at startup in Windows Server?Reverse ssh-agent forwarding. Using a remote ssh keyDiscrepancy in ssh-agent behaviorStraight forward way to run ssh-agent and ssh-add on login via SSH?shell script for running ssh-agentBash script as use shell not working with SSHAllowing SSH To use ssh-agent in bash script
I'm trying to create a shell script that, among other things, starts up ssh-agent and adds a private key to the agent. Example:
#!/bin/bash
# ...
ssh-agent $SHELL
ssh-add /path/to/key
# ...
The problem with this is ssh-agent apparently kicks off another instance of $SHELL (in my case, bash) and from the script's perspective it's executed everything and ssh-add and anything below it is never run.
How can I run ssh-agent from my shell script and keep it moving on down the list of commands?
ssh bash shell shell-scripting ssh-agent
add a comment |
I'm trying to create a shell script that, among other things, starts up ssh-agent and adds a private key to the agent. Example:
#!/bin/bash
# ...
ssh-agent $SHELL
ssh-add /path/to/key
# ...
The problem with this is ssh-agent apparently kicks off another instance of $SHELL (in my case, bash) and from the script's perspective it's executed everything and ssh-add and anything below it is never run.
How can I run ssh-agent from my shell script and keep it moving on down the list of commands?
ssh bash shell shell-scripting ssh-agent
add a comment |
I'm trying to create a shell script that, among other things, starts up ssh-agent and adds a private key to the agent. Example:
#!/bin/bash
# ...
ssh-agent $SHELL
ssh-add /path/to/key
# ...
The problem with this is ssh-agent apparently kicks off another instance of $SHELL (in my case, bash) and from the script's perspective it's executed everything and ssh-add and anything below it is never run.
How can I run ssh-agent from my shell script and keep it moving on down the list of commands?
ssh bash shell shell-scripting ssh-agent
I'm trying to create a shell script that, among other things, starts up ssh-agent and adds a private key to the agent. Example:
#!/bin/bash
# ...
ssh-agent $SHELL
ssh-add /path/to/key
# ...
The problem with this is ssh-agent apparently kicks off another instance of $SHELL (in my case, bash) and from the script's perspective it's executed everything and ssh-add and anything below it is never run.
How can I run ssh-agent from my shell script and keep it moving on down the list of commands?
ssh bash shell shell-scripting ssh-agent
ssh bash shell shell-scripting ssh-agent
asked Oct 22 '13 at 20:57
DanDan
3371511
3371511
add a comment |
add a comment |
8 Answers
8
active
oldest
votes
ssh-agent is supposed to start a session and when it finishes the user session is over. So any command after ssh-agent would perhaps be executed after logoff.
What you want is a session-script
that contains your sessions commands like this:
#!/bin/bash
ssh-add /path/to/key
bash -i # or other session starter
Then start ssh-agent session-script
.
Thanks! Creating a separate script and ending the script withexit
did the trick.
– Dan
Oct 23 '13 at 16:29
add a comment |
Put the following at the top of your script:
eval `ssh-agent`
Your script should look like this:
#!/bin/bash
eval `ssh-agent`
ssh-add /path/to/key
...
...
Explanation
The backticks around ssh-agent
collect its output. eval
collects that output, concatenates it into a single command, and then executes the command. Then you can use ssh-add
to provide your key credentials.
8
This is exactly what I needed, thanks, although worth pointing out that backticks are on the way out. In the new bash form, it should beeval $(ssh-agent)
– sibaz
Jan 4 '16 at 13:01
This solution didn't work for me until I putbash -i
at the end of the script.
– Adolfo Correa
Sep 9 '18 at 23:52
add a comment |
I tend to do something like this in scripts that require an agent.
#!/bin/bash
# if we can't find an agent, start one, and restart the script.
if [ -z "$SSH_AUTH_SOCK" ] ; then
exec ssh-agent bash -c "ssh-add ; $0"
exit
fi
... and so on.
Basically the first thing the script does it check to see if an agent is running. If it isn't exec is used to start a new process in place of the script. The agent is started, keys are added, and finally, the script is called again (see the $0
).
But that will not preserve any script parameters. And if any of the parameters has whitespace, it won't be easy to pass them along.
– Denilson Sá Maia
Aug 7 '14 at 14:24
2
You could use.. "ssh-add ; $0 $*"
, or.. "ssh-add ; $0 $@"
instead, which may work. Which wouldn't be perfect, but would certainly work in many cases. The best solution is almost always to have your agent running before anything else anyway, this is just something that might be useful in obscure cases.
– Zoredache
Aug 7 '14 at 16:40
add a comment |
It is better to use keychain in this case
Debian/Ubuntu:
apt-get install keychain
RHEL/Fedora/CentOS
yum install keychain
Add in your .bashrc the following:
eval `keychain --eval id_rsa`
Better? Why is it better?
– JFlo
Oct 3 '17 at 18:24
@JFlo "Better" in that, it will save the env variables to $HOME/.keychain/<file>. Running that command again will pickup an existing ssh-agent if it is still running. It can then be reused between shells/scripts. In some scenarios that isn't super safe, so you have to make that call. For me, it is an improvement over some scripts I'd written to accomplish the same task
– Scott Carlson
Aug 6 '18 at 13:06
add a comment |
I found this works for me.
eval `ssh-agent` # create the process
ssh-add ~/.ssh/priv_key # add the key
git -C $repo_dir pull # this line is the reason for the ssh-agent
eval `ssh-agent -k` # kill the process
I create the ssh-agent process, add the key, do what I need to do, then kill it. No need to check if it's running later.
add a comment |
I found with Zoredache's solution, the key would be available to any shell that happens to share the same ssh-agent as the shell that called the script. I wanted to avoid this in a script that required root access to a remote machine, for obvious security reasons.
I've found putting the following shebang at the top of the script works:
#!/usr/bin/ssh-agent bash
ssh-add /path/to/ssh-key
ssh root@remotehost "remote commands"
add a comment |
None of these solutions worked aparently for me.
I tried multiple variations of the solutions provided by the experts above.
#!/usr/bin/ssh-agent bash
#
# if we can't find an agent, start one, and restart the script.
# if [ -z "$SSH_AUTH_SOCK" ] ; then
# exec ssh-agent bash -c "ssh-add /home/username/.ssh/user_rsa ; $0"
# exit
# fi
#eval `ssh-agent`
ssh-add /home/username/.ssh/user_rsa
ssh-add -l
However I if I just run these commands manually, it works for me. wondering what is going wrong here.
eval `ssh-agent`
ssh-add /home/username/.ssh/user_rsa
ssh-add -l
appreciate any help you can offer.
New contributor
add a comment |
I've tried and lot and the solution that finally worked was replacing my passphrase with an empty string.
ssh-keygen -p
This is a very unsafe practice. Why bother using ssh at all? If you don't protect your private key, you might as well be talking in clear text.
– JFlo
Aug 7 '18 at 14:47
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "2"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f547923%2frunning-ssh-agent-from-a-shell-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
ssh-agent is supposed to start a session and when it finishes the user session is over. So any command after ssh-agent would perhaps be executed after logoff.
What you want is a session-script
that contains your sessions commands like this:
#!/bin/bash
ssh-add /path/to/key
bash -i # or other session starter
Then start ssh-agent session-script
.
Thanks! Creating a separate script and ending the script withexit
did the trick.
– Dan
Oct 23 '13 at 16:29
add a comment |
ssh-agent is supposed to start a session and when it finishes the user session is over. So any command after ssh-agent would perhaps be executed after logoff.
What you want is a session-script
that contains your sessions commands like this:
#!/bin/bash
ssh-add /path/to/key
bash -i # or other session starter
Then start ssh-agent session-script
.
Thanks! Creating a separate script and ending the script withexit
did the trick.
– Dan
Oct 23 '13 at 16:29
add a comment |
ssh-agent is supposed to start a session and when it finishes the user session is over. So any command after ssh-agent would perhaps be executed after logoff.
What you want is a session-script
that contains your sessions commands like this:
#!/bin/bash
ssh-add /path/to/key
bash -i # or other session starter
Then start ssh-agent session-script
.
ssh-agent is supposed to start a session and when it finishes the user session is over. So any command after ssh-agent would perhaps be executed after logoff.
What you want is a session-script
that contains your sessions commands like this:
#!/bin/bash
ssh-add /path/to/key
bash -i # or other session starter
Then start ssh-agent session-script
.
answered Oct 22 '13 at 21:18
Michael SuelmannMichael Suelmann
31614
31614
Thanks! Creating a separate script and ending the script withexit
did the trick.
– Dan
Oct 23 '13 at 16:29
add a comment |
Thanks! Creating a separate script and ending the script withexit
did the trick.
– Dan
Oct 23 '13 at 16:29
Thanks! Creating a separate script and ending the script with
exit
did the trick.– Dan
Oct 23 '13 at 16:29
Thanks! Creating a separate script and ending the script with
exit
did the trick.– Dan
Oct 23 '13 at 16:29
add a comment |
Put the following at the top of your script:
eval `ssh-agent`
Your script should look like this:
#!/bin/bash
eval `ssh-agent`
ssh-add /path/to/key
...
...
Explanation
The backticks around ssh-agent
collect its output. eval
collects that output, concatenates it into a single command, and then executes the command. Then you can use ssh-add
to provide your key credentials.
8
This is exactly what I needed, thanks, although worth pointing out that backticks are on the way out. In the new bash form, it should beeval $(ssh-agent)
– sibaz
Jan 4 '16 at 13:01
This solution didn't work for me until I putbash -i
at the end of the script.
– Adolfo Correa
Sep 9 '18 at 23:52
add a comment |
Put the following at the top of your script:
eval `ssh-agent`
Your script should look like this:
#!/bin/bash
eval `ssh-agent`
ssh-add /path/to/key
...
...
Explanation
The backticks around ssh-agent
collect its output. eval
collects that output, concatenates it into a single command, and then executes the command. Then you can use ssh-add
to provide your key credentials.
8
This is exactly what I needed, thanks, although worth pointing out that backticks are on the way out. In the new bash form, it should beeval $(ssh-agent)
– sibaz
Jan 4 '16 at 13:01
This solution didn't work for me until I putbash -i
at the end of the script.
– Adolfo Correa
Sep 9 '18 at 23:52
add a comment |
Put the following at the top of your script:
eval `ssh-agent`
Your script should look like this:
#!/bin/bash
eval `ssh-agent`
ssh-add /path/to/key
...
...
Explanation
The backticks around ssh-agent
collect its output. eval
collects that output, concatenates it into a single command, and then executes the command. Then you can use ssh-add
to provide your key credentials.
Put the following at the top of your script:
eval `ssh-agent`
Your script should look like this:
#!/bin/bash
eval `ssh-agent`
ssh-add /path/to/key
...
...
Explanation
The backticks around ssh-agent
collect its output. eval
collects that output, concatenates it into a single command, and then executes the command. Then you can use ssh-add
to provide your key credentials.
edited Jan 4 '16 at 15:15
answered Jul 14 '15 at 14:29
scottyseusscottyseus
304139
304139
8
This is exactly what I needed, thanks, although worth pointing out that backticks are on the way out. In the new bash form, it should beeval $(ssh-agent)
– sibaz
Jan 4 '16 at 13:01
This solution didn't work for me until I putbash -i
at the end of the script.
– Adolfo Correa
Sep 9 '18 at 23:52
add a comment |
8
This is exactly what I needed, thanks, although worth pointing out that backticks are on the way out. In the new bash form, it should beeval $(ssh-agent)
– sibaz
Jan 4 '16 at 13:01
This solution didn't work for me until I putbash -i
at the end of the script.
– Adolfo Correa
Sep 9 '18 at 23:52
8
8
This is exactly what I needed, thanks, although worth pointing out that backticks are on the way out. In the new bash form, it should be
eval $(ssh-agent)
– sibaz
Jan 4 '16 at 13:01
This is exactly what I needed, thanks, although worth pointing out that backticks are on the way out. In the new bash form, it should be
eval $(ssh-agent)
– sibaz
Jan 4 '16 at 13:01
This solution didn't work for me until I put
bash -i
at the end of the script.– Adolfo Correa
Sep 9 '18 at 23:52
This solution didn't work for me until I put
bash -i
at the end of the script.– Adolfo Correa
Sep 9 '18 at 23:52
add a comment |
I tend to do something like this in scripts that require an agent.
#!/bin/bash
# if we can't find an agent, start one, and restart the script.
if [ -z "$SSH_AUTH_SOCK" ] ; then
exec ssh-agent bash -c "ssh-add ; $0"
exit
fi
... and so on.
Basically the first thing the script does it check to see if an agent is running. If it isn't exec is used to start a new process in place of the script. The agent is started, keys are added, and finally, the script is called again (see the $0
).
But that will not preserve any script parameters. And if any of the parameters has whitespace, it won't be easy to pass them along.
– Denilson Sá Maia
Aug 7 '14 at 14:24
2
You could use.. "ssh-add ; $0 $*"
, or.. "ssh-add ; $0 $@"
instead, which may work. Which wouldn't be perfect, but would certainly work in many cases. The best solution is almost always to have your agent running before anything else anyway, this is just something that might be useful in obscure cases.
– Zoredache
Aug 7 '14 at 16:40
add a comment |
I tend to do something like this in scripts that require an agent.
#!/bin/bash
# if we can't find an agent, start one, and restart the script.
if [ -z "$SSH_AUTH_SOCK" ] ; then
exec ssh-agent bash -c "ssh-add ; $0"
exit
fi
... and so on.
Basically the first thing the script does it check to see if an agent is running. If it isn't exec is used to start a new process in place of the script. The agent is started, keys are added, and finally, the script is called again (see the $0
).
But that will not preserve any script parameters. And if any of the parameters has whitespace, it won't be easy to pass them along.
– Denilson Sá Maia
Aug 7 '14 at 14:24
2
You could use.. "ssh-add ; $0 $*"
, or.. "ssh-add ; $0 $@"
instead, which may work. Which wouldn't be perfect, but would certainly work in many cases. The best solution is almost always to have your agent running before anything else anyway, this is just something that might be useful in obscure cases.
– Zoredache
Aug 7 '14 at 16:40
add a comment |
I tend to do something like this in scripts that require an agent.
#!/bin/bash
# if we can't find an agent, start one, and restart the script.
if [ -z "$SSH_AUTH_SOCK" ] ; then
exec ssh-agent bash -c "ssh-add ; $0"
exit
fi
... and so on.
Basically the first thing the script does it check to see if an agent is running. If it isn't exec is used to start a new process in place of the script. The agent is started, keys are added, and finally, the script is called again (see the $0
).
I tend to do something like this in scripts that require an agent.
#!/bin/bash
# if we can't find an agent, start one, and restart the script.
if [ -z "$SSH_AUTH_SOCK" ] ; then
exec ssh-agent bash -c "ssh-add ; $0"
exit
fi
... and so on.
Basically the first thing the script does it check to see if an agent is running. If it isn't exec is used to start a new process in place of the script. The agent is started, keys are added, and finally, the script is called again (see the $0
).
answered Oct 22 '13 at 21:14
ZoredacheZoredache
111k30230377
111k30230377
But that will not preserve any script parameters. And if any of the parameters has whitespace, it won't be easy to pass them along.
– Denilson Sá Maia
Aug 7 '14 at 14:24
2
You could use.. "ssh-add ; $0 $*"
, or.. "ssh-add ; $0 $@"
instead, which may work. Which wouldn't be perfect, but would certainly work in many cases. The best solution is almost always to have your agent running before anything else anyway, this is just something that might be useful in obscure cases.
– Zoredache
Aug 7 '14 at 16:40
add a comment |
But that will not preserve any script parameters. And if any of the parameters has whitespace, it won't be easy to pass them along.
– Denilson Sá Maia
Aug 7 '14 at 14:24
2
You could use.. "ssh-add ; $0 $*"
, or.. "ssh-add ; $0 $@"
instead, which may work. Which wouldn't be perfect, but would certainly work in many cases. The best solution is almost always to have your agent running before anything else anyway, this is just something that might be useful in obscure cases.
– Zoredache
Aug 7 '14 at 16:40
But that will not preserve any script parameters. And if any of the parameters has whitespace, it won't be easy to pass them along.
– Denilson Sá Maia
Aug 7 '14 at 14:24
But that will not preserve any script parameters. And if any of the parameters has whitespace, it won't be easy to pass them along.
– Denilson Sá Maia
Aug 7 '14 at 14:24
2
2
You could use
.. "ssh-add ; $0 $*"
, or .. "ssh-add ; $0 $@"
instead, which may work. Which wouldn't be perfect, but would certainly work in many cases. The best solution is almost always to have your agent running before anything else anyway, this is just something that might be useful in obscure cases.– Zoredache
Aug 7 '14 at 16:40
You could use
.. "ssh-add ; $0 $*"
, or .. "ssh-add ; $0 $@"
instead, which may work. Which wouldn't be perfect, but would certainly work in many cases. The best solution is almost always to have your agent running before anything else anyway, this is just something that might be useful in obscure cases.– Zoredache
Aug 7 '14 at 16:40
add a comment |
It is better to use keychain in this case
Debian/Ubuntu:
apt-get install keychain
RHEL/Fedora/CentOS
yum install keychain
Add in your .bashrc the following:
eval `keychain --eval id_rsa`
Better? Why is it better?
– JFlo
Oct 3 '17 at 18:24
@JFlo "Better" in that, it will save the env variables to $HOME/.keychain/<file>. Running that command again will pickup an existing ssh-agent if it is still running. It can then be reused between shells/scripts. In some scenarios that isn't super safe, so you have to make that call. For me, it is an improvement over some scripts I'd written to accomplish the same task
– Scott Carlson
Aug 6 '18 at 13:06
add a comment |
It is better to use keychain in this case
Debian/Ubuntu:
apt-get install keychain
RHEL/Fedora/CentOS
yum install keychain
Add in your .bashrc the following:
eval `keychain --eval id_rsa`
Better? Why is it better?
– JFlo
Oct 3 '17 at 18:24
@JFlo "Better" in that, it will save the env variables to $HOME/.keychain/<file>. Running that command again will pickup an existing ssh-agent if it is still running. It can then be reused between shells/scripts. In some scenarios that isn't super safe, so you have to make that call. For me, it is an improvement over some scripts I'd written to accomplish the same task
– Scott Carlson
Aug 6 '18 at 13:06
add a comment |
It is better to use keychain in this case
Debian/Ubuntu:
apt-get install keychain
RHEL/Fedora/CentOS
yum install keychain
Add in your .bashrc the following:
eval `keychain --eval id_rsa`
It is better to use keychain in this case
Debian/Ubuntu:
apt-get install keychain
RHEL/Fedora/CentOS
yum install keychain
Add in your .bashrc the following:
eval `keychain --eval id_rsa`
answered Jun 12 '15 at 19:42
ZIADI Mohamed aliZIADI Mohamed ali
412
412
Better? Why is it better?
– JFlo
Oct 3 '17 at 18:24
@JFlo "Better" in that, it will save the env variables to $HOME/.keychain/<file>. Running that command again will pickup an existing ssh-agent if it is still running. It can then be reused between shells/scripts. In some scenarios that isn't super safe, so you have to make that call. For me, it is an improvement over some scripts I'd written to accomplish the same task
– Scott Carlson
Aug 6 '18 at 13:06
add a comment |
Better? Why is it better?
– JFlo
Oct 3 '17 at 18:24
@JFlo "Better" in that, it will save the env variables to $HOME/.keychain/<file>. Running that command again will pickup an existing ssh-agent if it is still running. It can then be reused between shells/scripts. In some scenarios that isn't super safe, so you have to make that call. For me, it is an improvement over some scripts I'd written to accomplish the same task
– Scott Carlson
Aug 6 '18 at 13:06
Better? Why is it better?
– JFlo
Oct 3 '17 at 18:24
Better? Why is it better?
– JFlo
Oct 3 '17 at 18:24
@JFlo "Better" in that, it will save the env variables to $HOME/.keychain/<file>. Running that command again will pickup an existing ssh-agent if it is still running. It can then be reused between shells/scripts. In some scenarios that isn't super safe, so you have to make that call. For me, it is an improvement over some scripts I'd written to accomplish the same task
– Scott Carlson
Aug 6 '18 at 13:06
@JFlo "Better" in that, it will save the env variables to $HOME/.keychain/<file>. Running that command again will pickup an existing ssh-agent if it is still running. It can then be reused between shells/scripts. In some scenarios that isn't super safe, so you have to make that call. For me, it is an improvement over some scripts I'd written to accomplish the same task
– Scott Carlson
Aug 6 '18 at 13:06
add a comment |
I found this works for me.
eval `ssh-agent` # create the process
ssh-add ~/.ssh/priv_key # add the key
git -C $repo_dir pull # this line is the reason for the ssh-agent
eval `ssh-agent -k` # kill the process
I create the ssh-agent process, add the key, do what I need to do, then kill it. No need to check if it's running later.
add a comment |
I found this works for me.
eval `ssh-agent` # create the process
ssh-add ~/.ssh/priv_key # add the key
git -C $repo_dir pull # this line is the reason for the ssh-agent
eval `ssh-agent -k` # kill the process
I create the ssh-agent process, add the key, do what I need to do, then kill it. No need to check if it's running later.
add a comment |
I found this works for me.
eval `ssh-agent` # create the process
ssh-add ~/.ssh/priv_key # add the key
git -C $repo_dir pull # this line is the reason for the ssh-agent
eval `ssh-agent -k` # kill the process
I create the ssh-agent process, add the key, do what I need to do, then kill it. No need to check if it's running later.
I found this works for me.
eval `ssh-agent` # create the process
ssh-add ~/.ssh/priv_key # add the key
git -C $repo_dir pull # this line is the reason for the ssh-agent
eval `ssh-agent -k` # kill the process
I create the ssh-agent process, add the key, do what I need to do, then kill it. No need to check if it's running later.
answered Oct 14 '15 at 19:43
steampoweredsteampowered
3052622
3052622
add a comment |
add a comment |
I found with Zoredache's solution, the key would be available to any shell that happens to share the same ssh-agent as the shell that called the script. I wanted to avoid this in a script that required root access to a remote machine, for obvious security reasons.
I've found putting the following shebang at the top of the script works:
#!/usr/bin/ssh-agent bash
ssh-add /path/to/ssh-key
ssh root@remotehost "remote commands"
add a comment |
I found with Zoredache's solution, the key would be available to any shell that happens to share the same ssh-agent as the shell that called the script. I wanted to avoid this in a script that required root access to a remote machine, for obvious security reasons.
I've found putting the following shebang at the top of the script works:
#!/usr/bin/ssh-agent bash
ssh-add /path/to/ssh-key
ssh root@remotehost "remote commands"
add a comment |
I found with Zoredache's solution, the key would be available to any shell that happens to share the same ssh-agent as the shell that called the script. I wanted to avoid this in a script that required root access to a remote machine, for obvious security reasons.
I've found putting the following shebang at the top of the script works:
#!/usr/bin/ssh-agent bash
ssh-add /path/to/ssh-key
ssh root@remotehost "remote commands"
I found with Zoredache's solution, the key would be available to any shell that happens to share the same ssh-agent as the shell that called the script. I wanted to avoid this in a script that required root access to a remote machine, for obvious security reasons.
I've found putting the following shebang at the top of the script works:
#!/usr/bin/ssh-agent bash
ssh-add /path/to/ssh-key
ssh root@remotehost "remote commands"
edited Feb 25 '15 at 10:11
answered Feb 24 '15 at 18:01
Andy WoodAndy Wood
214
214
add a comment |
add a comment |
None of these solutions worked aparently for me.
I tried multiple variations of the solutions provided by the experts above.
#!/usr/bin/ssh-agent bash
#
# if we can't find an agent, start one, and restart the script.
# if [ -z "$SSH_AUTH_SOCK" ] ; then
# exec ssh-agent bash -c "ssh-add /home/username/.ssh/user_rsa ; $0"
# exit
# fi
#eval `ssh-agent`
ssh-add /home/username/.ssh/user_rsa
ssh-add -l
However I if I just run these commands manually, it works for me. wondering what is going wrong here.
eval `ssh-agent`
ssh-add /home/username/.ssh/user_rsa
ssh-add -l
appreciate any help you can offer.
New contributor
add a comment |
None of these solutions worked aparently for me.
I tried multiple variations of the solutions provided by the experts above.
#!/usr/bin/ssh-agent bash
#
# if we can't find an agent, start one, and restart the script.
# if [ -z "$SSH_AUTH_SOCK" ] ; then
# exec ssh-agent bash -c "ssh-add /home/username/.ssh/user_rsa ; $0"
# exit
# fi
#eval `ssh-agent`
ssh-add /home/username/.ssh/user_rsa
ssh-add -l
However I if I just run these commands manually, it works for me. wondering what is going wrong here.
eval `ssh-agent`
ssh-add /home/username/.ssh/user_rsa
ssh-add -l
appreciate any help you can offer.
New contributor
add a comment |
None of these solutions worked aparently for me.
I tried multiple variations of the solutions provided by the experts above.
#!/usr/bin/ssh-agent bash
#
# if we can't find an agent, start one, and restart the script.
# if [ -z "$SSH_AUTH_SOCK" ] ; then
# exec ssh-agent bash -c "ssh-add /home/username/.ssh/user_rsa ; $0"
# exit
# fi
#eval `ssh-agent`
ssh-add /home/username/.ssh/user_rsa
ssh-add -l
However I if I just run these commands manually, it works for me. wondering what is going wrong here.
eval `ssh-agent`
ssh-add /home/username/.ssh/user_rsa
ssh-add -l
appreciate any help you can offer.
New contributor
None of these solutions worked aparently for me.
I tried multiple variations of the solutions provided by the experts above.
#!/usr/bin/ssh-agent bash
#
# if we can't find an agent, start one, and restart the script.
# if [ -z "$SSH_AUTH_SOCK" ] ; then
# exec ssh-agent bash -c "ssh-add /home/username/.ssh/user_rsa ; $0"
# exit
# fi
#eval `ssh-agent`
ssh-add /home/username/.ssh/user_rsa
ssh-add -l
However I if I just run these commands manually, it works for me. wondering what is going wrong here.
eval `ssh-agent`
ssh-add /home/username/.ssh/user_rsa
ssh-add -l
appreciate any help you can offer.
New contributor
New contributor
answered 1 hour ago
GrayGray
1
1
New contributor
New contributor
add a comment |
add a comment |
I've tried and lot and the solution that finally worked was replacing my passphrase with an empty string.
ssh-keygen -p
This is a very unsafe practice. Why bother using ssh at all? If you don't protect your private key, you might as well be talking in clear text.
– JFlo
Aug 7 '18 at 14:47
add a comment |
I've tried and lot and the solution that finally worked was replacing my passphrase with an empty string.
ssh-keygen -p
This is a very unsafe practice. Why bother using ssh at all? If you don't protect your private key, you might as well be talking in clear text.
– JFlo
Aug 7 '18 at 14:47
add a comment |
I've tried and lot and the solution that finally worked was replacing my passphrase with an empty string.
ssh-keygen -p
I've tried and lot and the solution that finally worked was replacing my passphrase with an empty string.
ssh-keygen -p
answered Aug 21 '14 at 7:40
Stephan WeinholdStephan Weinhold
12616
12616
This is a very unsafe practice. Why bother using ssh at all? If you don't protect your private key, you might as well be talking in clear text.
– JFlo
Aug 7 '18 at 14:47
add a comment |
This is a very unsafe practice. Why bother using ssh at all? If you don't protect your private key, you might as well be talking in clear text.
– JFlo
Aug 7 '18 at 14:47
This is a very unsafe practice. Why bother using ssh at all? If you don't protect your private key, you might as well be talking in clear text.
– JFlo
Aug 7 '18 at 14:47
This is a very unsafe practice. Why bother using ssh at all? If you don't protect your private key, you might as well be talking in clear text.
– JFlo
Aug 7 '18 at 14:47
add a comment |
Thanks for contributing an answer to Server Fault!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f547923%2frunning-ssh-agent-from-a-shell-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown