論壇徽章: 0
30 可用積分
本帖最后由 lucash 于 2010-08-29 19:08 編輯
在結合使用Net::OpenSSH 和 Expect模塊時,遇到的問題。
用Net::OpenSSH 建立一個master ssh connection ,然后用一個叫做open2pty的方法來
打開一個pty供Expect模塊使用。
在spawn時出錯,提示信息如下:
warning: TIOCSCTTY failed, slave might not be set as controlling terminal: Operation not permitted at /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/IO/Pty.pm line 120.
Error: could not connect pty as controlling terminal!
但不知道怎么才能正確的使用IO::Pty中的make_slave_controlling_terminal() 方法來spawn一個子進程。
該方法的說明文檔中提示說只能通過fork一個子進程來調(diào)用它。不知道該如何使用。
我的出錯代碼如下:
#!/usr/bin/perl -w
use strict;
use Net::OpenSSH;
use Expect;
my $ssh = Net::OpenSSH->new('192.168.10.100',
user => 'lucash',
passwd => 'lucash'
);
$ssh->error and die "Unable to connect: " . $ssh->error . "\n";
my @cmd = qw(who);
my ($pty,$pid) = $ssh->open2pty({},@cmd);
my $exp = Expect->init($pty) or die "Can not init pty: $!\n";
$exp->spawn(@cmd) or die "Unable to spawn command: $!\n";
復制代碼 加了make_slave_controlling_terminal后還是報錯:
my ($pty, $pid) = $ssh->open2pty({},@cmd};
if ( (my $ppid = fork()) == 0) {
$pty->slave();
$pty->make_slave_controlling_terminal();
my $exp = Expect->init($pty) or die "Can not init pty : $!\n";
}
復制代碼 難道是要open2pty得到的那個$pid fork的子進程中make_slave_controlling_terminal()才有用嗎?該怎么做呢?
以下是IO::Pty的部分源碼: [font=Verdana]
sub make_slave_controlling_termianl {
…………………… 部分省略。
# Acquire a controlling terminal if this doesn't happen automatically
if (defined TIOCSCTTY) {
if (not defined ioctl( ${*$self}{'io_pty_slave'}, TIOCSCTTY, 0 )) {
warn "warning: TIOCSCTTY failed, slave might not be set as controlling terminal: $!" if $^W;
}
} elsif (defined TCSETCTTY) {
if (not defined ioctl( ${*$self}{'io_pty_slave'}, TCSETCTTY, 0 )) {
warn "warning: TCSETCTTY failed, slave might not be set as controlling terminal: $!" if $^W;
}
}
if (not open(\*DEVTTY, "/dev/tty")) {
warn "Error: could not connect pty as controlling terminal!\n";
return undef;
} else {
close \*DEVTTY;
}
return 1
}
復制代碼 IO:: Pty的文檔還說: See the try script (also test.pl) for an example how to correctly spawn a subprocess. 復制代碼 天哪,這個‘try’ 腳本在哪啊?
搞兩天了也沒搞定,各位大俠幫幫忙。多謝了。
注:這個部分只是一個腳本中的一小部分,因此暫不考慮拋棄Net::OpenSSH模塊的方法
我來回答