1

The below command works fine from my localhost and I get the desired output. See below

$ ssh -o ConnectTimeout=7  -o BatchMode=yes root@10.9.60.26 "echo WORKS;exit"
Output:
WORKS

Now, I put this command in a script file test.sh having sufficient execution privileges

$ cat test.sh
segment=$(ssh -o ConnectTimeout=7  -o BatchMode=yes root@10.9.60.26 "echo WORKS;exit")
echo "SEGMENT $segment"

I don't get the desired output. Infact I get the below error:

$ ./test.sh
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
SEGMENT

I was expecting

SEGMENT WORKS

Now, the Permission denied error goes away but

Can you please suggest what could be the issue ?

AdminBee
  • 22,803
Ashar
  • 491
  • How are you running your script (crontab?)? How are you authenticating with the remote system (public key in a ssh-agent?)? – Kusalananda Sep 24 '20 at 08:37
  • @Kusalananda i updated my original post to answer your question. – Ashar Sep 24 '20 at 08:51
  • I got the problem. alias ssh with private key ssh -i /<path_to_private_key> was set is what it was working on local command line but in the script I had to specify the private key explicitly. – Ashar Sep 24 '20 at 08:58
  • Yes, aliases are not expanded in bash scripts. See e.g. https://unix.stackexchange.com/questions/368246/cant-use-alias-in-script-even-if-i-define-it-just-above – Kusalananda Sep 24 '20 at 09:04
  • @Ashar you should write you solution as an answer, this might help someone in same situation. – Archemar Sep 24 '20 at 09:14

1 Answers1

1

The issue was that I used an alias

alias ssh=ssh -i /app/private_key

stored in the user profile. However, this setting doesn't take effect in a shell script (see here e.g.), so I had to explicitly call

ssh -i app/private_key

in the script. This resolved the issue.

AdminBee
  • 22,803
Ashar
  • 491