# Net::SSH serialized commands

**URL:** <https://www.ruby-forum.com/t/net-ssh-serialized-commands/157710>\
**Category:** Ruby\
**Created:** [January 14, 2009, 6:05pm UTC](https://www.ruby-forum.com/t/net-ssh-serialized-commands/157710 "2009-01-14T18:05:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyrics](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/13edae/32.png) [@lyrics](https://www.ruby-forum.com/u/lyrics)\
**Post date:** [January 14, 2009, 6:05pm UTC](https://www.ruby-forum.com/t/net-ssh-serialized-commands/157710/1 "2009-01-14T18:05:28Z")

</div>

Hi,  
I am using Net::SSH v2  
I want to execute a command when the first one is complete.  
But I can’t make it working.  
Here is my code:

Net::SSH.start( addr, user, :password =\> passwd) do |session|  
session.exec! (“service X restart”)  
session.exec!("")  
end

The issue is that the second “session.exec!” is executed before  
service is restarted.

I guess I am not doing the right way but I don’t know how to do it.  
Can someone help me,please?

lyrics

---

<div class="post-metadata">

**Author:** ![lyrics](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/13edae/32.png) [@lyrics](https://www.ruby-forum.com/u/lyrics)\
**Post date:** [January 14, 2009, 10:13pm UTC](https://www.ruby-forum.com/t/net-ssh-serialized-commands/157710/2 "2009-01-14T22:13:53Z")

</div>

lyrics wrote:

> Hi,  
> I am using Net::SSH v2  
> I want to execute a command when the first one is complete.  
> But I can’t make it working.  
> Here is my code:
> 
> Net::SSH.start( addr, user, :password =\> passwd) do |session|  
> session.exec! (“service X restart”)  
> session.exec!("")  
> end
> 
> The issue is that the second “session.exec!” is executed before  
> service is restarted.
> 
> I guess I am not doing the right way but I don’t know how to do it.  
> Can someone help me,please?
> 
> lyrics

Not sure if this would accomplish what you are looking for, but you  
could try just doing this:

Net::SSH.start( addr, user, :password =\> passwd) do |session|  
session.exec! ("service X restart; ")  
end

---

<div class="post-metadata">

**Author:** ![lyrics](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/13edae/32.png) [@lyrics](https://www.ruby-forum.com/u/lyrics)\
**Post date:** [January 14, 2009, 11:21pm UTC](https://www.ruby-forum.com/t/net-ssh-serialized-commands/157710/3 "2009-01-14T23:21:00Z")

</div>

On 14 jan, 22:12, “Sebastian W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end  
> > could try just doing this:
> 
> ## Net::SSH.start( addr, user, :password =\> passwd) do |session| session.exec! (“service X restart; ”) end
> 
> Posted viahttp://www.ruby-forum.com/.

Thanks for replying.  
The solution isn’t suitable.  
I need to execute other commands on another ssh connexion.  
Actually, my code is more like this:

Net::SSH.start( addr, user, :password =\> passwd) do |session1|  
Net::SSH.start( addr2, user2, :password =\> passwd2) do |session2|  
session1.exec! (“service X restart;”)  
session2.exec! (“\<command…\>”)  
session1.exec! (“\<command …\>”)  
end  
end

---

<div class="post-metadata">

**Author:** ![lyrics](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/13edae/32.png) [@lyrics](https://www.ruby-forum.com/u/lyrics)\
**Post date:** [January 14, 2009, 11:32pm UTC](https://www.ruby-forum.com/t/net-ssh-serialized-commands/157710/4 "2009-01-14T23:32:05Z")

</div>

lyrics wrote:

> On 14 jan, 22:12, “Sebastian W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > end  
> > > could try just doing this:
> > 
> > ## Net::SSH.start( addr, user, :password =\> passwd) do |session| ï¿½session.exec! (“service X restart; ”) end
> > 
> > Posted viahttp://www.ruby-forum.com/.
> 
> Thanks for replying.  
> The solution isn’t suitable.  
> I need to execute other commands on another ssh connexion.  
> Actually, my code is more like this:
> 
> Net::SSH.start( addr, user, :password =\> passwd) do |session1|  
> Net::SSH.start( addr2, user2, :password =\> passwd2) do |session2|  
> session1.exec! (“service X restart;”)  
> session2.exec! (“\<command…\>”)  
> session1.exec! (“\<command …\>”)  
> end  
> end

Hm…that looks a bit odd to me. Why do the sessions have to be nested?

If you need to do more advanced stuff - which it sounds like - “exec!”  
probably will not suffice as it is just a convenience method.

Here is the Net::SSHv2 docs, they’re very helpful:  
[http://net-ssh.rubyforge.org/ssh/v2/api/index.html](http://net-ssh.rubyforge.org/ssh/v2/api/index.html)

What you are likely looking for is to use Net::SSH::Multi (maybe), build  
up an aggregated event loop, and then run it with the “loop” method.

---

<div class="post-metadata">

**Author:** ![lyrics](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/13edae/32.png) [@lyrics](https://www.ruby-forum.com/u/lyrics)\
**Post date:** [January 15, 2009, 12:30am UTC](https://www.ruby-forum.com/t/net-ssh-serialized-commands/157710/5 "2009-01-15T00:30:58Z")

</div>

lyrics wrote:

> On 14 jan, 23:31, “Sebastian W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > ï¿½ end  
> > > up an aggregated event loop, and then run it with the “loop” method.
> > 
> > –  
> > Posted viahttp://www.ruby-forum.com/.
> 
> Basically, I just want to manage 2 PCs.  
> “sending command to PC1  
> wait until command finished  
> sending command to PC2  
> wait until command finished  
> sending command to PC1  
> wait until command finished”
> 
> I thought open both sessions at the same time was a convenient way to  
> open them only once.

Hm…I’m not an expert, but it seems to me in this case that it’d be  
easier actually just to code it that way.

If your commands are short, you could always inline them with a {} style  
block instead of do/end, if you really want to cut down on your line  
count. 🙂

I guess for establishing the sessions, maybe it’s a little inefficient  
since you’re opening up four sessions instead of two, but sometimes you  
just have to DTSTTCPW. 😛

---

<div class="post-metadata">

**Author:** ![lyrics](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/13edae/32.png) [@lyrics](https://www.ruby-forum.com/u/lyrics)\
**Post date:** [January 14, 2009, 11:51pm UTC](https://www.ruby-forum.com/t/net-ssh-serialized-commands/157710/6 "2009-01-14T23:51:35Z")

</div>

On 14 jan, 23:31, “Sebastian W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end  
> > up an aggregated event loop, and then run it with the “loop” method.
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

Basically, I just want to manage 2 PCs.  
“sending command to PC1  
wait until command finished  
sending command to PC2  
wait until command finished  
sending command to PC1  
wait until command finished”

I thought open both sessions at the same time was a convenient way to  
open them only once.

---

<div class="post-metadata">

**Author:** ![lyrics](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/13edae/32.png) [@lyrics](https://www.ruby-forum.com/u/lyrics)\
**Post date:** [January 15, 2009, 10:45am UTC](https://www.ruby-forum.com/t/net-ssh-serialized-commands/157710/7 "2009-01-15T10:45:44Z")

</div>

On 15 jan, 00:30, “Sebastian W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > "sending command to PC1  
> > easier actually just to code it that way.
> 
> If your commands are short, you could always inline them with a {} style  
> block instead of do/end, if you really want to cut down on your line  
> count. 🙂
> 
> ## I guess for establishing the sessions, maybe it’s a little inefficient since you’re opening up four sessions instead of two, but sometimes you just have to DTSTTCPW. 😛
> 
> Posted viahttp://www.ruby-forum.com/.

I may try this, but I want to avoid since this code is executed more  
than 200 times.

---

<div class="post-metadata">

**Author:** ![lyrics](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/13edae/32.png) [@lyrics](https://www.ruby-forum.com/u/lyrics)\
**Post date:** [January 15, 2009, 1:52am UTC](https://www.ruby-forum.com/t/net-ssh-serialized-commands/157710/8 "2009-01-15T01:52:44Z")

</div>

Sebastian W. wrote:

> lyrics wrote:  
> I would enclose all the commands in a specific script on the target  
> client.  
> And then just execute this top level script from your ssh session…your  
> native script will take care of the rest.

(obviously it does not work if your a building your program logic on the  
fly …but you can also create your script on your box, scp to the target  
box and then execute it)

…just another view

-r.

---

<div class="post-metadata">

**Author:** ![lyrics](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/13edae/32.png) [@lyrics](https://www.ruby-forum.com/u/lyrics)\
**Post date:** [January 15, 2009, 11:10am UTC](https://www.ruby-forum.com/t/net-ssh-serialized-commands/157710/9 "2009-01-15T11:10:28Z")

</div>

On 15 jan, 10:41, lyrics [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > –  
> > > > I thought open both sessions at the same time was a convenient way to  
> > > > since you’re opening up four sessions instead of two, but sometimes you  
> > > > just have to DTSTTCPW. 😛  
> > > > –  
> > > > Posted viahttp://www.ruby-forum.com/.
> 
> I may try this, but I want to avoid since this code is executed more  
> than 200 times.

I did more test.  
I found out that I was working on the wrong issue:  
the “exec!” command do it job well.  
I think my issue comes from restarting a service.  
It likes it takes times before really “up” whereas the command returns.
