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
Related
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
I have installed libpam-google-authenticator and freeradius on server ubuntu 16.0405. Everything works good, except for if I use the command google-auth in bash script I get a error message "google-auth: command not found"
But the same works if I put it on terminal directly.
#!/bin/bash
google-auth
That is not a bash script.
To make it a bash script, your first line needs to include a "#" as follows:
#!/bin/bash
google-auth
Also, you need to ensure that the script is executable:
chmod +x yourscript.sh
Hopefully that will solve your problem.
As per the comments below, it seems like the command "google-auth" was an alias which wasn't being established in the child shell.
I know that there are some related questions already about this, but can't make it work for me!
So I can run grep in the command line and it works fine, but if I do that on a bash script, I have the following error:
grep: secondword:No such file or directory
I am connecting via ssh to the server, then I run some commands. The path to grep in the server is /bin/grep, but still it does not work. Here is the sample code:
#!/bin/bash
$host="user#host";
ssh $host "
myinfo=\$(grep "word secondword" path/to/file);
"
I also verified that it does not have the CR that is created in Windows with Notepad++. Any ideas on how to fix this?
EDIT:
As suggested, I made the following change with the quotes:
#!/bin/bash
$host="user#host";
ssh $host "
myinfo=\$(grep \"word secondword\" path/to/file);
"
but now I have a very weird behavior: it looks like is listing all the files that are on the home server path.Doing echo to the variable:
file1 file2 file 3
file4 file5 etc.
Why it as this behavior? Did I miss something?
Please Put Script working Dir. While run crontab it will take as user home as default path.
#!/bin/bash
cd Your_Path
$host="user#host"
ssh $host
myinfo=\$(grep "word secondword" path/to/file)
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.
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.