LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to use ssh and run command in the remote machine using shell script (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-use-ssh-and-run-command-in-the-remote-machine-using-shell-script-877395/)

salmanmanekia 04-27-2011 08:22 AM

How to use ssh and run command in the remote machine using shell script
 
Hi All,
As mentioned in the Subject ...How can i do that.. Today is my first day writing a script.. and correct me if this Thread should be somewhere else..I rarely use this forum.
Code:

if [ $1=$anyvariable ]; then
    ssh www.a.com  <Have to set few variables and give commands >

Thanks

EricTRA 04-27-2011 12:03 PM

Hello,

SSH gives you the possibility to execute commands from the command line like this:
Code:

ssh user@server 'your command; other command; next command'
or another way
Code:

ssh user@server <<<EOF
command1
command2
command3
EOF

Best practice is to open a session at your remote server and execute the commands you want to execute, next copy them in another terminal and execute them as indicated above for example to see if you get the desired output.

Some examples and tips:
Right here at LQ
Using SSH from Bash
http://tldp.org/LDP/abs/html/communications.html#REMOTE

Kind regards,

Eric

salmanmanekia 04-27-2011 01:35 PM

Thanks EricTRA but this doesnt answer my question.
You mentioned how in the terminal ssh can be done.. but what i wanted to ask is how in script you can give some command (like setenv or ls -a)intended for the remote pc after connecting to the remote terminal through ssh..
i tried ls -a in the below mentioned manner but it printed out the content of my local folder...whereas, i anticipated it show the contents of the current remote folder..

Code:

if [ $1=$anyvariable ]; then
    ssh www.a.com  ls -a # i also tried it with single and double quotations

Thanks

EricTRA 04-27-2011 01:44 PM

Hello,

There is basically not that much difference between executing SSH commands from the console or from within a shell script. I don't think you've read through the links I posted since all of them contain examples on how to put SSH with remote commands in a script. Also have a second look at the console examples I posted, both of them mention
Code:

ssh user@host 'command'
yet you keep executing

Code:

ssh www.a.com 'command'
where is that hostname (FQDN) pointing to? Defined in your hosts file to your localhost or another server you have remote access to and that has SSH installed and correctly configured?

For example, if you would like to put the following you execute from a terminal in a Bash script:
Code:

ssh user@host 'ls -l'
you would do that like this:
Code:

#!/bin/bash

ssh user@host 'ls -l'

make the script, for example if you named it sshtest.sh, executable:
Code:

chmod +x sshtest.sh
and run the script:
Code:

./sshtest.sh
If the command from the terminal gave you an output then the script will give you the same output. I told you in my first reply to login at the remote server, execute a command to see if it gives you the output you want and close the session. Next run the same command in combination with ssh as I indicated from your terminal to see if you get the same output and as a last point put it in a script and run the script. The first two steps are just to troubleshoot, if any errors arise, before executing a script to connect to a remote server which may or may not show you any errors, depending on how you've written your script.

Kind regards,

Eric

salmanmanekia 04-28-2011 02:47 AM

Thanks EricTRA,
Quote:

Best practice is to open a session at your remote server and execute the commands you want to execute
You were right when you mentioned about the best practices .. My problem isnt with the script because when i ssh like
ssh emulator 'ls -a'
it connects to the emulator and disconnects it after showing me the contents of the directory.. I dont want it to be disconnected..
Quote:

I don't think you've read through the links I posted since all of them contain examples on how to put SSH with remote commands in a script
Ya,initially it just skipped me ,i didnt saw it ...and now i have just gone through it quickly to find the solution but didnt had any hint from the link you posted ..
Quote:

Also have a second look at the console examples I posted, both of them mention
Code:

ssh user@host 'command'

yet you keep executing

Code:

ssh www.a.com 'command'

where is that hostname (FQDN) pointing to? Defined in your hosts file to your localhost or another server you have remote access to and that has SSH installed and correctly configured?
I dont understand completely the above statement ,i mean what difference does it make to have the www.a.com sort of statement or user@host.. I was given the command like the one i mentioned for connecting and working on that machine and i have been using it since then (i.e for last one month) and
also what would be the command to see where the host name is pointing too ?
Quote:

Next run the same command in combination with ssh as I indicated from your terminal to see if you get the same output and as a last point put it in a script and run the script. The first two steps are just to troubleshoot
this is the problem.. ssh and command dont work together from the terminal while seperately it works fine !..

EricTRA 04-28-2011 04:31 AM

Quote:

Originally Posted by salmanmanekia (Post 4338967)
Thanks EricTRA,

You were right when you mentioned about the best practices .. My problem isnt with the script because when i ssh like
ssh emulator 'ls -a'
it connects to the emulator and disconnects it after showing me the contents of the directory.. I dont want it to be disconnected..

If you don't want to be disconnected then what's the use of a script? Just login and execute your commands. I don't understand completely what you're trying to obtain I guess. I've given you options you can use to run multiple commands in the same session and pointed you to several examples. Unless if you want to connect to several servers automatically and stay logged in until you exit the session. That's completely different since you will not be passing any commands from the script itself.
Quote:

Ya,initially it just skipped me ,i didnt saw it ...and now i have just gone through it quickly to find the solution but didnt had any hint from the link you posted ..
No harm done :)
Quote:

I dont understand completely the above statement ,i mean what difference does it make to have the www.a.com sort of statement or user@host.. I was given the command like the one i mentioned for connecting and working on that machine and i have been using it since then (i.e for last one month) and also what would be the command to see where the host name is pointing too?
Well there is no difference if you have an alias for your command configured or if you have a configured .ssh/config file that points to that hosts. If you don't then by default SSH will try connect to that host with your username. If that username exists on the remote host no problem but if it doesn't then you'll error out.
Quote:

this is the problem.. ssh and command dont work together from the terminal while seperately it works fine !..
Now I don't understand what you mean. Can you clarify what you mean by that statement?

Kind regards,

Eric

salmanmanekia 04-28-2011 05:20 AM

Quote:

Now I don't understand what you mean. Can you clarify what you mean by that statement?
I mean if i give the ssh command first and then ls -a then it connects first and shows me the content from the second statement and doesnt disconnect ..
Quote:

.ssh/config
just out of curiosity where would this be located.. ?
Quote:

Unless if you want to connect to several servers automatically and stay logged in until you exit the session. That's completely different since you will not be passing any commands from the script itself.
The first part of the above statement is what i am looking to do ..I will explain to you briefly what i am planning to do in the script and why i thought script would be a good idea for this....
first i want to ssh ..then set some environment variables and paths in the remote machine and then i want to issue few ssh connection originating from the remote prompt without disconnecting these connections until ctrl+c ... :)
i will explain how i am doing it in the terminal now so its more clear
Code:

[local/home/blabla]ssh connection
>set env ...
>set env ...
>set path ...
and then
>param -ssh -exportEnv X ,license file ,symbol dir ,conifg -S //process detail// -C //process detail// .....


brownie_cookie 04-28-2011 05:26 AM

you can do that normally with one ssh command
Code:

ssh user@host 'command1; command2; command3; ...'
if you put all those commands after eachother, like i showed, all those commands will be done on the same remote host without disconnecting
After all the commands are done, then he disconnects

NORMALLY :p

EDIT
you can also do this in a script

script.sh
Code:

ssh user@host 'command1; command2; command3; ...'
CMD
Code:

# chmod 755 script.sh
Code:

# ./script.sh

salmanmanekia 04-28-2011 07:41 AM

Thanks brownie but this doesnt help...Please read my post before your http://www.linuxquestions.org/questi...5/#post4339110


All times are GMT -5. The time now is 10:02 AM.