Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1076
  • Last Modified:

Socket:INET + Pipe to external program

Hello
i want to write a perl file that starts a ftp program and then a udp server that listens for commands to send to the ftp program.

For tetsing purpose i used:
---------------------------
#!/usr/bin/perl
open(FTP, "|/home/user1/ftp/bin/ftp");
print FTP "open ftp://user:pass@127.0.0.1:21/test/";
----------------------------

So far everything works fine.
But if I add the INET Socket part it does not work anymore:

----------------------------
#!/usr/bin/perl
use IO::Socket::INET;
my $PASSWORD = 'test';open(FTP, "|/home/user1/ftp/bin/ftp") ;
my $sock = new IO::Socket::INET(LocalPort => 45720,Proto => "udp") || die @!;
printf "Waiting for message..\n";
while ($sock->recv($buf, 1024)) {
        chomp $buf;
        my ($password, $rcmd) = split(/ /, $buf);
        if ($password ne $PASSWORD) {
                printf "Wrong password from client (%s), ignoring request ..\n", $password;
                next;
        }
        print FTP "open ftp://user:pass@1.1.1.1:21/test/";
        printf "New message .. %s \n",  $rcmd;
        printf "Waiting for next message..\n";
}
print FTP "quit";
close(FTP);
exit 0;
----------------------------

This does not work as i thought it would.
I think the ftp program gets started (using ps -aux tells me it is running) but the line:
print FTP "open ftp://user:pass@1.1.1.1:21/test/";
is not sent to the program for some reason.

To avoid unnecessary questions:
I use: print FTP "open ftp://user:pass@1.1.1.1:21/test/";
for testing purpose later it should be print FTP "$rcmd"

Thank you
bug2k3
0
Bug2k3
Asked:
Bug2k3
  • 3
  • 2
  • 2
1 Solution
 
FishMongerCommented:
Why not use the Net::FTP module instead of the pipe to the external prgram?  The module will give you better control of the FTP connection, and provides error status and handling.

http://search.cpan.org/~gbarr/libnet-1.19/Net/FTP.pm
0
 
Bug2k3Author Commented:
The external program comes with some extra functions the NET::FTP module does not have.
0
 
FishMongerCommented:
What functions do you need?  The FTP module has some functions that are available to it but are not listed because they are inherited from Net::Netrc, Net::Cmd, and IO::Socket::INET.  Could those inherited functions be the ones you need?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Bug2k3Author Commented:
Believe me, the things I use are not included in those components/modules.
And my question is not limited to ftp programs but to all command line applications in combination with the Socket:INET Server.
0
 
jmcgOwnerCommented:
You got this to work without sending line terminations down the pipe to the FTP command? That seems anomalous.

If it's necessary to use this external FTP application, you might prefer to run it under the control of something like the Expect module. When you send commands down the pipe, there is no reverse communication channel to see the results in your program.

0
 
Bug2k3Author Commented:
Thanks for your answers. I solved the problem by disabling the puffering.
-----------------------------
my $oldfh = select(FTP);
$| = 1;
select($oldfh);
-----------------------------
I also added the line terminations and now it works fine.

So i think this problem is solved.
Thanks
0
 
jmcgOwnerCommented:
Buffering was to be the next suspect, but I couldn't point a finger at it in the presence of your claim to have things working in that first example.

Since you've mostly solved this problem without our help, do you want to have this question PAQed and your points refunded?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now