LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 04-27-2011, 08:22 AM   #1
salmanmanekia
LQ Newbie
 
Registered: Jul 2009
Posts: 13

Rep: Reputation: 0
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
 
Old 04-27-2011, 12:03 PM   #2
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
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
 
Old 04-27-2011, 01:35 PM   #3
salmanmanekia
LQ Newbie
 
Registered: Jul 2009
Posts: 13

Original Poster
Rep: Reputation: 0
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
 
Old 04-27-2011, 01:44 PM   #4
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
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
 
1 members found this post helpful.
Old 04-28-2011, 02:47 AM   #5
salmanmanekia
LQ Newbie
 
Registered: Jul 2009
Posts: 13

Original Poster
Rep: Reputation: 0
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 !..
 
Old 04-28-2011, 04:31 AM   #6
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
Quote:
Originally Posted by salmanmanekia View Post
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
 
Old 04-28-2011, 05:20 AM   #7
salmanmanekia
LQ Newbie
 
Registered: Jul 2009
Posts: 13

Original Poster
Rep: Reputation: 0
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// .....
 
Old 04-28-2011, 05:26 AM   #8
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Rep: Reputation: 12
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

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

Last edited by brownie_cookie; 04-28-2011 at 05:28 AM.
 
Old 04-28-2011, 07:41 AM   #9
salmanmanekia
LQ Newbie
 
Registered: Jul 2009
Posts: 13

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to run a script to a remote linux machine from a windows machine squirrel001 Linux - Newbie 5 05-04-2009 06:06 PM
Run application on *monitor* of remote machine via SSH? DiBosco Linux - Networking 5 04-09-2007 08:57 AM
running shell command on remote machine with ssh qrshat Solaris / OpenSolaris 3 08-17-2006 07:32 AM
telneting remote machine from a shell script sanjith11 Programming 11 05-26-2004 06:50 AM
How do I setup xwindow to run a remote desktop on my local machine through ssh. green-ears Linux - Newbie 4 11-04-2003 05:47 PM


All times are GMT -5. The time now is 03:40 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration