How can I open a remote ssh session with perl

How can I open a remote ssh session with perl

am 24.08.2010 10:55:01 von S Pratap Singh

--0016364d1bd7a4f76c048e8deba3
Content-Type: text/plain; charset=ISO-8859-1

Hello ,

I want to open a remote ssh session using the perl script , I am able to
login to the server using the simple script and i am also able to execute
few commands which is defined in the script on the server.

I am writing a script which takes the user name , password and hostname from
the database and login to the server .

I am only able to login to the server and my script logs out and only I can
run few command if I provide those command in my script else I am not able
to run those command after logging in.

I need to run those command once I have logged in using my script and I want
to track down all the commands being executed in a particular session.

I know how to run command on remote shell using script , but I want to open
remote shell using perl and want to run the command manually and terminate
the session when I am done as we normally do with shell .

Any help or guidance will be highly appreciated.


--
Regards,
Pratap Singh

--0016364d1bd7a4f76c048e8deba3--

Re: How can I open a remote ssh session with perl

am 24.08.2010 16:08:15 von Peter Scott

On Tue, 24 Aug 2010 14:25:01 +0530, S Pratap Singh wrote:

> I know how to run command on remote shell using script , but I want to
> open remote shell using perl and want to run the command manually and
> terminate the session when I am done as we normally do with shell .

CPAN: Net::SSH::Perl .

--
Peter Scott
http://www.perlmedic.com/ http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=0137001274
http://www.oreillyschool.com/courses/perl1/

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: How can I open a remote ssh session with perl

am 24.08.2010 21:28:36 von Brian Fraser

--001485f4541481ad7f048e96c5f6
Content-Type: text/plain; charset=ISO-8859-1

On Tue, Aug 24, 2010 at 11:08 AM, Peter Scott wrote:


> CPAN: Net::SSH::Perl .

I had a similar issue not too long ago; Spent a couple of days attempting to
get Net::SSH::Perl to compile properly, gave up, went to CPAN, found
[Net::SSH::Expect][0]; So far so good, so here's a recommendation.

[0]
http://search.cpan.org/~bnegrao/Net-SSH-Expect-1.09/lib/Net/ SSH/Expect.pod

--001485f4541481ad7f048e96c5f6--

Re: How can I open a remote ssh session with perl

am 25.08.2010 08:35:11 von Agnello George

--000e0cd2e1f85fb1f9048ea015b5
Content-Type: text/plain; charset=ISO-8859-1

On Wed, Aug 25, 2010 at 12:58 AM, Brian Fraser wrote:

> On Tue, Aug 24, 2010 at 11:08 AM, Peter Scott wrote:
>
>
> > CPAN: Net::SSH::Perl .
>
> I had a similar issue not too long ago; Spent a couple of days attempting
> to
> get Net::SSH::Perl to compile properly, gave up, went to CPAN, found
> [Net::SSH::Expect][0]; So far so good, so here's a recommendation.
>
> [0]
> http://search.cpan.org/~bnegrao/Net-SSH-Expect-1.09/lib/Net/ SSH/Expect.pod
> <
> http://search.cpan.org/%7Ebnegrao/Net-SSH-Expect-1.09/lib/Ne t/SSH/Expect.pod
> >
>
i had written a small shell script to make this happen.
cat /root/.logindetails.txt
1:serverinitials:ipaddress:web:userpaswd:rootpassword:0
#!/bin/bash

cat /root/.logindetails.txt |awk -F: {'print $1'} > /tmp/ssh.txt

sleep 2
> ~/.ssh/known_hosts
#data variables

user=$(cat /root/.logindetails.txt |grep -w $1 |awk -F: {'print $4'})
server=$(cat /root/.logindetails.txt |grep -w $1 |awk -F: {'print $3'})
useracc=$(cat /root/.logindetails.txt |grep -w $1 |awk -F: {'print $5'})
suacc=$(cat /root/.logindetails.txt |grep -w $1 |awk -F: {'print $6'})
echo " $user $useracc $suacc"



echo "Connecting to host"
echo " supassword is $suacc"
expect -c "set timeout -1;\
spawn ssh $server -l $user ;\
match_max 100000;\
expect *(yes/no)*;\
send -- yes\r;\
expect *password:*;\
send -- $useracc\r;\
interact;"





--
Regards
Agnello D'souza

--000e0cd2e1f85fb1f9048ea015b5--

Re: How can I open a remote ssh session with perl

am 25.08.2010 19:51:49 von derykus

On Aug 24, 12:28=A0pm, frase...@gmail.com (Brian Fraser) wrote:
> On Tue, Aug 24, 2010 at 11:08 AM, Peter Scott wrote:
> > CPAN: Net::SSH::Perl .
>
> I had a similar issue not too long ago; Spent a couple of days attempting=
to
> get Net::SSH::Perl to compile properly, gave up, went to CPAN, found
> [Net::SSH::Expect][0]; So far so good, so here's a recommendation.
>
> [0]http://search.cpan.org/~bnegrao/Net-SSH-Expect-1.09/lib/N et/SSH/Expec.=
... >


Another option is Net::SSH2 which, although it supports only SSH2,
is much easier to build than Net::SSH::Perl.

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: How can I open a remote ssh session with perl

am 26.08.2010 07:52:29 von S Pratap Singh

--00163683395a896e60048eb39a23
Content-Type: text/plain; charset=ISO-8859-1

Here is my code which logs in and performs some task but it does not show
the output of top and pstree command what would i do to get the output of
these commands along with vmstat and iostat

#!/usr/bin/perl -w
use Net::SSH::Perl
$host = '192.168.3.36';
$username = 'root';
$login_passwd = 'password';
#$session -> close;

#my $cmd = "ls -l";
my $ssh = Net::SSH::Perl->new($host);
$ssh->login($username, $login_passwd);
#my $arg= join '',@ARGV;
#while ($arg) {
while(<>) {
$cmd = $_;
my($stdout,$sterr, $stderr) = $ssh->cmd("$cmd");
print "\n$stdout\n$stderr ";
print "quiting from the server " if eof;
}

Thanks for all the responses ..

Regards,
Pratap

--00163683395a896e60048eb39a23--

Re: How can I open a remote ssh session with perl

am 26.08.2010 10:16:55 von Shlomi Fish

Hi Pratap,

a few comments on your code. I'm not sure they will help with anything, but
they are still appropriate.

On Thursday 26 August 2010 08:52:29 S Pratap Singh wrote:
> Here is my code which logs in and performs some task but it does not show
> the output of top and pstree command what would i do to get the output of
> these commands along with vmstat and iostat
>
> #!/usr/bin/perl -w

