LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 12-05-2015, 09:09 AM   #1
iFunction
Member
 
Registered: Nov 2015
Posts: 248

Rep: Reputation: Disabled
how to run a command remotely through a script


Hi there,

I am writing a script that will shut down a cluster of raspberry pi's.

So far I have 2 Raspberry Pi's,:
pi_one - with keyboard, mouse and screen
pi_two - headless

A key-gen has been generated so pi_one can log into pi_two automatically.

The script connects and logs in successfully from pi_one to pi_two via ssh, but I can't work how to get it to run the command.

I am trying to get pi_one to shutdown pi_two:
Code:
#/bin/bash
clear

ssh pi@192.168.0.1 # Thanks to keygen it logs in automatically

sudo reboot
initially it logs in but just opens up an ssh session with pi_two. When I exit the ssh session, the next line executes closing down the pi_one from which I am using. What do I need to get the next line to execute the reboot command in the pi that I am connecting to?

Regards
iFunc
 
Old 12-05-2015, 09:56 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Put the sudo reboot on the same line as the ssh.

Since a new line marks the end of a command, your current script has sudo reboot executed by the local shell running on pi_one, not by ssh.

Last edited by berndbausch; 12-05-2015 at 10:01 AM.
 
1 members found this post helpful.
Old 12-05-2015, 10:42 AM   #3
iFunction
Member
 
Registered: Nov 2015
Posts: 248

Original Poster
Rep: Reputation: Disabled
Thank you, such a simple thing, and actually I did read it in the bash how to manual, but didn't put two and two together.
 
Old 12-05-2015, 10:51 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by iFunction View Post
Thank you, such a simple thing, and actually I did read it in the bash how to manual, but didn't put two and two together.
The man page for SSH has further details, but something like:
Code:
ssh pi@192.168.0.1 "sudo shutdown"
ssh pi2@192.168.0.2 "sudo shutdown"
<repeat until you shut them ALL down>
would be what berndbausch is talking about.
 
Old 12-05-2015, 11:30 AM   #5
iFunction
Member
 
Registered: Nov 2015
Posts: 248

Original Poster
Rep: Reputation: Disabled
Yes, that was what I had in mind.

I am curious about the use of Quotation marks. Both:
Code:
ssh pi@192.168.0.1 sudo shutdown
and
Code:
ssh pi@192.168.0.1 'sudo shutdown'
works and I am assuming:
Code:
ssh pi@ 192.168.0.1 "sudo shutdown"
will also work.

What is the difference please?

Regards
iFunc
 
Old 12-06-2015, 05:16 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
as you can see in this case there is no difference.
But in general see man bash, look for quoting
 
Old 12-06-2015, 05:55 AM   #7
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by iFunction View Post
Yes, that was what I had in mind.

I am curious about the use of Quotation marks. Both:
Code:
ssh pi@192.168.0.1 sudo shutdown
and
Code:
ssh pi@192.168.0.1 'sudo shutdown'
works and I am assuming:
Code:
ssh pi@ 192.168.0.1 "sudo shutdown"
will also work.

What is the difference please?
Single quotes are "stronger" than double quotes. In particular, the dollar sign inside double quotes retains its special meaning, not so in single quotes.
The remote shell will receive the string that comes after the IP address in your command. With or without quotes, it will parse it correctly. In your example, no difference.

Here is an example where you need quotes:
Code:
ssh pi@192.168.0.1 sh -c "echo hi there"
Whatever comes after the -c must be a single parameter, thus the space must be quoted. It also works this way, but is harder to read:
Code:
ssh pi@192.168.0.1 sh -c echo\ hi\ there
See the two bash links in my signature.
 
2 members found this post helpful.
Old 12-06-2015, 08:03 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Well said, berndbausch.
 
Old 12-06-2015, 10:31 AM   #9
iFunction
Member
 
Registered: Nov 2015
Posts: 248

Original Poster
Rep: Reputation: Disabled
Thanks berndbausch, gives me a good starting point.
 
  


Reply



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
Exiting a script run remotely Grumpy.inkedup.biker Linux - Server 1 08-23-2013 07:31 AM
Unable to run another command from within shell script, when run from home directory blackray1 Linux - Newbie 7 07-01-2013 09:42 AM
run a windows exe remotely from a linux command line? Springs Linux - General 4 01-06-2013 07:37 AM
How to run a command remotely fw12 Linux - General 12 11-21-2006 02:18 AM
Running a shell script remotely via 'ssh <hostname> <command>' davee Linux - General 4 10-09-2005 01:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:07 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