jeku_expert
asked on
Calling the standard ftp client from a C program
Hi,
I need to download files from a ftp server and I need to initiate it from
within a c program. I would like to avoid expect scripts and libexpect as it
might not be available.
How can I handle the password prompt from the server?
And then place the file where I want it?
One of the exec functions would be the best solution for my case.
Any good examples?
I need to download files from a ftp server and I need to initiate it from
within a c program. I would like to avoid expect scripts and libexpect as it
might not be available.
How can I handle the password prompt from the server?
And then place the file where I want it?
One of the exec functions would be the best solution for my case.
Any good examples?
This one gets a file from the site:
Settings as above.
echo %TCPLogin% >getrep.txt
echo bin >>getrep.txt
echo hash >>getrep.txt
echo cd outgoing >>getrep.txt
echo get <Remote File> <Local File> >>getrep.txt
echo quit >>getrep.txt
ftp -v -i -n -s:getrep.txt %IPAddress%
Paul
Settings as above.
echo %TCPLogin% >getrep.txt
echo bin >>getrep.txt
echo hash >>getrep.txt
echo cd outgoing >>getrep.txt
echo get <Remote File> <Local File> >>getrep.txt
echo quit >>getrep.txt
ftp -v -i -n -s:getrep.txt %IPAddress%
Paul
>>How can I handle the password prompt from the server?
There is one other password needed, thats the PPP password. Use something like:
rasdial %Connection% %PPPName% %PPPPass%
after building a Dial-Up-Networking connection called whatever %Connection% is set to.
Paul
There is one other password needed, thats the PPP password. Use something like:
rasdial %Connection% %PPPName% %PPPPass%
after building a Dial-Up-Networking connection called whatever %Connection% is set to.
Paul
ASKER
Sorry, I forgot to mention that I am devoloping under linux.
>>Sorry, I forgot to mention that I am devoloping under linux.
This question should probably be posted in the linux section then as they are all C gurus anyway.
Are the command-line parameters to 'ftp' simillar on linux? The technique may still work.
Paul
This question should probably be posted in the linux section then as they are all C gurus anyway.
Are the command-line parameters to 'ftp' simillar on linux? The technique may still work.
Paul
ASKER
My problem is that I can not use any external (to the C program) scripts.
That is why I should avoid scripts like expect. Maybe I should change this to
a linux question as you suggest. I am new with expers-exchange so I need to find out how to best use it!
PS. Thanks for the effort!
That is why I should avoid scripts like expect. Maybe I should change this to
a linux question as you suggest. I am new with expers-exchange so I need to find out how to best use it!
PS. Thanks for the effort!
Do you have libfetch availiable on your system?
ASKER
No, it does not look like I have that either...
If it is availiable, it has calls like
FILE *
fetchGetFTP(struct url *u, struct url_stat *us, const char *flags);
and
FILE *
fetchPutFTP(struct url *u, const char *flags);
take a look into the documentation and code it up a bit for a start.
hope this helps,
van_dy
FILE *
fetchGetFTP(struct url *u, struct url_stat *us, const char *flags);
and
FILE *
fetchPutFTP(struct url *u, const char *flags);
take a look into the documentation and code it up a bit for a start.
hope this helps,
van_dy
>> No, it does not look like I have that either...
Hmm,
in that case, it seems like you are supposed to code up
a ftp client from scratch.
ASKER
I was hoping to just call the ftp client already available... And that should
be enough if I could find an easy way to give the client the password when he
asks for it...
be enough if I could find an easy way to give the client the password when he
asks for it...
ohh thats simpler
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) //argv[1] = server ip/name
{
char command[100];
strcpy(command, "ftp");
strcat(command, " ");
strcat(command, argv[1]);
system(command);
return 34;
}
wonder if something as simple will server ur purpose
#include <stdlib.h>
int main(int argc, char **argv) //argv[1] = server ip/name
{
char command[100];
strcpy(command, "ftp");
strcat(command, " ");
strcat(command, argv[1]);
system(command);
return 34;
}
wonder if something as simple will server ur purpose
ASKER
But no external scripts please.... :-)
Only standard linux functions like exec and similar.
Only standard linux functions like exec and similar.
>>if I could find an easy way to give the client the password when he asks for it
If the linux ftp client is like the Windoze one and van_dy's 'system' method works then you can pass the login name and password to ftp using the '-s:...' parameter like I do in my batch files.
Paul
If the linux ftp client is like the Windoze one and van_dy's 'system' method works then you can pass the login name and password to ftp using the '-s:...' parameter like I do in my batch files.
Paul
ASKER
It would have worked if I could pass the password as a parameter to ftp.
Or sftp that I might use... But they do not accept password as a parameter, unless there is something I have misunderstood?
They always ask for it afterwards.
Or sftp that I might use... But they do not accept password as a parameter, unless there is something I have misunderstood?
They always ask for it afterwards.
please check out the autologin feature in you
ftp client.
.netrc in your home directory contains information
that will be used to autologin into a server.
a typical .netrc looks like
-------------
machine 10.2.1.38
login somename
password mypassWORD
machine 10.6.2.56
login someothername
password someotherpassword
---------------------
now when you run somethign like
me@pc# ftp 10.6.2.56
ftp will read .netrc to supply the user name and password as and when the server asks
without user intervention.this can be used to code up something that might be useful
in your case.
ftp client.
.netrc in your home directory contains information
that will be used to autologin into a server.
a typical .netrc looks like
-------------
machine 10.2.1.38
login somename
password mypassWORD
machine 10.6.2.56
login someothername
password someotherpassword
---------------------
now when you run somethign like
me@pc# ftp 10.6.2.56
ftp will read .netrc to supply the user name and password as and when the server asks
without user intervention.this can be used to code up something that might be useful
in your case.
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
int main(int argc, char **argv)
{
FILE *fp = NULL;
char string[100];
int i = 0;
fp = fopen(".netrc", "w");
if(argc != 4){
printf("Usage: %s <username> <password> <servip>\n", argv[0]);
exit(1);
}
/* setup netrc file */
strcpy(string, "machine ");
strcat(string, argv[3]);
strcat(string, "\n");
fputs(string, fp);
strcpy(string, "\tlogin ");
strcat(string, argv[1]);
strcat(string, "\n");
fputs(string, fp);
strcpy(string, "\tpassword ");
strcat(string, argv[2]);
strcat(string, "\n");
fputs(string, fp);
fchmod(fileno(fp), 0600);
fclose(fp);
/* setting up .netrc complete */
/* invoke ftp */
strcpy(string, "ftp ");
strcat(string, argv[3]);
system(string);
// do something
return 34;
return 0;
}
#include <stdlib.h>
#include <sys/stat.h>
int main(int argc, char **argv)
{
FILE *fp = NULL;
char string[100];
int i = 0;
fp = fopen(".netrc", "w");
if(argc != 4){
printf("Usage: %s <username> <password> <servip>\n", argv[0]);
exit(1);
}
/* setup netrc file */
strcpy(string, "machine ");
strcat(string, argv[3]);
strcat(string, "\n");
fputs(string, fp);
strcpy(string, "\tlogin ");
strcat(string, argv[1]);
strcat(string, "\n");
fputs(string, fp);
strcpy(string, "\tpassword ");
strcat(string, argv[2]);
strcat(string, "\n");
fputs(string, fp);
fchmod(fileno(fp), 0600);
fclose(fp);
/* setting up .netrc complete */
/* invoke ftp */
strcpy(string, "ftp ");
strcat(string, argv[3]);
system(string);
// do something
return 34;
return 0;
}
ASKER
I think the type of solution I am looking for have to be something which does not require anything outside my c program. Password and username will be provided to me from a function call, but I can not expect to change the
configuration files or have any other script files. I think I need to read
"password:" from stdin and answer some way..
configuration files or have any other script files. I think I need to read
"password:" from stdin and answer some way..
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for the good advices. I will have to consider which of the three last option I will go for, but I think it will have to be one of them so I will split the points! Thanks!
%TCPLogin% reads 'user <UserName> <Password>'
%IPAddress% is a normal ip address.
%FileRoot%.%DataFile% reads the name of the file expected at the target.
This one logs you on and leaves you at the 'ftp>' prompt.
echo %TCPLogin% >logon.txt
echo bin >>logon.txt
echo hash >>logon.txt
ftp -v -i -n -s:logon.txt %IPAddress%
This one will send a named file.
echo %TCPLogin% >senddat.txt
:echo bin >>senddat.txt
echo hash >>senddat.txt
echo cd incoming >>senddat.txt
echo put <FileName> %FileRoot%.%DataFile% >>senddat.txt
echo quit >>senddat.txt
ftp -v -i -n -s:senddat.txt %IPAddress%
Good luck.
Paul