Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Possible to respond to output from system(linuxcommand)

Reply
Thread Tools

Possible to respond to output from system(linuxcommand)

 
 
Richard Sandoval
Guest
Posts: n/a
 
      12-29-2010
Hello,

I have a ruby script that executes a linux command however the command
returns a question:

This will update 1 entry, continue? [y/N]:

I can see this output in when in debug mode for the script. How can I
pass a y and return/enter automatically for each time this question
comes up? I tried to add system(echo y) and puts "\r" to send a y and do
a return but no success. Any pointers?



Here is an example of my small script:

require 'yaml'
require 'nventory'
nvclient = NVentory::Client.new(:dryrun => true, :debug => true, :server
=> 'http://nventory.local')

mdb = YAML::load_file('mdb_dump.yml')
mdb.each_key do |server|
setdata = { :name => "#{server}", :hardware_profile_id => 4, :status_id
=> "#{mdb[server]['status']}"}
nvclient.set_objects('nodes',{},setdata,'jsmith')
end
mdb = YAML::load_file('mdb_dump.yml')
mdb.each_key do |server|
system("opsdb --dry-run --server nventory.local nv --exactget
name=dhcpserver.local --addtonodegroup=temp1,temp2")

system(echo y)
puts "\r"

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Adam Ms.
Guest
Posts: n/a
 
      12-29-2010
Richard Sandoval wrote in post #971231:
> Hello,
>
> snip
>
> system("opsdb --dry-run --server nventory.local nv --exactget
> name=dhcpserver.local --addtonodegroup=temp1,temp2")
>
> system(echo y)
> puts "\r"


This might help:

http://tech.natemurray.com/2007/03/r...-commands.html

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Jeremy Bopp
Guest
Posts: n/a
 
      12-29-2010
On 12/29/2010 12:36 AM, Richard Sandoval wrote:
> Hello,
>
> I have a ruby script that executes a linux command however the command
> returns a question:
>
> This will update 1 entry, continue? [y/N]:
>
> I can see this output in when in debug mode for the script. How can I
> pass a y and return/enter automatically for each time this question
> comes up? I tried to add system(echo y) and puts "\r" to send a y and do
> a return but no success. Any pointers?
>
>
>
> Here is an example of my small script:
>
> require 'yaml'
> require 'nventory'
> nvclient = NVentory::Client.new(:dryrun => true, :debug => true, :server
> => 'http://nventory.local')
>
> mdb = YAML::load_file('mdb_dump.yml')
> mdb.each_key do |server|
> setdata = { :name => "#{server}", :hardware_profile_id => 4, :status_id
> => "#{mdb[server]['status']}"}
> nvclient.set_objects('nodes',{},setdata,'jsmith')
> end
> mdb = YAML::load_file('mdb_dump.yml')
> mdb.each_key do |server|
> system("opsdb --dry-run --server nventory.local nv --exactget
> name=dhcpserver.local --addtonodegroup=temp1,temp2")
>
> system(echo y)
> puts "\r"


The system method runs the child program non-interactively from the
perspective of your script. You'll want to look at IO.popen. That will
run the child and return its stdin and stdout as a duplexed IO object
for you. You can then use something like the expect library (part of
the Ruby standard library) to watch the program's output for the prompt
at which point you can send whatever you like using puts or print on the
IO object popen gave you.

require 'expect'

child_io = IO.popen(<<-BASH, 'r+')
bash -c "
printf 'the prompt> '
read
echo The user typed \\\\\\"\\"\\$REPLY\\"\\\\\\"
"
BASH
child_io.expect('the prompt> ') { child_io.puts 'this is my response' }
puts child_io.read
#-> The user typed "this is my response"

-Jeremy

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      12-30-2010
Adam Ms. wrote in post #971304:
> Richard Sandoval wrote in post #971231:
>> Hello,
>>
>> snip
>>
>> system("opsdb --dry-run --server nventory.local nv --exactget
>> name=dhcpserver.local --addtonodegroup=temp1,temp2")
>>
>> system(echo y)
>> puts "\r"

>
> This might help:
>
> http://tech.natemurray.com/2007/03/r...-commands.html
>
> Also, something like this might work:
>
> %Q{sh -c 'echo "Y" | yourcomamnd'}


Or perhaps:

system("yes | opsdb ... ")

See "man yes" for info.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Router does not respond in console Thread7@hotmail.com Cisco 3 05-18-2005 09:02 PM
Firefox won't respond duane Chism Firefox 4 02-25-2005 04:12 AM
Cisco VPN - Connection Manager failed to respond Swami Cisco 1 07-07-2004 03:41 AM
Altera unable to respond -- SDF and testbench ALuPin VHDL 3 06-18-2004 05:58 PM
A respond to "MCSE NT4, W2K.." "Network Guru" & "Techie" Samantha MCSE 46 06-09-2004 07:49 AM



Advertisments