Net::SSH and Net::FTP?

------_=_NextPart_001_01C3B7DF.20E3F54A
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

I would like to use SSH to login to a remote system, then using FTP,
upload a file from the remote machine to another machine which will
display the data. If I can automate this process, I think I'll be in
great shape, tell me does this code seem like it would work for this?
>use Net::SSH::Perl;=20
>my $ssh =3D Net::SSH::Perl->new($host);	#Logging into the remote =
machine

>$ssh->login;=20
>my($READ, $WRITE) =3D $ssh->open2($cmd);=20
>print $WRITE "cd /var/log\n";
>print $WRITE "ftp\n";
>print $WRITE "open $mainhost\n";		#FTP connecting to main
web host to display the files.
>print $WRITE "username\n";
>print $WRITE "password\n";=20
>print $WRITE "cd /central/storage/location\n";
>print $WRITE "put filenamea\n";
>print $WRITE "put filenameb\n";
>print $WRITE "put filenamec\n";
>print $WRITE "bye\n"; 			#Disconnecting from the main web
host
>close($ssh);					#Disconnecting from the
SSH host


Peace,
Mark-Nathaniel Weisman
President / Owner
Weisman Bros. Construction
Anchorage, Alaska



------_=_NextPart_001_01C3B7DF.20E3F54A--
0
mark
12/1/2003 7:45:45 AM
perl.beginners 29381 articles. 4 followers. Follow

4 Replies
847 Views

Similar Articles

[PageSpeed] 35


> 
> I would like to use SSH to login to a remote system, then using FTP,
> upload a file from the remote machine to another machine which will
> display the data. If I can automate this process, I think I'll be in
> great shape, tell me does this code seem like it would work for this?
> >use Net::SSH::Perl; 
> >my $ssh = Net::SSH::Perl->new($host);	#Logging into the remote machine
> 
> >$ssh->login; 
> >my($READ, $WRITE) = $ssh->open2($cmd); 
> >print $WRITE "cd /var/log\n";
> >print $WRITE "ftp\n";
> >print $WRITE "open $mainhost\n";		#FTP connecting to main
> web host to display the files.
> >print $WRITE "username\n";
> >print $WRITE "password\n"; 
> >print $WRITE "cd /central/storage/location\n";
> >print $WRITE "put filenamea\n";
> >print $WRITE "put filenameb\n";
> >print $WRITE "put filenamec\n";
> >print $WRITE "bye\n"; 			#Disconnecting from the main web
> host
> >close($ssh);					#Disconnecting from the
> SSH host
> 

Does it work?  Best way to find out is to try it and see. 

'open2' isn't exactly well documented (read: not at all) which may or
may not be a reason to use or not to use it in the Net::SSH::Perl docs,
a cursory look at the source, assuming I understand (like you), that the
filehandles returned are actually attached to the STDOUT/STDIN of the
remote process rather than the SSH connection then it *should* work. Of
course you may run into nightmares with all kinds of things such as
whether or not the FTP client buffers input/output, whether or not it
will keep track of input thrown its way when it may not be ready, for
instance your SSH pipe is going to throw the username at the FTP client
regardless of whether your client has asked for it, same with the
password, commands, etc. So if your FTP client is stuck blocking waiting
for a response to connect to the remote location and you shove a
password down its throat I am not entirely sure how it will handle it,
it may work, it may cough up a fur ball.  Then there is the question of
catching and handling return codes and signals from the remote client,
aka what happens when the connection times out?

There are other options to explore as well, but most often the best way
to answer a question such as yours is to find a development environment
where you won't blow away anyone's production data and just give it a
shot.  I assume you can't 'scp' from remote server A to remote server B?
Can local server A connect directly to remote server B? 

Depending on your purposes and if this is production quality code I
would either a) test *a lot* b) look at other options...

http://danconia.org
0
wiggins
12/1/2003 4:22:19 PM
> >=20
> > I would like to use SSH to login to a remote system, then=20
> using FTP,=20
> > upload a file from the remote machine to another machine which will=20
> > display the data. If I can automate this process, I think=20
> I'll be in=20
> > great shape, tell me does this code seem like it would work=20
> for this?

Just a thought, if you're having trouble with the ssh part.

You could just use ftp for both servers?

    use Net::FTP;

    $ftpa =3D Net::FTP->new("a.host.name", Debug =3D> 0)
      or die "Cannot connect to a.host.name: $@";
    $ftpb =3D Net::FTP->new("b.host.name", Debug =3D> 0)
      or die "Cannot connect to b.host.name: $@";
=20
    $ftpa->login($usera,$passa)
      or die "Cannot login to A", $ftp->message;
    $ftpb->login($userb,$passb)
      or die "Cannot login to B", $ftp->message;

    $ftpa->cwd("/files/blah")
      or die "Cannot change working directory A", $ftp->message;
    $ftpb->cwd("/foo/monkey")
      or die "Cannot change working directory B", $ftp->message;

    for(@filestotransfer) {
# then use $ftpa->read() and $ftpb->write() to xfr them between each =
other,=20
# sorry for lack of example I'm a bit busy!
    }

    $ftpa->quit;
    $ftpb->quit;

Transfer files back and forth between the 2 Net::FTP objects. I'm not=20
saying it's better or anythgin, just another way to do it that may help=20
if ssh is being tricky as it some times can.

HTH

