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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-27-2011, 08:22 AM
|
#1
|
|
LQ Newbie
Registered: Jul 2009
Posts: 13
Rep:
|
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
|
|
|
|
04-27-2011, 12:03 PM
|
#2
|
|
Guru
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 18 with Awesome WM
Posts: 6,796
|
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
|
|
|
|
04-27-2011, 01:35 PM
|
#3
|
|
LQ Newbie
Registered: Jul 2009
Posts: 13
Original Poster
Rep:
|
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
|
|
|
|
04-27-2011, 01:44 PM
|
#4
|
|
Guru
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 18 with Awesome WM
Posts: 6,796
|
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:
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.
|
04-28-2011, 02:47 AM
|
#5
|
|
LQ Newbie
Registered: Jul 2009
Posts: 13
Original Poster
Rep:
|
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 !..
|
|
|
|
04-28-2011, 04:31 AM
|
#6
|
|
Guru
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 18 with Awesome WM
Posts: 6,796
|
Quote:
Originally Posted by salmanmanekia
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
|
|
|
|
04-28-2011, 05:20 AM
|
#7
|
|
LQ Newbie
Registered: Jul 2009
Posts: 13
Original Poster
Rep:
|
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 ..
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// .....
|
|
|
|
04-28-2011, 05:26 AM
|
#8
|
|
Member
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Rep:
|
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
Last edited by brownie_cookie; 04-28-2011 at 05:28 AM.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:20 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|