Store shell session/cmd.exe session in some object?

Z

Zach Dennis

I'd like to do something like:

mysession = SystemShell.new //Would I open a pipe? Would it be a IO.new?
dir_list = mysession.puts( "ls -al" ) //on Linux
dir_list = mysession.puts( "dir" ) //on Windows

and then further more do this:

mysession.puts( "ssh -l myname 127.0.0.1" )
mysession.puts( "cd /opt/webstar/etc/etc/" )
list = mysession.puts( "ls -al" )

And so on. Does this make sense. Basically I am looking to be able to keep a
session open with the system and receive it's output. This would prove
beneficial in the case that I ssh into several server and gather data from
each server in my script. Any ideas?

Thanks,


Zach
 
R

Robert Klemme

Zach Dennis said:
I'd like to do something like:

mysession = SystemShell.new //Would I open a pipe? Would it be a IO.new?
dir_list = mysession.puts( "ls -al" ) //on Linux
dir_list = mysession.puts( "dir" ) //on Windows

How about using Dir.entries( "." ) - this is more portable and likely
faster, too.
and then further more do this:

mysession.puts( "ssh -l myname 127.0.0.1" )
mysession.puts( "cd /opt/webstar/etc/etc/" )
list = mysession.puts( "ls -al" )

And so on. Does this make sense. Basically I am looking to be able to keep a
session open with the system and receive it's output.

You mean "with the command processor". For simple usage you can resort to
IO.popen() (some caveats apply for windows). An example:

IO.popen( "cmd.exe", "r+" ) do |cmd|
Thread.new(cmd) do |io|
while line = io.gets
puts line
end
end

cmd.puts "dir"
cmd.puts "cd \\"
cmd.puts "dir"

sleep 3
end


Else there is expect for Ruby.
http://raa.ruby-lang.org/list.rhtml?name=ruby-expect

Regards

robert
 
J

Joel VanderWerf

Zach said:
I'd like to do something like:

mysession = SystemShell.new //Would I open a pipe? Would it be a IO.new?
dir_list = mysession.puts( "ls -al" ) //on Linux
dir_list = mysession.puts( "dir" ) //on Windows

and then further more do this:

mysession.puts( "ssh -l myname 127.0.0.1" )
mysession.puts( "cd /opt/webstar/etc/etc/" )
list = mysession.puts( "ls -al" )

And so on. Does this make sense. Basically I am looking to be able to keep a
session open with the system and receive it's output. This would prove
beneficial in the case that I ssh into several server and gather data from
each server in my script. Any ideas?

I've never used it, but does shell.rb in the standard lib do what you want?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top