Add "use strict;" and "use warnings;" (though some people believe that "-w" is
better than "use warnings;" - but it's no substitute to "use strict;").

> use Net::SSH::Perl
> $host = '192.168.3.36';

my $host = '192.168.3.36'; ("use strict;" will catch that).

> $username = 'root';
> $login_passwd = 'password';
> #$session -> close;

Why do you have commented-out code here? Please clean it up.

>
> #my $cmd = "ls -l";
> my $ssh = Net::SSH::Perl->new($host);
> $ssh->login($username, $login_passwd);
> #my $arg= join '',@ARGV;
> #while ($arg) {

You need to split your code into paragraphs with empty lines in between.

> while(<>) {
> $cmd = $_;

This is better written as:

[code]
while (my $cmd = <>)
[/code]

And you probably want chomp.

> my($stdout,$sterr, $stderr) = $ssh->cmd("$cmd");

What is $sterr? And you seem to have both that and $stderr.

> print "\n$stdout\n$stderr ";
> print "quiting from the server " if eof;

You've misspelled "quitting" and better put a filehandle in eof().

Regards,

Shlomi Fish

> }
>
> Thanks for all the responses ..
>
> Regards,
> Pratap

--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
List of Portability Libraries - http://shlom.in/port-libs

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: How can I open a remote ssh session with perl

am 26.08.2010 11:21:43 von S Pratap Singh

--001636418291c81cfe048eb6866f
Content-Type: text/plain; charset=ISO-8859-1

> Hello ,
> Thanks Shlomi for valuable giving input on my code I will implement that
> too.
>
> But still my question remains same is it possible to open a remote session
> with perl and get the output of "top", "vmsate 1 10", "iostat 1" , "yum
> install etc", etc ..
>
>
> I am not getting the output of those command using my script,other (command
> such as "w, ls,ls-al,") works fine, is there any way we can get the output
> of these ("top", "vmstat 1 10", "iostat 1" , "yum install etc") commands.
>
> Thanks
> Pratap
>

--001636418291c81cfe048eb6866f--

Re: How can I open a remote ssh session with perl

am 27.08.2010 04:43:33 von Peter Scott

On Thu, 26 Aug 2010 11:22:29 +0530, S Pratap Singh wrote:
> Here is my code which logs in and performs some task but it does not
> show the output of top and pstree command what would i do to get the
> output of these commands along with vmstat and iostat
>
> #!/usr/bin/perl -w
> use Net::SSH::Perl
> $host = '192.168.3.36';
> $username = 'root';
> $login_passwd = 'password';
> #$session -> close;
>
> #my $cmd = "ls -l";
> my $ssh = Net::SSH::Perl->new($host); $ssh->login($username,
> $login_passwd); #my $arg= join '',@ARGV;
> #while ($arg) {
> while(<>) {
> $cmd = $_;
> my($stdout,$sterr, $stderr) = $ssh->cmd("$cmd"); print
> "\n$stdout\n$stderr ";
> print "quiting from the server " if eof; }

I just tried this code with a server and credentials of my own and it
worked fine with several commands including pstree. So you have some
problem specific to your configuration that needs debugging. Either the
module isn't built right, you have the wrong credentials, you aren't
connecting, or the remote end doesn't have the right environment,
something like that.

--
Peter Scott
http://www.perlmedic.com/ http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=0137001274
http://www.oreillyschool.com/courses/perl1/

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: How can I open a remote ssh session with perl

am 27.08.2010 05:47:22 von S Pratap Singh

--0022159756eae6b5af048ec5f8b4
Content-Type: text/plain; charset=ISO-8859-1

Hello Peter ,

Did you try executing command "top -cd2", "iostat 1", vmstat 1 10" etc ? Did
you get the output of those command ? I am not getting the output of these
commands rest works fine for me . Pstree, I am getting partial output
Here is the output at my end of pstree command
============================
/>perl login.pl
pstree
?-+-dbus-daemon
|-dbus-launch
|-dcopserver
|-gconfd-2
|-kaccess
|-kded
|-kdeinit-+-artsd
| |-kio_file
| |-kio_pop3
| |-klauncher
| |-konsole-+-bash---ssh
| | |-bash---perl
| | |-2*[bash---e---ssh]
| | `-bash
| |-kwin
| `-pidgin
|-kdesktop---run-mozilla.sh---firefox---9*[{firefox}]
|-kicker
|-kmail---4*[{kmail}]
|-kmix
|-knotify
`-ksmserver
=============================

Thank you for your continued help.

Regards,
Pratap

--0022159756eae6b5af048ec5f8b4--

Re: How can I open a remote ssh session with perl

am 27.08.2010 14:51:19 von Peter Scott

On Fri, 27 Aug 2010 09:17:22 +0530, S Pratap Singh wrote:

> Hello Peter ,
>
> Did you try executing command "top -cd2", "iostat 1", vmstat 1 10" etc ?
> Did you get the output of those command ?

vmstat 1 10 works for me; pstree works; but of course you're not going to
run top -cd2 this way, that's an interactive program that won't terminate
without user input. Neither will iostat 1 terminate; but iostat 1 10
works fine, just got to wait 10 seconds.

I don't see anything in your pstree output that looks truncated.

--
Peter Scott
http://www.perlmedic.com/ http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=0137001274
http://www.oreillyschool.com/courses/perl1/

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: How can I open a remote ssh session with perl

am 28.08.2010 08:25:01 von S Pratap Singh

--0016e6d99b5f8f933b048edc4ad3
Content-Type: text/plain; charset=ISO-8859-1

Hello ,

Is there any way I can get these interactive output via my script or I am
missing something? .. Thanks for your continued help.

I need to get those interactive outputs to my script or is there any way to
open a psuedo terminal. I do not want to execute command on server it
should be examined via my script and then it should get executed . Let me
know if this is possible using perl or I have to use any other scripting
language like python.

Thank you
Pratap

--0016e6d99b5f8f933b048edc4ad3--

Re: How can I open a remote ssh session with perl

am 28.08.2010 15:29:23 von Peter Scott

On Sat, 28 Aug 2010 11:55:01 +0530, S Pratap Singh wrote:
> Is there any way I can get these interactive output via my script or I
> am missing something? .. Thanks for your continued help.
>
> I need to get those interactive outputs to my script or is there any way
> to open a psuedo terminal. I do not want to execute command on server
> it should be examined via my script and then it should get executed .
> Let me know if this is possible using perl or I have to use any other
> scripting language like python.

Yes, the documentation for Net::SSH::Perl explains how.

--
Peter Scott
http://www.perlmedic.com/ http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=0137001274
http://www.oreillyschool.com/courses/perl1/

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: How can I open a remote ssh session with perl

am 01.09.2010 08:39:05 von Salvador Fandino

On 08/25/2010 07:51 PM, C.DeRykus wrote:
> On Aug 24, 12:28 pm, frase...@gmail.com (Brian Fraser) wrote:
>> On Tue, Aug 24, 2010 at 11:08 AM, Peter Scott wrote:
>>> CPAN: Net::SSH::Perl .
>>
>> I had a similar issue not too long ago; Spent a couple of days attempting to
>> get Net::SSH::Perl to compile properly, gave up, went to CPAN, found
>> [Net::SSH::Expect][0]; So far so good, so here's a recommendation.
>>
>> [0]http://search.cpan.org/~bnegrao/Net-SSH-Expect-1.09/lib/N et/SSH/Expec...
>
>
> Another option is Net::SSH2 which, although it supports only SSH2,
> is much easier to build than Net::SSH::Perl.

Or Net::OpenSSH that's even easier to install, though it doesn't work on
Windows.

- Salva

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: How can I open a remote ssh session with perl

am 01.09.2010 08:41:49 von Salvador Fandino

On 08/27/2010 05:47 AM, S Pratap Singh wrote:
> Hello Peter ,
>
> Did you try executing command "top -cd2", "iostat 1", vmstat 1 10" etc ? Did
> you get the output of those command ? I am not getting the output of these
> commands rest works fine for me.


use Net::OpenSSH;
my $ssh = Net::OpenSSH->new($host, user => $user, passwd => $passwd);

my $out = $ssh->pipe_out('iostat 1');
while(<$out>) {
print $_;
}

- Salva

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: How can I open a remote ssh session with perl

am 02.09.2010 17:15:28 von S Pratap Singh

--001636832f32d15fa3048f4848e2
Content-Type: text/plain; charset=ISO-8859-1

This code(given below) is working fine for all the command but for some
command it is not able to print the output.
I am getting the following output
==================
w
52 column window is too narrow
==================
Similarly for top it is not printing all the field .
top -cd2
top - 20:36:28 up 21:30, 18 users, load average: 1.
Tasks: 38 total, 1 running, 37 sleeping, 0 sto
Cpu(s): 8.4%us, 0.4%sy, 0.0%ni, 90.8%id, 0.3%wa,
Mem: 1025056k total, 1003548k used, 21508k fre
Swap: 2031608k total, 103608k used, 1928000k fre

Actual top output on the same system is as below

top -cd2

top - 20:37:05 up 21:30, 17 users, load average: 1.10, 1.07, 1.00
Tasks: 34 total, 1 running, 33 sleeping, 0 stopped, 0 zombie
Cpu(s): 8.5%us, 0.4%sy, 0.0%ni, 90.8%id, 0.3%wa, 0.0%hi, 0.0%si,
0.0%st
Mem: 1025056k total, 994780k used, 30276k free, 2940k buffers
Swap: 2031608k total, 103600k used, 1928008k free, 86892k cached

============================

#!/usr/bin/perl -w
use strict;
use Net::SSH::Perl;
my $host = '192.168.0.114';
my $username = 'pratap';
my $login_passwd = 'pratap123';
my $ssh = Net::SSH::Perl->new($host, $username);
$ssh->config->set('interactive', 1)
unless defined $ssh->config->get('interactive');

$ssh->login($username, $login_passwd);
my $cmd;
if ($cmd) {
my($out, $err, $exit) = $ssh->cmd($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";
}
==================================

Is there any way to get all the fields properly.

Thank you
Pratap

--001636832f32d15fa3048f4848e2--

Re: How can I open a remote ssh session with perl

am 10.09.2010 00:17:07 von S Pratap Singh

--0016e64c2ddc994a2b048fdafd4d
Content-Type: text/plain; charset=ISO-8859-1

I am stuck with this issue does any one have any idea about this ...


This code(given below) is working fine for all the command but for some
command it is not able to print the output.
I am getting the following output
==================
w
52 column window is too narrow

Output of the same command on the same system while accessing it using ssh
konsole
03:37:01 up 1 day, 12:20, 11 users, load average: 0.72, 0.52, 0.40
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
pratap :0 - 23:00 ?xdm? 31:52 0.06s /bin/sh
/usr/bin/startkde
pratap pts/2 :0 23:00 3.00s 0.55s 0.51s perl log.pl
pratap pts/3 :0 23:00 4:26m 1.51s 1.50s ssh -X
pratap@192.168.3.31

==================
Similarly for top it is not printing all the field .
top -cd2
top - 20:36:28 up 21:30, 18 users, load average: 1.
Tasks: 38 total, 1 running, 37 sleeping, 0 sto
Cpu(s): 8.4%us, 0.4%sy, 0.0%ni, 90.8%id, 0.3%wa,
Mem: 1025056k total, 1003548k used, 21508k fre
Swap: 2031608k total, 103608k used, 1928000k fre

Actual top output on the same system is as below

top -cd2

top - 20:37:05 up 21:30, 17 users, load average: 1.10, 1.07, 1.00
Tasks: 34 total, 1 running, 33 sleeping, 0 stopped, 0 zombie
Cpu(s): 8.5%us, 0.4%sy, 0.0%ni, 90.8%id, 0.3%wa, 0.0%hi, 0.0%si,
0.0%st
Mem: 1025056k total, 994780k used, 30276k free, 2940k buffers
Swap: 2031608k total, 103600k used, 1928008k free, 86892k cached

============================

#!/usr/bin/perl -w
use strict;
use Net::SSH::Perl;
my $host = '192.168.0.114';
my $username = 'pratap';
my $login_passwd = 'pratap123';
my $ssh = Net::SSH::Perl->new($host, $username);
$ssh->config->set('interactive', 1)
unless defined $ssh->config->get('interactive');


$ssh->login($username, $login_passwd);
my $cmd;
if ($cmd) {
my($out, $err, $exit) = $ssh->cmd($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";
}
==================================

Is there any way to get all the fields properly.

Thank you
Pratap

--0016e64c2ddc994a2b048fdafd4d--