LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-27-2008, 07:06 AM   #1
gt2nv
LQ Newbie
 
Registered: Dec 2007
Posts: 5

Rep: Reputation: 0
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.
 
Old 08-27-2008, 07:11 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
What seems to be the problem that you are having? Do you need to know how to set up public key authentication?
 
Old 08-27-2008, 07:15 AM   #3
gt2nv
LQ Newbie
 
Registered: Dec 2007
Posts: 5

Original Poster
Rep: Reputation: 0
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.
 
Old 08-27-2008, 07:39 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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'
 
Old 08-27-2008, 07:41 AM   #5
gt2nv
LQ Newbie
 
Registered: Dec 2007
Posts: 5

Original Poster
Rep: Reputation: 0
I will give that a shot. Thank you so much!!
 
Old 08-27-2008, 07:43 AM   #6
gt2nv
LQ Newbie
 
Registered: Dec 2007
Posts: 5

Original Poster
Rep: Reputation: 0
that worked!!!! Thank you very much!!!!
 
Old 08-27-2008, 07:50 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 08-27-2008, 08:07 AM   #8
gt2nv
LQ Newbie
 
Registered: Dec 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
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!
 
Old 08-27-2008, 09:02 AM   #9
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
Quote:
Originally Posted by colucix View Post
[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 "-"....
 
Old 08-27-2008, 09:12 AM   #10
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
Check here http://tldp.org/LDP/abs/html/here-docs.html, example 18-4.
 
Old 08-27-2008, 09:13 AM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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!
 
Old 08-27-2008, 09:17 AM   #12
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
That leading tabs thing is great - I learned something awesome today!
 
Old 08-27-2008, 10:20 AM   #13
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
Quote:
Originally Posted by vladmihaisima View Post
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.
 
Old 08-27-2008, 10:32 AM   #14
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Quote:
Originally Posted by matthewg42 View Post
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.
 
Old 08-27-2008, 11:30 AM   #15
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
Thumbs up

Quote:
Originally Posted by colucix View Post
Indeed it is not useful in my example.
Sure it is usefull, at least to teach others.... So many comments about this trick !

cheers,
 
  


Reply

Tags
bash, scripting, ssh


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
bash scripting question johnpaulodonnell Linux - Newbie 7 06-20-2007 07:24 AM
Bash scripting question nitroid Programming 3 04-13-2006 07:08 AM
Bash, SSH and scripting. Namuna Linux - Newbie 2 06-08-2004 02:01 PM
bash scripting question mehesque Programming 2 03-07-2004 01:37 PM
Bash scripting... ssh? TheIrish Programming 4 11-27-2003 08:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration