加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl的expect模块实现自动输入密码登录

发布时间:2020-12-15 21:00:25 所属栏目:大数据 来源:网络整理
导读:?1.安装Expect模块 cpaninstall Expect ? ? 2.脚本如下: #!/usr/bin/perl -w use strict; use Expect; #passwd.txt格式如下: #host port user????????pass if (@ARGV != 2) { ????????????????????????????????????????????????????????????????print "Usa
?1.安装Expect模块
cpan>install Expect
?
?
2.脚本如下:
#!/usr/bin/perl -w
use strict;
use Expect;

#passwd.txt格式如下:
#host port user????????pass

if(@ARGV != 2) {
????????????????????????????????????????????????????????????????print "Usage:n";
????????????????????????????????????????????????????????????????print "perl ssh_expect.pl????????passwd.txt????????hostn";
????????????????????????????????????????????????????????????????exit 1;
}
my $passwd = $ARGV[0];
my $ip = $ARGV[1];
open(FILE,$passwd)||die "can not open the file: $passwd";
????????

while (defined (my $line =<FILE>)) {
???????????????????????????????? chomp $line;
???????? if ($line =~ /$ip/) {
????????my @array = split(/s+/,$line);
????????&ssh_expect($array[0],$array[1],$array[2],$array[3]);
????????????????????????????????????????}
????}


sub ssh_expect() {
????my $host=$_[0];
????my $port=$_[1];
????my $user=$_[2];
????my $pass=$_[3];
????my $exp = new Expect;
????$exp=Expect->spawn( "ssh -p$port $user@$host" );
????$exp->expect(10,[ qr/(yes/no)?s*$/ => sub { $exp->send( "yesn"); exp_continue; } ],);
????$exp->expect(10,"assword:" );
????$exp->send( "$passn" );
????$exp->interact();
}

close FILE;

使用expect做自动login,一直有个麻烦的问题.就是当本地的终端窗口大小发生改变时,因为使用了expect,没法传送改变窗口的信号到远程机 器上面,所以看起来很奇怪,严重影响工作的效率. 以前在perl上解决过,perl上对这个问题的详解如下 I set the terminal size as explained above,but if I resize the window,the application does not notice this. You have to catch the signal WINCH ("window size changed"),change the terminal size and propagate the signal to the spawned application:

#!/usr/bin/perl -w use strict; use Expect; #passwd.txt格式如下: #host port user? pass if(@ARGV != 2) { ??????????????? print "Usage:n"; ??????????????? print "perl ssh_expect.pl? passwd.txt? hostn"; ??????????????? exit 1; } my $passwd = $ARGV[0]; my $ip = $ARGV[1]; open(FILE,$passwd)||die"can not open the file: $passwd"; ? while (defined (my $line =<FILE>)) { ???????? chomp $line; ??? ?if ($line =~ /$ip/) { ??? ??? my @array = split(/s+/,$line); ??? ??? &ssh_expect($array[0],$array[3]); ???????? ??? } ??? } sub ssh_expect() { ??? my $host=$_[0]; ??? my $port=$_[1]; ??? my $user=$_[2]; ??? my $pass=$_[3]; ??? my $exp = new Expect; ??? $exp=Expect->spawn( "ssh -p$port $user@$host" ); ??? $exp->slave->clone_winsize_from(*STDIN); ??? $SIG{WINCH} = &;winch; ??? $exp->expect(10,[ qr/(yes/no)?s*$/ => sub { $exp->send("yesn"); exp_continue; } ],); ??? $exp->expect(10,"assword:" ); ??? $exp->send( "$passn" ); ??? $exp->interact(); } sub winch { ? ??? my $exp->slave->clone_winsize_from(*STDIN); ? ??? kill WINCH => $exp->pid if $exp->pid; ???? $SIG{WINCH} = &;winch; } close FILE;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读