ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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 "-"....
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!
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.