Thursday, February 08, 2007

Perl-Expect

It's been a long time since I actually wrote a program to solve a problem. Ditch the previous post, 'cuz I found a much better SSH daemon for Win called FreeSSHD which lets you run it with "ssh host command" just like I wanted. But even with a help page I found on the topic, I was unable to configure it to work without requiring a password, so I decided to finally get down to Expect (something I've claimed to know, tho' I never got it working to simulate a telnet job like I wanted) and found out that this code works!

#!/usr/bin/perl
use Expect;
my $timeout=2;
my $exp=Expect->spawn("ssh xtp2 \"cmd.exe /c dir\"");
$exp->expect($timeout,
[ "password:" => sub { my $self = shift;
$self->send("root123\n")
} ]
);
$exp->soft_close();