The Adventures of Systems Boy!

Confessions of a Mac SysAdmin...

Send Remote Commands Via SSH

This is one of those "I'm posting it so I remember, 'cause I keep forgetting" posts. It's also astoundingly cool, though, if you didn't know about it. Which I didn't until fairly recently.

If you've ever wanted to send a command to a remote computer without ever actually logging in to that computer, ssh is your friend. Yes, with ssh you can send commands directly to another system. Who knew?

I'll keep this short and sweet. Here are some examples.

The basic form looks something like this:
ssh systemsboy@rhost.systemsboy.edu 'ls -l'


where "systemsboy" is actually your username on the remote host, and "rhost.systemsboy.edu" is your remote system. The command you're sending is contained in single quotes.

Here is an example sending multiple commands:
ssh systemsboy@rhost.systemsboy.edu 'ls -l; ps -aux; whoami'


wherein each command is separated by a semicolon.

Finally, here is an example sending a command that requires user interaction:
ssh -t systemsboy@rhost.systemsboy.edu 'top'


Note the -t flag. That tells ssh that you'll be interacting with remote shell. Without the -t flag top will return results after which ssh will log you out of the remote host immediately. With the -t flag, ssh keeps you logged in until you exit the interactive command. The -t flag can be used with most interactive commands, including text editors like pico and vi.

Sending remote commands via ssh is incredibly handy when writing shell scripts as it allows you to run your scripts locally even if those scripts are meant to effect changes on a remote machine. I just wrote a script, for instance, that sets up vacation mail forwarding for staff members. Without these remote commands I would have had to have staff members log directly onto the mail server and run the scripts from the command line, which I don't think they'd be too happy about. With ssh remote commands, I can give them the scripts and they can run them right from their Desktops. Believe me, they much prefer this.

Credit where due, all information was obtained from Rambo's post. Rambo, thanks. Whoever you are.

UPDATE:
For additional information about using this trick with variables within the remote command, see "Using SSH to Send Variables in Scripts."

Labels: , ,

« Home | Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »

5:35 PM

I've been using SSH for years and I never knew about this feature until this January when I attended the Macworld Expo IT conference. This is one coolest things I've ever heard of. (Almost as cool as electric cars!) And I can't believe no one really has talked about it. It's just so darn freaking cool. And like you said works well for those scripts to have other users execute what you've prepared for them.    



5:24 PM

I think I found the origin of that info that "Rambo" sourced. It's half in German and half in English, but all good stuff.

http://www.akadia.com/services/unix_tools.html    



7:10 PM

Ach! Nein! I can't read those pages! (Well, most of them anyway.)

I'm glad I'm not the only one who was unaware of this SSH superpower. Maybe it's just one of those things everyone already knows. But man is it handy!

-systemsboy    



1:05 PM

Here you have some info about how to set up the ssh connection using keys with restrictions:

command="/path/to/ilium/rdistd -S" NNNN MM 89012[bulk still gone for the sake of example]34567 priam@sparta

so that only a certain command be called while using this key. very neat.    



1:08 PM

Ashley,

Excellent! Thank you!

-systemboy    



8:29 PM

Hi,

I'm writing a small python script, which should ssh to 5 diff boxes and get the result for uptime from each box. I want to run this as a daemon process so that I know the least loaded box at any time.

This can be easily done using a simple for loop around -
load = str(commands.getoutput("ssh " + machine[0]+ " uptime"))

But the problem arises when 1 of the 5 remote machines is down. The script just waits for a response from that machine and does not move on to the next box

How do I instruct the ssh cmd to timeout after say 1 min?

This can be achieved using a Timer thread in the python script, but it would be much easier if ssh had a switch for the same.

Unfortunately I can't locate anything in the man pages for ssh

BTW I'm using -
$ ssh -V
OpenSSH_3.6.1p2, SSH protocols 1.5/2.0, OpenSSL 0x0090701f
$

Thanks for your help    



3:42 PM

when u have more then one server to do that too...... with a good naming convention you can laways use the for command
for i in 1 2 3 4 5 6 7 8 9 10 11 ; do ssh admin@fileserver$i.domain.com 'date' 'cd / ; du -sh /* ; done

etc etc    



3:51 PM

Yup. Nothing like a good ol' for loop. You could do similar with a simple list of computer names:
for computer in `cat computers.txt`; do ssh -t user@$computer.domain 'ls /Applications'; done

Love it!

-systemsboy    



1:22 AM

How do I accomplish the same task but after I terminate the command, I want to remain logged into the terminal?    



7:53 PM

Razi,

The end of the command should not log you out of the Terminal. It should only log you out of the machine you're SSHing to. If you don't want to log out of that machine, there's not really any reason to use this method. Just SSH into the machine and run your command(s). Then log out when you're good and ready.

Or, to answer your question more directly, I don't know of a way to do this, but I don't really know why you'd need to either.

-systemsboy    



6:00 PM

With openssh you can set a ConnectTimeout...
ssh -o ConnectTimeout=8 blah blah

Timeout is in seconds.    



» Post a Comment