DMuey
0
dmuey
12/2/2003 3:37:46 PM
Thanks for the reply Dan, the two remote servers do not have FTP running
on them, so they must initialize the FTP connection. I've now built the
scripts on their ends, I'm working on an SSH script that will login to
them and launch the scripts on each server. Got the traffic direction
thing going on, so I'm trying to figure out who can login where? I'll
post the results once they all work. Right in the middle of all this, my
primary server got upgraded to 5.6.1 so we'll see how that messes
everything up? Ever forward.

Sincerely in Christ,
Mark-Nathaniel Weisman
President / Owner
Outland Domain Group
Anchorage, Alaska



-----Original Message-----
From: Dan Muey [mailto:dmuey@infiniplex.com]=20
Sent: Tuesday, December 02, 2003 6:38 AM
To: beginners@perl.org
Subject: RE: Net::SSH and Net::FTP?


> >=20
> > I would like to use SSH to login to a remote system, then
> using FTP,
> > upload a file from the remote machine to another machine which will
> > display the data. If I can automate this process, I think=20
> I'll be in
> > great shape, tell me does this code seem like it would work
> for this?

Just a thought, if you're having trouble with the ssh part.

You could just use ftp for both servers?

    use Net::FTP;

    $ftpa =3D Net::FTP->new("a.host.name", Debug =3D> 0)
      or die "Cannot connect to a.host.name: $@";
    $ftpb =3D Net::FTP->new("b.host.name", Debug =3D> 0)
      or die "Cannot connect to b.host.name: $@";
=20
    $ftpa->login($usera,$passa)
      or die "Cannot login to A", $ftp->message;
    $ftpb->login($userb,$passb)
      or die "Cannot login to B", $ftp->message;

    $ftpa->cwd("/files/blah")
      or die "Cannot change working directory A", $ftp->message;
    $ftpb->cwd("/foo/monkey")
      or die "Cannot change working directory B", $ftp->message;

    for(@filestotransfer) {
# then use $ftpa->read() and $ftpb->write() to xfr them between each
other,=20
# sorry for lack of example I'm a bit busy!
    }

    $ftpa->quit;
    $ftpb->quit;

Transfer files back and forth between the 2 Net::FTP objects. I'm not=20
saying it's better or anythgin, just another way to do it that may help=20
if ssh is being tricky as it some times can.

HTH

DMuey

--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

0
mark
12/3/2003 1:37:19 AM
> Thanks for the reply Dan, the two remote servers do not have=20
> FTP running on them, so they must initialize the FTP=20

Nuts!

> connection. I've now built the scripts on their ends, I'm=20
> working on an SSH script that will login to them and launch=20
> the scripts on each server. Got the traffic direction thing=20
> going on, so I'm trying to figure out who can login where?=20

Here's a nutty idea if permissions allow:
	pu the ftp script on one servers and call it via http(s) instead of ssh
	I do that to update certain files across multiple places:
		- submit a form that modifies a local file
		- then it ftp's the new file to several servers giving me=20
		status on each one so if it fails I can do it by hand

Not sure if that will help or not but you never know!
Of course ssh is cool too so either way I'm happy!

DMuey

> I'll post the results once they all work. Right in the middle=20
> of all this, my primary server got upgraded to 5.6.1 so we'll=20
> see how that messes everything up? Ever forward.
>=20
> Mark-Nathaniel Weisman
> President / Owner
> Outland Domain Group
> Anchorage, Alaska
>=20
>=20
>=20
> -----Original Message-----
> From: Dan Muey [mailto:dmuey@infiniplex.com]=20
> Sent: Tuesday, December 02, 2003 6:38 AM
> To: beginners@perl.org
> Subject: RE: Net::SSH and Net::FTP?
>=20
>=20
> > >=20
> > > I would like to use SSH to login to a remote system, then
> > using FTP,
> > > upload a file from the remote machine to another machine=20
> which will=20
> > > display the data. If I can automate this process, I think
> > I'll be in
> > > great shape, tell me does this code seem like it would work
> > for this?
>=20
> Just a thought, if you're having trouble with the ssh part.
>=20
> You could just use ftp for both servers?
>=20
>     use Net::FTP;
>=20
>     $ftpa =3D Net::FTP->new("a.host.name", Debug =3D> 0)
>       or die "Cannot connect to a.host.name: $@";
>     $ftpb =3D Net::FTP->new("b.host.name", Debug =3D> 0)
>       or die "Cannot connect to b.host.name: $@";
> =20
>     $ftpa->login($usera,$passa)
>       or die "Cannot login to A", $ftp->message;
>     $ftpb->login($userb,$passb)
>       or die "Cannot login to B", $ftp->message;
>=20
>     $ftpa->cwd("/files/blah")
>       or die "Cannot change working directory A", $ftp->message;
>     $ftpb->cwd("/foo/monkey")
>       or die "Cannot change working directory B", $ftp->message;
>=20
>     for(@filestotransfer) {
> # then use $ftpa->read() and $ftpb->write() to xfr them=20
> between each other,=20
> # sorry for lack of example I'm a bit busy!
>     }
>=20
>     $ftpa->quit;
>     $ftpb->quit;
>=20
> Transfer files back and forth between the 2 Net::FTP objects. I'm not=20
> saying it's better or anythgin, just another way to do it=20
> that may help=20
> if ssh is being tricky as it some times can.
>=20
> HTH
>=20
> DMuey
>=20
> --=20
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
>=20
>=20
> --=20
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
>=20
>=20
0
dmuey
12/3/2003 2:42:25 PM
Reply: