piping input to ssh session

Hi there,

I am trying to input a bunch of commands to an ssh session. One way to
do it is to create a file and pipe it. Something like 'cat <filename> |
ssh admin [at] <device>' would work.

But what If I just want to send a bunch of lines of text without
creating a file. How can I do it?


Cheers,

Noah


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Noah [ Fr, 23 Juli 2010 21:59 ] [ ID #2045045 ]

Re: piping input to ssh session

2010/7/24 Noah <noah-list [at] enabled.com>:

>
> But what If I just want to send a bunch of lines of text without creating=
a
> file. =A0 How can I do it?
>
>

Try Net::SSH::Perl.

my $ssh =3D Net::SSH::Perl->new("host1");
$ssh->login("user1", "pass1");

$ssh->cmd("foo");
$ssh->cmd("bar");

SSH-2 fully supports running more than one command over the same connection=
..


--
Jeff Pang
http://home.arcor.de/pangj/

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jeff Pang [ Fr, 23 Juli 2010 22:15 ] [ ID #2045047 ]

Re: piping input to ssh session

On 7/23/10 Fri Jul 23, 2010 12:59 PM, "Noah" <noah-list [at] enabled.com>
scribbled:

> Hi there,
>
> I am trying to input a bunch of commands to an ssh session. One way to
> do it is to create a file and pipe it. Something like 'cat <filename> |
> ssh admin [at] <device>' would work.
>
> But what If I just want to send a bunch of lines of text without
> creating a file. How can I do it?

Use the "pipe" mode argument to open:

open(my $cmd, '|-', 'command') or die ...

A fork is done to execute 'command', and print statements to $cmd will be
sent to to the standard input of 'command'. I don't know if this will work
for ssh. Note that you cannot easily get the output from ssh this way.

See 'perldoc perlipc' and search for the section titled "Using open() for
IPC". See also 'perldoc -f open'.

See also the Net::SSH::Perl module (as Jeff Pang has suggested).




--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jim Gibson [ Fr, 23 Juli 2010 22:26 ] [ ID #2045048 ]

Re: piping input to ssh session

On Fri, Jul 23, 2010 at 3:59 PM, Noah <noah-list [at] enabled.com> wrote:
> But what If I just want to send a bunch of lines of text without creating=
a
> file. =C2=A0 How can I do it?

It's not a Perl solution, but you can just echo the lines to ssh...

echo 'line1
line2
line3' | ssh ...

Be sure to escape single quotes. :)

$ echo 'line1
line2 with a single-quote: '\''
line3' | ssh ...

--
Brandon McCaig <bamccaig [at] gmail.com>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <bamccaig [at] castopulence=
..org>

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Brandon McCaig [ Sa, 24 Juli 2010 01:24 ] [ ID #2045052 ]

Re: piping input to ssh session

On 07/23/2010 09:59 PM, Noah wrote:
> Hi there,
>
> I am trying to input a bunch of commands to an ssh session. One way to
> do it is to create a file and pipe it. Something like 'cat <filename> |
> ssh admin [at] <device>' would work.
>
> But what If I just want to send a bunch of lines of text without
> creating a file. How can I do it?

There are several ways, for instance:

use Net::OpenSSH;

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

my $output = $ssh->capture({stdin_data => $input}, $cmd)
print $output;

But this may or may not be what you want.

Tell us your real problem, what you are trying to achive!

- Salva

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Salvador Fandino [ Di, 27 Juli 2010 09:36 ] [ ID #2045222 ]
Perl » gmane.comp.lang.perl.beginners » piping input to ssh session

Vorheriges Thema: Data migration
Nächstes Thema: Extract data from BEncoded .torrent files