LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-20-2009, 06:57 AM   #1
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Rep: Reputation: 23
automate an ftp connection from a c++ program


I need to logon to an ftp using a user-name and password, send a file, get a file and then logoff.

for example:

ftp 1.2.3.3
Connected to 1.2.3.4.
Name (1.2.3.4:user): user-name
Password required for user-name.
Password: password
User user-name logged in.
ftp>

and then send a file and then receive a file.

Two questions:

1) How to send and recevie files with ftp?
2) how to automate this from a c++ program. I have tried system() for each line above but it does nothing.

I am working on an embedded platfrom and have few programs current available so would prefer to use ftp rather than kermi or lftp.

Any ideas?
 
Old 05-20-2009, 07:22 AM   #2
ChrisAbela
Member
 
Registered: Mar 2008
Location: Malta
Distribution: Slackware
Posts: 572

Rep: Reputation: 154Reputation: 154
I don't know much about C++, but if you stick to the Linux OS, you may use wget and wput.

Chris
 
Old 05-20-2009, 07:34 AM   #3
bhaslinux
Member
 
Registered: Oct 2003
Location: UnitedKingdom
Distribution: Debian Bullseye
Posts: 357

Rep: Reputation: 49
you have to get hold of the FTP RFC
it typically listens in port 20 and 21.
You have to control the ftp using 20 port and the data comes in 21
there are other methods to receive file just by using port 21 only
which is referred-to as 'passive mode'

take a look at RFC 959 to start with
http://www.faqs.org/rfcs/rfc959.html
 
Old 05-20-2009, 07:50 AM   #4
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
ChrisAbela

Funny I forgot about using wget and wput! However, both of these aren't available on my embedded Linux platform so I'd have to get the source and compile these. I will consider this as an alternative option.

Thanks,
 
Old 05-20-2009, 07:51 AM   #5
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
bhaslinux

Oh my word that sounds rather low-level. are you referring to controlling the ftp stuff myself? Surely this isn't necessary?
 
Old 05-20-2009, 07:53 AM   #6
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
I was hoping for a solution that could use ftp, that is poke the name and password and other commands when promted?

Surely this can be done in bash if not c++?
 
Old 05-20-2009, 07:57 AM   #7
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
For example if I type ftp 1.2.3.4 in the terminal the ftp program is invoked and a name followed by password is prompted.

However, if I write a bash script doing the same the terminal shows nothing - e.g. script:

ftp 1.2.3.4

Again from c++ if I call system("ftp 1.2.3.4"); the ternial is blank, waiting for the program to do something.
 
Old 05-20-2009, 08:19 AM   #8
rriggs
Member
 
Registered: Mar 2009
Location: Colorado, US
Distribution: Fedora 13, Fedora 14, RHEL6 Beta
Posts: 46

Rep: Reputation: 17
Why not use libcurl for this?
 
Old 05-20-2009, 09:56 AM   #9
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
rriggs

libcurl is a good alternative but agin, I have to get source and compile in for my target whilst ftp is already there.
 
Old 05-20-2009, 03:06 PM   #10
ChrisAbela
Member
 
Registered: Mar 2008
Location: Malta
Distribution: Slackware
Posts: 572

Rep: Reputation: 154Reputation: 154
Hi DEF.

I have to insist on shell coomands:-)

There are 2 options:

1. (sleep 3; echo username; sleep 3 ; echo password ; sleep 5 ; echo "put foo" ; sleep 3 ; echo "get foobar" ; sleep 3 ; echo "quit") | ftp remote_hostname

2. cat $HOME/.netrc
machine remote_hostname login username password the_password
macdef init
put foo
get foobar
quit

VERY IMPORTANT NOTES:

chmod 600 .netrc
the last line has to be empty

see: man netrc

Chris

Last edited by ChrisAbela; 05-20-2009 at 03:08 PM.
 
Old 05-20-2009, 06:33 PM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by DEF. View Post
rriggs

libcurl is a good alternative but agin, I have to get source and compile in for my target whilst ftp is already there.

But 'curl' and its library are distributed in source form.
 
Old 05-20-2009, 08:47 PM   #12
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
You can only use system() to run a program in a “non-interactive” manner. You have a few ways to proceed (in POSIX C/C++):
  1. Use a library as has been suggested. This does away with calling an external program, and instead you do all ftp work in C(++).
  2. As ChrisAbela has stated, the ftp autologin can be controlled by .netrc file (essentially turning an interactive command into a non-interactive command). So to do this, you might (over)write relevant information to this file using your preferred I/O functions (e.g., iostreams, C stdio, POSIX open()/write()). Once you are done, then use system("ftp") to launch the ftp command (with no arguments); it should autologon and process the init macro without any further input.
  3. Since system() launches a shell to launch your command, you can build a very complicated string which uses the shell to accomplish piping. For example, keeping with ChrisAbela’s approach, you would do something like (if you use C++ strings):
    Code:
    	std::string command("(sleep 3; echo " + username +
    	                    "; sleep 3 ; echo " + password +
    	                    "; sleep 5 ; echo \"put" + putfile_name +
    	                    "\" ; sleep 3 ; echo \"get " + getfile_name +
    	                    "\" ; sleep 3 ; echo \"quit\") | ftp " + remote_hostname);
    
    	system(command.c_str());
    Here you would already have strings for username, etc. Of course, this option may not be very reliable, depending on your ftp client. If precise timing is required (i.e., you can only enter the username after the prompt comes up), then you may get into problems. If, however, your ftp client will accept commands on a buffered input stream (and process them sequentially) which is the way most work (I think), then you can eliminate all the sleep statements and be confident that your code will work.
  4. The next option will only work if your ftp client is not time-sensitive. You can open a pipe to the ftp client with popen("ftp"). Then, successively write the lines you would normally type to the FILE* with fwrite().
  5. The last option (along second) is the only reliable way to call an external ftp client if it is timing sensitive. You will need to open two pipes (with pipe()), then fork(). In the parent process you close the read end of one pipe and the write end of the other. In the child process close the opposite ends, and dup2() the pipes with the the STDIN and STDOUT file descriptors. Then exec ftp in the child. In the parent, you will have a loop which reads from the reading pipe, until it sees a prompt. After this, you can go ahead and write the next line to the writing pipe. The if you write the quit string, the child should exit, and you should get EOF on your attempt to read the pipe, so your loop will exit.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Automate FTP steps vinayras Linux - Software 4 03-30-2007 06:06 AM
LXer: Speaking Unix, Part 6: Automate, automate, automate! LXer Syndicated Linux News 0 01-04-2007 09:54 AM
How to automate an FTP process? ksakamuri Linux - Newbie 6 11-05-2006 05:52 PM
automate ftp (non-GUI) prx Linux - Software 1 04-26-2005 09:14 PM
Automate FTP noodle123 Linux - General 1 10-15-2002 04:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration