0

I am having a tough time figuring out a way to execute a .sh file present on my local linux machine on to a remote linux machine which happens to be an AWS ec2 instance. Here's what I am doing on my local machine:

ssh -i sample.pem ec2-user@server_name.amazon.com 'bash -s' < file_to_remotely_execute.sh

Error that I get is:

Warning: Identity file sample.pem not accessible: No such file or directory. Pseudo-terminal will not be allocated because stdin is not a terminal. Host key verification failed.

This was referred to arrive at the above approach.

Community
  • 1
  • 1
Praveen Pandey
  • 658
  • 3
  • 16
  • 32
  • The warning message tells you clearly what the problem is. You are providing an identity file, `sample.pem`, that doesn't exist, is not in the current directory or inaccessible for some other reason. But why are you attempting to provide an explicit identity file rather than use the default one that `ssh` will look for (usually in `$HOME/.ssh/`)? – kaylum Jul 14 '16 at 05:50
  • {sample.pem} is not the actual name. The real name is something else. Its just for illustration. This pem file is available in /home directory. Not sure why it is not accessible. If I do not use the pem file explicitly, it gives me 'permission denied (public key)' – Praveen Pandey Jul 14 '16 at 06:03
  • But are you running the `ssh` command from the same directory as the pem file? Please provide a run log showing the relevant info - like a listing of the current directory and then the ssh command invocaiton. – kaylum Jul 14 '16 at 06:05
  • Yes I am running ssh from the directory where pem file is present. – Praveen Pandey Jul 14 '16 at 06:20
  • Yes I am running ssh from the directory where pem file is present. Current directory is /home/PandePK. Items in it are: cftemplate.zip deploy.sh fipmt_server_qatst.pem local.profile.ORIG o.log output.log. My ssh command invocation is from /home/PandePK which is: ssh -i fipmt_server_qatst.pem ec2-user@ec2-user@server_name.amazon.com 'bash -s' < deploy.sh – Praveen Pandey Jul 14 '16 at 06:28
  • Your problem is caused by the ssh login and the command `'bash -s' < file_to_remotely_execute.sh` is right. Howere I think your title of this question is really irrelevant. So I -1 it, and you should open another new question. – Ekeyme Mo Jul 14 '16 at 07:27
  • I do not mind a down vote but more than that I will appreciate a solution that you can advise. – Praveen Pandey Jul 14 '16 at 08:14

1 Answers1

0

Possible issues :

  1. The PEM file doesn't exist.
  2. The PEM file doesn't have proper permissions.
  3. The host is not allowed in ~/.ssh/known_hosts file.

Solutions :

  1. Navigate to the location where PEM file exists or give full path of PEM file.
  2. Give 400 permissions to the PEM file, (sudo chmod 400 /path/to/file.pem).
  3. Login to the server by ssh once, and allow the host.
Deepak Chaudhary
  • 476
  • 5
  • 15
  • 1. The pem file exists. I even tried with full path but doesnt work 2. To give it permissions, I ran chmod 400 sample.pem 3. I opened ~/.ssh/know_hosts file and found that the my remote server is present in it. I have even tried logging into the remote server once through ssh and then ran ssh -i sample.pem ec2-user@server_name.amazon.com 'bash -s' < file_to_remotely_execute.sh. Still the same error :( – Praveen Pandey Jul 14 '16 at 08:13
  • Now the problem seems to be in your shell file, which needs Psudo-terminal. Try [This Workaround](http://stackoverflow.com/questions/7114990/pseudo-terminal-will-not-be-allocated-because-stdin-is-not-a-terminal) – Deepak Chaudhary Jul 14 '16 at 08:34
  • So according to your reference, I now have this as the command - `ssh -t -t -i fipmt_server_qatst.pem ec2-user@pmtserver-1.qa1tst.cltest.wellmanage.com 'bash -s' < deploy.sh` – Praveen Pandey Jul 14 '16 at 09:15
  • Output I get - `#!/bin/ksh ssh -i fipmt_server_qatst.pem ec2-user@pmtserver-1.qa1tst.cltest.wellmanage.com pwd > o.log [ec2-user@pmtserver-1 ~]$ #!/bin/ksh [ec2-user@pmtserver-1 ~]$ tst.cltest.wellmanage.com ssh -i fipmt_server_qatst.pem ec2-user@pmtserver-1.qa1 Warning: Identity file fipmt_server_qatst.pem not accessible: No such file or directory. The authenticity of host 'pmtserver-1.qa1tst.cltest.wellmanage.com (10.93.212.137)' can't be established. ECDSA key fingerprint is 57:84:3c:98:68:d4:53:7b:7e:a2:c9:17:42:e0:9e:74. Are you sure you want to continue connecting (yes/no)?` – Praveen Pandey Jul 14 '16 at 09:16
  • and then when I type in **yes** and hit enter, nothing happens. Its blank. – Praveen Pandey Jul 14 '16 at 09:17
  • What is the output of `ls -la fipmt_server_qatst.pem` (run it from the same folder where you tried ssh)? – Dusan Bajic Jul 14 '16 at 09:26
  • 2
    Try with a test script, like `echo "Hello"` in your **deploy.sh** and see if it runs correctly – Deepak Chaudhary Jul 14 '16 at 09:31
  • @DusanBajic: This is what I get - `-r-------- 1 PandePK develop 1692 May 4 2015 fipmt_server_qatst.pem` – Praveen Pandey Jul 14 '16 at 11:09
  • Are you perhaps trying to start another ssh session in your deploy.sh? – Dusan Bajic Jul 14 '16 at 11:27
  • This is what I have in deploy.sh `#!/bin/ksh ssh -i fipmt_server_qatst.pem ec2-user@pmtserver-1.qa1tst.cltest.wellmanage.com pwd > o.log` – Praveen Pandey Jul 14 '16 at 11:34
  • Oh sorry, I am doing another ssh. My bad – Praveen Pandey Jul 14 '16 at 11:35
  • But that means 'yes'. Your initial ssh connection succeeds, then on remote server you once again run `ssh -i ..` which fails because there is no fipmt_server_qatst.pem on remote server – Dusan Bajic Jul 14 '16 at 11:38
  • Thanks @DusanBajic. Just to keep things simple, can you please tell me how to test if I am able to successfully run deploy.sh script remotely from my local linux machine? – Praveen Pandey Jul 14 '16 at 11:43
  • 2
    remove `ssh -i ...` line from deploy.sh, and put `echo $HOSTNAME` line in deploy.sh. Then try again, you should see a remote hostname in your output – Dusan Bajic Jul 14 '16 at 11:54
  • Awesome!! @DusanBajic. Its working. Thank you so much! – Praveen Pandey Jul 14 '16 at 12:01
  • @DusanBajic, I'd suggest you to put up effort to write a detailed answer, so that others can easily get the workaround for the problem. Also OP, please do accept the answer. – Deepak Chaudhary Jul 14 '16 at 12:49