如何通过perl telnet设置远程服务器外壳

如何通过perl telnet设置远程服务器外壳,perl,bash,telnet,env,Perl,Bash,Telnet,Env,如何通过perl telnet将远程服务器shell设置为bash 我的代码如下: $telnet = Net::Telnet->new(Timeout=>90,Errmode=>'die'); $telnet->open($ipAddress); $telnet->login($username,$password); $telnet->waitfor('/$/'); $telnet->print("exec bash"); print "after

如何通过perl telnet将远程服务器shell设置为bash

我的代码如下:

$telnet = Net::Telnet->new(Timeout=>90,Errmode=>'die');
$telnet->open($ipAddress);
$telnet->login($username,$password);
$telnet->waitfor('/$/');
$telnet->print("exec bash");
print "after bash";
print $telnet->cmd("ls -lrt");
print $telnet->cmd("cd $homePath");
在上面的代码中,在execbash语句之后,没有任何命令被执行。我需要将远程shell设置为bash,因为在这一行之后需要运行的某些进程需要env设置


请告诉我如何执行相同操作。

您等待命令提示的正则表达式错误

$telnet->waitfor('/$/');
试一试

更好的是,请参见Net::Telnet 3.04文档中的第一个示例:

my $host = 'your_destination_host_here';
my $user = 'your_username_here';
my $passwd = 'your_password_here';
my ($t, @output);

## Create a Net::Telnet object.
use Net::Telnet ();
$t = new Net::Telnet (Timeout  => 10);

## Connect and login.
$t->open($host);

$t->waitfor('/login: ?$/i');
$t->print($user);

$t->waitfor('/password: ?$/i');
$t->print($passwd);

## Switch to a known shell, using a known prompt.
$t->prompt('/<xPROMPTx> $/');
$t->errmode("return");

$t->cmd("exec /usr/bin/env 'PS1=<xPROMPTx> ' /bin/sh -i")
    or die "login failed to remote host $host";

$t->errmode("die");

## Now you can do cmd() to your heart's content.
@output = $t->cmd("uname -a");
print @output;
my$host='your_destination_host_here';
my$user='your_username_here';
my$passwd='your_password_here';
我的($t,@输出);
##创建一个Net::Telnet对象。
使用Net::Telnet();
$t=新网络::Telnet(超时=>10);
##连接并登录。
$t->open($host);
$t->waitfor('/login:?$/i');
$t->print($user);
$t->waitfor('/密码:?$/i');
$t->print($passwd);
##使用已知提示切换到已知shell。
$t->prompt('/$/');
$t->errmode(“返回”);
$t->cmd(“exec/usr/bin/env'PS1=”/bin/sh-i)
或“登录到远程主机$host失败”;
$t->errmode(“模具”);
##现在您可以随心所欲地执行cmd()。
@输出=$t->cmd(“uname-a”);
打印@输出;

可以试试
/bin/bash
?或者
exec$SHELL
my $host = 'your_destination_host_here';
my $user = 'your_username_here';
my $passwd = 'your_password_here';
my ($t, @output);

## Create a Net::Telnet object.
use Net::Telnet ();
$t = new Net::Telnet (Timeout  => 10);

## Connect and login.
$t->open($host);

$t->waitfor('/login: ?$/i');
$t->print($user);

$t->waitfor('/password: ?$/i');
$t->print($passwd);

## Switch to a known shell, using a known prompt.
$t->prompt('/<xPROMPTx> $/');
$t->errmode("return");

$t->cmd("exec /usr/bin/env 'PS1=<xPROMPTx> ' /bin/sh -i")
    or die "login failed to remote host $host";

$t->errmode("die");

## Now you can do cmd() to your heart's content.
@output = $t->cmd("uname -a");
print @output;