LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Question about ssh and bash scripting...... (http://www.linuxquestions.org/questions/programming-9/question-about-ssh-and-bash-scripting-665754/)

gt2nv 08-27-2008 07:06 AM

Question about ssh and bash scripting......
 
I am trying to write a bash script that will ssh to a machine, run a command, display the result back to me and then exit. Right now i am not having much luck, and was wanting to know if i could get some help. Thanks in advance.

matthewg42 08-27-2008 07:11 AM

What seems to be the problem that you are having? Do you need to know how to set up public key authentication?

gt2nv 08-27-2008 07:15 AM

Here is what i added to my script.


#!/bin/bash
#
#
ssh -p22 machinex
pdsh -w server[1-4] uptime
exit
exit 0

The only thing that it is doing is sshing to machinex and no executing the command or exiting. Maybe i am trying to do something that is not possible. I am kinda new to bash scripting.

matthewg42 08-27-2008 07:39 AM

You can provide remote host commands on the invocation line for ssh.

You might want to enclose the whole command in single quotes to prevent the local shell from expanding the glob pattern "server[1-4]" (in the case where there exists some local files which match this pattern).

There is also no need to exit from the ssh connection - when you provide a command in this way, the ssh session terminates when the command is complete.

The -p22 in your example is redundant, as port 22 is the default port for ssh.

Code:

ssh user@host 'pdsh -w server[1-4] uptime'

gt2nv 08-27-2008 07:41 AM

I will give that a shot. Thank you so much!!

gt2nv 08-27-2008 07:43 AM

that worked!!!! Thank you very much!!!!

colucix 08-27-2008 07:50 AM

I would add that if you want to run more than one command, you can concatenate them using logical operators, like && and || or put them in a "here document" like this:
Code:

ssh user@host <<-EOF
  cd /path/to/dir
  whoami
  pdsh -w server[1-4] uptime
  cd /path/to/some/other/dir
  exit
EOF

Moreover, to avoid interactive input for password, you have to setup a public/private key authentication, as already suggested by matthewg42.

gt2nv 08-27-2008 08:07 AM

Quote:

Originally Posted by colucix (Post 3261355)
I would add that if you want to run more than one command, you can concatenate them using logical operators, like && and || or put them in a "here document" like this:
Code:

ssh user@host <<-EOF
  cd /path/to/dir
  whoami
  pdsh -w server[1-4] uptime
  cd /path/to/some/other/dir
  exit
EOF

Moreover, to avoid interactive input for password, you have to setup a public/private key authentication, as already suggested by matthewg42.

Thanks, i will give that a shot!

marozsas 08-27-2008 09:02 AM

Quote:

Originally Posted by colucix (Post 3261355)
[CODE]ssh user@host <<-EOF

Hi Colucix !
Great tip.
But I was unable to find in documentation (I guess in the bash documentation) the meaning of that "-" in front of "EOF". Can you explain it, please ?

Your code works even in the absence of that "-"....

vladmihaisima 08-27-2008 09:12 AM

Check here http://tldp.org/LDP/abs/html/here-docs.html, example 18-4.

colucix 08-27-2008 09:13 AM

Indeed it is not useful in my example. From the Advanced Bash Scripting Guide:
Quote:

The − option to mark a here document limit string (<<−LimitString) suppresses leading tabs (but not
spaces) in the output. This may be useful in making a script more readable.
This means you can safely indent statements inside a here document using TABs, since they are stripped out by the shell and not passed as input. One of the many nuances in shell scripting! :)

matthewg42 08-27-2008 09:17 AM

That leading tabs thing is great - I learned something awesome today!

marozsas 08-27-2008 10:20 AM

Quote:

Originally Posted by vladmihaisima (Post 3261470)

Thanks, I learn one more thing today. :) great !

by the way, are your name a Hungary name ? My first name in hungary is "Mihay" my last name means "Rose" in hungary, and I have a close Hungary friend which his last name is "Sima" too...so it is too much coincidences to not be a hungary name....sorry if not.

raconteur 08-27-2008 10:32 AM

Quote:

Originally Posted by matthewg42 (Post 3261476)
That leading tabs thing is great - I learned something awesome today!

Yep, same here (pun intended ;) ) I've always left-justified my here documents and that does add some obfuscation to indented code. How did I miss this for so long, I wonder? Thanks for the tip.

marozsas 08-27-2008 11:30 AM

Quote:

Originally Posted by colucix (Post 3261472)
Indeed it is not useful in my example.

Sure it is usefull, at least to teach others.... So many comments about this trick !

cheers,


All times are GMT -5. The time now is 05:42 AM.