Thread: manipulating ftp commands from c, is it possible?

  1. #1
    Unregistered
    Guest

    Question manipulating ftp commands from c, is it possible?

    Hello to all,

    I found out earlier that we can log into a ftp server from a c program with the following line:

    system("ftp serverName");

    provided that the user's root directory has a file named .netrc with the following line:

    machine serverName login usersName passward usersPasswd

    Note: .netrc should have the permission of 600.

    After login, I could not use any ftp commands such as get, put etc. from the c program, are there any ways to do that (I mean from the c program)?

    Any suggestions would be highly appreciated.

    Damar

  2. #2
    Unregistered
    Guest
    Thanks,

    OS is linux RH7.1 and compiler gcc.

    Damar

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure, you're signing in, but that does nothing for you. It doesn't actually put you in an FTP shell so you can do whatever you like. You either have to:

    a) keep sending / getting info from the FTP session using "system" over and over

    b) connect directly using sockets


    a) is a really crappy way to do it
    b) is much better

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Unregistered
    Guest
    Thanks,

    Connect through socket means I have to place a socket server program in the ftp server, and connect from the client. I have shell access in the ftp server, but I may not have right to run a socket server program there (FTP server I am trying to connect to belongs to my ISP!).

    If I go for your option (a) ie "keep sending / getting info from the FTP session using "system" over and over", how can I do that?
    Since I only have to use three commands (cd toDir, put aFile, and bye), this option should be okay.

    Thanks once again.

    Damar

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. Goto this site
    http://www.cs.cf.ac.uk/Dave/C/CE.html

    2. Scroll down to
    Process Control: <stdlib.h>,<unistd.h>
    Interprocess Communication (IPC), Pipes

    3. READ

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    7
    Dear Salem,

    Thank for your respond, and I went through the link you provided.

    I understand the basic of PiPES, but could not figure out how it can solve my problem.

    Can you provide a little more clues on this, please?

    Thanking you in advance.

    Da ar

  7. #7
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    The ftp command is meant for interactive use.
    Do you have wget installed?
    wget ftp://user:[email protected]/path/filename
    gets a file in one command...

    alex

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    7
    Alex,

    Seems, wget is the answer.

    Now, I am reading the manual of GNU Manual.

    Thanks.

    Damar

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    7
    Alex,

    Before I go further, can I use wget to upload to the ftp server(using put command) rather than downloading from there?

    Thanks,

    Damar

  10. #10
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    I guess I didn't read your original post too well...

    I checked the debian packages containing the substring "ftp"... and I found two possible solutions:

    use lftp (this one can put and get from the command line)
    -------------
    Description: Sophisticated command-line FTP/HTTP client programs
    lftp is a file retrieving tool that supports FTP and HTTP protocols under both IPv4 and IPv6. lftp has an amazing set of features, while preserving its interface as simple and easy as possible.
    .
    lftp can also be scriptable, it can be used to mirror sites, it let you copy files among remote servers (even between FTP and HTTP). It has an extensive online help. It supports bookmarks, and connecting to several ftp/http sites at the same time.
    -------------

    or use ftplib-dev/ftplib3
    -------------
    Description: Library of callable ftp routines
    Ftplib presents a convenient C interface for the standard File Transfer Protocol (FTP). It makes it easier for programmers to use file transfer in their programs.
    -------------

    This last one is of course the more sophisticated one... Moreover, the example programs ftpget and ftpsend do exactly what you want. (and it is also available for windows...)

    greetinx,

    alex

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    7
    Dear Alex,

    Thanks for your very informative response.

    I checked all your suggested applications/solutions, and saw the last one ie libftp (seeing its documentation with all its implemented functions) as a promising candidate to fulfil my requirement.

    But in the documentation, I did not find any thing in terms of how to setup libftp (ftplib.h) with gcc.

    I have done the followings so far:

    1. I downloaded ftplib-3.1-bin-linux-i386.tar.gz file.

    2. untarred/unzipped the file in /usr/local directory.

    3. Now,libftp.so.3.1, qftp, and other REAME files are in /usr/local/ftplib-3.1.

    What is/are next step/s I have to take so that when my c program has #include<ftplib.h>, gcc will finds it out to do the rest?

    Thanks once again.

    Damar



    cd

  12. #12
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    If everything is installed correctly, you should be able to use it simply by including the line "#include <ftplib.h>" in your .c-file, just below all other #include lines, and linking the library to the executable by adding "-lftp" to the gcc command-line.

    I checked my installation of ftplib (installing it is as easy as typing "apt-get install ftplib-dev" on a Debian system) I have the following files (excluding helpfiles, examples, man-pages and debian menu files):

    /usr/include/ftplib.h
    /usr/lib/libftp.a
    /usr/lib/libftp.so.3.1
    /usr/lib/libftp.so -> /usr/lib/libftp.so.3.1
    /usr/lib/libftp.so.3 -> /usr/lib/libftp.so.3.1

    alternate directories (where /usr is replaced by /usr/local) should also work automatically. If they are in other directories, you can specify these by adding the -I<dir> switch for the include directory and the -L<dir> switch for the library directory BEFORE the -lftp switch. (or you can make symlinks to the files in the /usr/include and /usr/lib directories)

    Hope this helps...

    alex

  13. #13
    Unregistered
    Guest

    ftplib - start up

    Hi,

    I downloaded ftplib3.1 source from its home page, and did the followings:

    1. gzip (uncompressed) the file in /usr/local/ftplib-3.1 directory.

    2. In /usr/local/ftplib-3.1/linux directory (I am using RH7.1), I copied the patch file, ftplib-3.1-1.patch.txt, and executed:

    patch -p0 ftplib.c ftplib-3.1-1.patch.txt

    3. On the same directory, I executed:

    make

    4. On the same directory, I executed:

    make install

    The above steps installed the require libraries in /usr/local/lib, /usr/lib/include etc directories.

    After installation, I tried to compile the following program:

    #include<stdio.h>
    #include "/usr/local/include/ftplib.h"

    int main(){

    int connectED, logIN;
    nebuf *buf;
    char *user="damar";
    char *password ="mypassword";
    char * host="ftp.myDomain.com";


    FtpInit();
    connectED=FtpConnect(host, &buf);
    logIN=FtpLogin(user, password, buf);
    }


    On compilation, I got the following error message:


    /tmp/ccLiWX1n.o: In function 'main':
    /tmp/ccLiWX1n.0(.text+0x1c); undefined reference to 'FtpInit'
    /tmp/ccLiWX1n.0(.text+0x2b); undefined reference to 'FtpConnect'
    /tmp/ccLiWX1n.0(.text+0x44); undefined reference to 'FtpLogin'

    Collect2: ld returned 1 exit status

    Can any one please tell me what I have not done or done wrong?
    Is it my installation process, the above program, or both are wrong?

    Thanks.

    Damar

  14. #14
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    Looks like you forgot -lftp:

    gcc program.c -lftp

    alex

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-12-2009, 12:49 PM
  2. How to program in unix
    By Cpro in forum Linux Programming
    Replies: 21
    Last Post: 02-12-2008, 10:54 AM
  3. manipulating ftp commands from c, is it possible?
    By CyBeR in forum C Programming
    Replies: 1
    Last Post: 06-03-2004, 01:22 PM
  4. FTP Server :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 10-03-2002, 07:14 PM