ssh can use in bash shell, can't use in shell file - bash

I use ssh to login EC2(Amazon)
ssh -i /home/XXX/mykey.pem ec2-user#ec2-NN-NNN-NN-NN.us-foo-N.compute.amazonaws.com
It success,but when I write the command to the file like this:
login.sh
#! /bin/bash
ssh -i /home/XXX/mykey.pem ec2-user#ec2-NN-NNN-NN-NN.us-foo-N.compute.amazonaws.com
after chmod +x login.sh
I run the script, it return:
ssh: Could not resolve hostname
So how to solve it, thanks.

There are two likely reasons:
You have a typo in the hostname in the script.
The script has a CRLF instead of LF as the line ending on the ssh line. This often happens when you edit the file on a Windows system and transfer it to Unix. Use dos2unix to fix the script.

Related

Run .sh file terminal [duplicate]

This question already has answers here:
Pass commands as input to another command (su, ssh, sh, etc)
(3 answers)
Difference between sh and Bash
(11 answers)
Closed last month.
I ran below command line from a terminal and it works:
sftp -i ~/.ssh/id_rsa username#host
put csv_file
put manifest_file
I want to automatically run this so I created a .sh file that looks like below:
#!/bin/bash
sftp -i ~/.ssh/id_rsa username#host
put csv_file
put manifest_file
and save it as run.sh. I ran it like below:
sh run.sh
It connect to the host but the next commands, the put lines, did not run.
Can I get insights why is that and how can I solve it?
It worked interactively because put commands were not consumed by shell but running sftp process. Script you have attempts to run put commands as standalone.
Redirection
To run put you can either feed them to standard input of the sftp:
#!/bin/bash
sftp -i ~/.ssh/id_rsa username#host <<END
put csv_file
put manifest_file
END
where <<END denotes take next lines until seeing END and redirect them (<<) to the standard input of sftp.
Batch File
Or alternatively if you can automate them via batch file called e.g. batch and containing simply the put lines:
put csv_file
put manifest_file
and then tell sftp it should process commands in batch file like this instead of having shell script:
sftp -i ~/.ssh/id_rsa username#host -b batch
NOTE: Since you have bash shebang in file you can make that file executable by chmod +x run.sh and then simply run it as ./run.sh.

Putty: trying to send multiple commands to remote server but only the first is executed [duplicate]

I want to run multiple commands automatically like sudo bash, ssh server01, ls , cd /tmp etc at server login..
I am using Remote command option under SSH in putty.
I tried multiple commands with delimiter && but not working.
There is a some information lacking in your question.
You say you want to run sudo bash, then ssh server01.
Will sudo prompt for a password in your remote server?
Assuming there is no password in sudo, running bash will open another shell waiting for user input. The command ssh server01 will not be run until that bash shell is exited.
If you want to run 2 commands, try first simpler ones like:
ls -l /tmp ; echo "hi there"
or if you prefer:
ls -l /tmp && echo "hi there"
Does this work?
If what you want is to run ssh after running bash, you can try :
sudo bash -c "ssh server01"
That is probably because the command is expected to be a program name followed by parameters, which will be passed directly to the program. In order to get && and other functionality that is provided by a command line interpreter such as bash, try this:
/bin/bash -c "command1 && command2"
I tried what I suggested in my previous answer.
It is possible to run 2 simple commands in putty separated by a semicolon. As in my example I tried with ls and echo. The remote server runs them and then the session closes.
I also tried to ssh to a remote server that is configured for not asking for a password. In that case, it also works, I get connected to the 2nd server and I can run commands on it. Upon exit, the 2 connections are closed.
So please, let us know what you actually need / want.
You can execute two consecutive commands in PuTTY using a regular shell syntax. E.g. using ; or &&.
But you want to execute ssh server01 in sudo bash shell, right?
These are not two consecutive commands, it's ssh server01 command executed within sudo bash.
So you have to use a sudo command-line syntax to execute the ssh server01, like
sudo bash ssh server01

BASH instead of CSH while running Commands on a Remote Linux Server over SSH

I would like to run a command on a remote server using ssh, under bash, while my default session is csh.
minimal example (true command is more complex and is generated by my IDE remote debugger):
ssh hostname 'ls | head'
I don't have admin privileges. Trying chsh -s /bin/bash results with an error chsh: cannot lock /etc/passwd; try again later.
I tried adding to .cshrc the following
setenv SHELL /bin/bash
exec /bin/bash --login
but it freezes the console when sending the command through ssh (while regular ssh works)
Any idea how to solve that?
NOTE: I must have a solution that would configure the host, because I don't have access to the ssh command which is generated automatically by the debugger of my IDE. On the IDE I can only set the host name and port number. (EDIT) Therefore solutions like ssh hostname '/bin/bash -c "ls | head"' wont apply
EDIT2:
Actual command shown by IDE (again, I can't edit it):
ssh://username#localhost:2213/home/lab/username/anaconda2/envs/tf_011b/bin/python -u /specific/a/home/cc/cs/username/.pycharm_helpers/pydev/pydevd.py --multiproc --qt-support --client '0.0.0.0' --port 41823 --file /home/lab/username/remote_py/nlteach/show_attend_and_tell/train_saat_classifier.py --train_dir=/home/lab/username/nlteach/output/train/d=cub/imSD=11%imSP=rnd%tcSP=cvpr16/CSat/res50%lr0_02LrDTexpLrDc0_938OrmspWDc0/emb=512%ldTrn=0%nU=512%noHid=1%lr=0_02%lrDT=fix%lrDc=1%o=rmsp/
I am not sure why, but on a bash enabled server it works, while it fails on the csh host.
Thanks!
Invoke bash on the remote side, telling it what commands to run:
ssh hostname '/bin/bash -c "ls | head"'
If the command is too complicated (eg because of quotation mark escaping), then write your commands to a script, copy the script, then run the script:
scp script.bash hostname:/tmp/
ssh hostname '/bin/bash /tmp/script.bash'

Unable to run ssh inside shell script

I am able to run ssh rach#kamel but if i try to put this in bash script, i get command not found.
Here is the bash script in question
#!/bin/bash
ssh rach#kamel
i also tried using kamel ip address instead, still same issue.
Update Here is the Exact message
$ ./devBuild.sh
: No such file or directory../
: command not found
Update Content of cat -A devBuild.sh
$ cat -A devBuild.sh
#!/bin/bash^M$
cd ../^M$
ssh adoshi#10.247.39.142 ^M$
Update : Now am getting
$ ./devBuild.sh
: hostname nor servname provided, or not known
^M$ is symptom of DOS line-end, fix that and you should be good.
Do you, by any chance, have the line endings wrong? Sometimes it happens that there is a \r\n at the end of lines instead of a mere \n. This makes the shebang (#!) line dysfunctional.
Could you post the result of cat -A dev.sh, especially the first line?
Is your script executable? If not use chmod +x dev.sh

bash command doesnt seem to work, but its echo does?

Well, I'm new to linux so this may be a very newbie kinda of thing, here it goes:
I have a script in which I'm trying to send some different jobs to remote computers (in fact Amazon's EC2 instances), these jobs are in fact the same function which I run with different parameters.
eventually in the script code I have this line:
nohup ssh -fqi key.pem ubuntu#${Instance_Id[idx]} $tmp
if I do:
echo nohup ssh -fqi key.pem ubuntu#${Instance_Id[idx]} $tmp
I get:
nohup ssh -fqi key.pem ubuntu#ec2-72-44-41-228.compute-1.amazonaws.com '(nohup ./Script.sh 11 1&)'
Now the weird thing. If I run the code with no echo in the script it doesnt work! it says in the nohup.out (in my laptop, no nohup.out is created in the remote instance) bash: (nohup ./Script.sh 10 1&): No such file or directory
The file does exist locally and remotely and is chmod +x.
If I simply run the very same script with an echo in front of the problematic line and copy its output and paste in the terminal, it works!.
Any clues welcome, thanks!
Try removing the single quotes from $tmp. It looks like bash is treating (nohup ./Script.sh 10 1&) as the command with no parameters, but technically nohup is the command with the parameters ./Script.sh 10 1.
The problem is the single quotes around the nohup command in your $tmp variable. These don't get used on the shell locally, so SSH passes them verbatim. This means remotely the ssh server tries to interpret (nohup ./Script.sh 10 1&) as a command (looks for a file named that) which there clearly isn't. Make sure you remove the single quotes in $tmp.

Resources