Find the answer to your Linux question:
Results 1 to 3 of 3
Hello , I am trying to write a perl script which will give an interactive session to a user to execute command on the server. I have written a small ...
  1. #1
    Just Joined! pratapsingh's Avatar
    Join Date
    Feb 2009
    Posts
    82

    Exclamation [SOLVED] Perl and Net:SSH

    Hello ,

    I am trying to write a perl script which will give an interactive session to a user to execute command on the server.

    I have written a small script to do this :

    Code:
    !/usr/bin/perl -w
    use strict;
    use Net::SSH::Perl; 
    my $host = '192.168.1.1';
    my $username = 'user';
    my $login_passwd = 'test123';
    
    
    my $ssh = Net::SSH::Perl->new($host, $username,trace => -1);
    
    $ssh->config->set('interactive', 1)
        unless defined $ssh->config->get('interactive');
    my $cmd;
    $ssh->login($username, $login_passwd);
    
    if ($cmd) {
        $cmd =$_;
        my($out, $err, $exit) = $ssh->channel($cmd);
        print $out if $out;
        print $err if $err;
    
    }
    else {
        eval "use Term::ReadKey;";
        ReadMode('raw');
        eval "END { ReadMode('restore') };";
        $ssh->shell;
        print "Connection to $host closed.\n";
    }
    It works fine but the problem is there when we execute "w" or "top" command on the server. It does not displays all the field for the "top" command and for "w" I am getting the following output

    Code:
    [root@server ~]# w
    47 column window is too narrow
    Any idea about this.

    Thank you
    Pratap

  2. #2
    Just Joined! pratapsingh's Avatar
    Join Date
    Feb 2009
    Posts
    82
    Any idea .. ?

  3. #3
    Just Joined! pratapsingh's Avatar
    Join Date
    Feb 2009
    Posts
    82
    I found the solution Here is the script which does not alter the output:
    You need to install Perl Module Net::OpenSSH and IO::Pty

    #!/usr/bin/perl

    use strict;
    use warnings;
    use Net::OpenSSH;

    my $host = '192.168.3.37';
    my $user = 'test';
    my $login_passwd = 'test123';

    my $ssh = Net::OpenSSH->new($host, user => $user, password => $login_passwd);
    $ssh->error and die "Unable to connect to remote host: " . $ssh->error;

    $ssh->system({tty => 1});

    $ssh->error and die "error: " . $ssh->error;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...