bash script failing to use ssh -i. Can't see directory
Actually your resolved command isn't right. It's really this:
ssh -i "~/Keypairs/jadam-macbookpro-ec2-1.pem" "[email protected]"
The double quotes stop the shell expanding ~
and it's treated there as a literal (passed directly on to ssh
). Replace it with the environment variable $HOME
and the problem will resolve itself:
KEYPAIR="$HOME/Keypairs/jadam-macbookpro-ec2-1.pem"
sudo ssh -i "$KEYPAIR" "$DST" "$REMOTE_SCRIPT"
I should point out that ssh
will evaluate the ~
as home directory, but in this case it will be doing so in root's context rather than yours, so will not be looking for the file in your $HOME/.ssh
directory.
Related videos on Youtube
Joshua
Updated on September 18, 2022Comments
-
Joshua almost 2 years
I am experiencing something strange. I am working on a script to remote install Splunk on servers via a script. In this script I ssh to my practice servers (5 amazon EC2 instances running Redhat).
However, when I run the script I get this error:
Warning: Identity file ~/Keypairs/jadam-macbookpro-ec2-1.pem not accessible: No such file or directory.
My first step was to obviously make sure that the path was there. I copy the command run and paste it into my terminal. I successfully connect to my instance using the keypair.
Here is the command in question. I have tried it both with and without sudo prefixing the command:
KEYPAIR="~/Keypairs/jadam-macbookpro-ec2-1.pem" sudo ssh -i "$KEYPAIR" "$DST" "$REMOTE_SCRIPT"
REMOTE_SCRIPT is the script file to run, and DST is my server addresses pulled from a hosts file.
The resolved command is:
ssh -i ~/Keypairs/jadam-macbookpro-ec2-1.pem [email protected]
The script fails, but copying the command and doing it manually works. I thought it was a permissions issue at first, which is why I ran it with sudo, but it thinks the directory doesn't exist.
Any ideas?
-
jordanm about 9 yearsChange
~
to$HOME
.
-