How to redirect $stdin to read from a string?

  • Thread starter A. Lester Buck III
  • Start date
A

A. Lester Buck III

Hi,

I want to send a Windows popup from a Linux machine. smbclient is
the standard utility, and it will accept a file on standard
input to avoid terminal interaction, viz.

smbclient -M netbiosname < /tmp/message.text

From my reading of the Ruby Cookbook, recipes 6.15 and 6.16, this
"should" work:

=========
#!/usr/bin/ruby -w

require 'stringio'

$stdin = StringIO.new "This is the test message"

system("smbclient -M netbiosname")
=========

Alas, it ignores the input message and prompts for terminal input
terminated by ^D.


Could someone please show me the correct code to use in a Ruby
script to send a string to a Windows popup?


Thanks a lot!

Lester
 
E

Eric Hodel

I want to send a Windows popup from a Linux machine. smbclient is
the standard utility, and it will accept a file on standard
input to avoid terminal interaction, viz.

smbclient -M netbiosname < /tmp/message.text

From my reading of the Ruby Cookbook, recipes 6.15 and 6.16, this
"should" work:

=========
#!/usr/bin/ruby -w

require 'stringio'

$stdin = StringIO.new "This is the test message"

system("smbclient -M netbiosname")
=========

Alas, it ignores the input message and prompts for terminal input
terminated by ^D.


Could someone please show me the correct code to use in a Ruby
script to send a string to a Windows popup?

You'll have to use #reopen with a real file instead. Assigning to
$stdin only affects ruby's interpretation of standard input, not the C
standard input.
 
D

David Masover

require 'stringio'

$stdin = StringIO.new "This is the test message"

system("smbclient -M netbiosname")

That only affects the stdin variable -- and if it did do what you're thinking,
that's probably not a good idea anyway. You probably don't want to set stdout
for the entire rest of your program.

What you probably want is something like popen:

def send_message message, netbiosname
IO.popen "smbclient -M #{netbiosname}", 'w' do |io|
message.each_line do |line|
io.write line
end
end
raise 'smbclient failed' unless $?.success?
end

I'm using each_line because it exists on both String and IO -- so you don't
have to wrap the message in a StringIO. It won't work very well for large
chunks of binary, but I don't think you'll be sending those...
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top