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.
|
What seems to be the problem that you are having? Do you need to know how to set up public key authentication?
|
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. |
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' |
I will give that a shot. Thank you so much!!
|
that worked!!!! Thank you very much!!!!
|
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 |
Quote:
|
Quote:
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 "-".... |
Check here http://tldp.org/LDP/abs/html/here-docs.html, example 18-4.
|
Indeed it is not useful in my example. From the Advanced Bash Scripting Guide:
Quote:
|
That leading tabs thing is great - I learned something awesome today!
|
Quote:
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. |
Quote:
|
Quote:
cheers, |
All times are GMT -5. The time now is 03:38 PM. |