How to redirect $stdin to read from a string?

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

On Jun 8, 2008, at 12:13 PM, A. Lester Buck III wrote:

=========
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.

On Sunday 08 June 2008 14:13:54 A. Lester Buck III wrote:

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…

Thanks for the pointers! I’m trying to avoid writing a temporary file
and then feeding that back to the command.

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

This works great. My script is a caller id popup/logger on Linux. I
thought I would add notifiers to the other systems on my KVM switch so I
don’t have to switch back and forth to see if I need to pick up. This
code runs inside a forked process which is then thrown away, so
redirecting all of $stdin wouldn’t have been a problem, but this avoids
that issue, too.

I really appreciate your help!

Lester