当前位置: 技术问答>linux和unix
如何在Linux下用C语言访问ftp服务器中的文件?
来源: 互联网 发布时间:2015-10-19
本文导语: 有一台ftp服务器,我需要在Linux下用C语言读取那个服务器中ftp的文件。总不至于让我编一个ftp客户端吧?有什么现成的ftp客户端的库文件可以用,或者谁能写出shell脚本反复调用系统带的ftp,每隔几分钟就自动把ftp se...
有一台ftp服务器,我需要在Linux下用C语言读取那个服务器中ftp的文件。总不至于让我编一个ftp客户端吧?有什么现成的ftp客户端的库文件可以用,或者谁能写出shell脚本反复调用系统带的ftp,每隔几分钟就自动把ftp server中的文件读到本地计算机?
|
以前写过,就是从一个ftp服务端下载一个叫net的文件,使用的是被动模式,下面是接口,希望对你有所帮助。
int ftpcmd(int sockftp,char *fmt,...) //cmd
{
va_list vp;
int err,len;
int i,j,n,p;
if (ftpio == NULL)
{
ftpio = fdopen(sockftp,"r"); //socket->file pointer
if (ftpio == NULL)
return -1;
}
if (fmt)
{
va_start(vp,fmt);
len = vsprintf(ftp_buf,fmt,vp);
ftp_buf[len++] = 'r';
ftp_buf[len++]='n';
write(sockftp,ftp_buf,len); //buf->socket
}
n=read(sockftp,ftp_buffer,sizeof(ftp_buffer));
printf("the buff is %sn",ftp_buffer);
if(fmt=="PASV") //被动模式下提取需要连接服务端的端口
{
ftp_buffer[n-3]='';
str1=ftp_buffer+39;
str2=strchr(str1,',');
i=atoi(str2+1);
n=0;
while(str1[n]!=',')
{
str3[n]=str1[n];
n++;
}
j=atoi(str3);
p=j*256+i;
return p;
}
return 1;
}
int ftp()
{
char *host="192.168.0.4";
char *user="yuan";
char *pass="123456";
char *filename="net";
char *savefile="net";
int sockftp = -1,sockdata = -1,sockxfer = -1;
struct sockaddr_in addr;
unsigned long hostip;
int len,tmp,err,err1;
int retval = -1;
int savefd;
char *del=":";
char *port;
struct hostent *he;
hostip = inet_addr(host); //ip
sockftp = socket(AF_INET,SOCK_STREAM,0); //control socket
if (sockftp == -1)
goto out;
addr.sin_family = PF_INET;
addr.sin_port = htons(21);
addr.sin_addr.s_addr = hostip;
if (connect(sockftp,(struct sockaddr *)&addr,sizeof(addr)) == -1) //wait for connect
goto out;
err = ftpcmd(sockftp,NULL); //connect ftp server
err = ftpcmd(sockftp,"USER %s",user);
err = ftpcmd(sockftp,"PASS %s",pass);
err = ftpcmd(sockftp,"TYPE I");
err1 = ftpcmd(sockftp,"PASV");
tmp = sizeof(addr);
sockdata = socket(AF_INET,SOCK_STREAM,0); //data socket
if (sockdata == -1)
goto out;
memset(&addr,0,tmp);
addr.sin_family = AF_INET;
addr.sin_port = htons(err1);
addr.sin_addr.s_addr = inet_addr(host);;
sockxfer = connect(sockdata,&addr,tmp);
if (sockxfer == -1)
goto out;
err=ftpcmd(sockftp,"RETR %s",filename);
/*savefd = open(savefile,O_RDWR | O_CREAT); //create file
if (savefd == -1)
goto out; */
len=read(sockdata,ftp_buf,sizeof(ftp_buf));
client_ip=strtok(ftp_buf,del);
port=strtok(NULL,del);
client_port=atoi(port);
printf("the cilentip is %s,the port is %dn",client_ip,client_port);
/*write(savefd,ftp_buf,len);
close(savefd);*/
out:
close(sockxfer);
close(sockdata);
close(sockftp);
}
int ftpcmd(int sockftp,char *fmt,...) //cmd
{
va_list vp;
int err,len;
int i,j,n,p;
if (ftpio == NULL)
{
ftpio = fdopen(sockftp,"r"); //socket->file pointer
if (ftpio == NULL)
return -1;
}
if (fmt)
{
va_start(vp,fmt);
len = vsprintf(ftp_buf,fmt,vp);
ftp_buf[len++] = 'r';
ftp_buf[len++]='n';
write(sockftp,ftp_buf,len); //buf->socket
}
n=read(sockftp,ftp_buffer,sizeof(ftp_buffer));
printf("the buff is %sn",ftp_buffer);
if(fmt=="PASV") //被动模式下提取需要连接服务端的端口
{
ftp_buffer[n-3]='';
str1=ftp_buffer+39;
str2=strchr(str1,',');
i=atoi(str2+1);
n=0;
while(str1[n]!=',')
{
str3[n]=str1[n];
n++;
}
j=atoi(str3);
p=j*256+i;
return p;
}
return 1;
}
int ftp()
{
char *host="192.168.0.4";
char *user="yuan";
char *pass="123456";
char *filename="net";
char *savefile="net";
int sockftp = -1,sockdata = -1,sockxfer = -1;
struct sockaddr_in addr;
unsigned long hostip;
int len,tmp,err,err1;
int retval = -1;
int savefd;
char *del=":";
char *port;
struct hostent *he;
hostip = inet_addr(host); //ip
sockftp = socket(AF_INET,SOCK_STREAM,0); //control socket
if (sockftp == -1)
goto out;
addr.sin_family = PF_INET;
addr.sin_port = htons(21);
addr.sin_addr.s_addr = hostip;
if (connect(sockftp,(struct sockaddr *)&addr,sizeof(addr)) == -1) //wait for connect
goto out;
err = ftpcmd(sockftp,NULL); //connect ftp server
err = ftpcmd(sockftp,"USER %s",user);
err = ftpcmd(sockftp,"PASS %s",pass);
err = ftpcmd(sockftp,"TYPE I");
err1 = ftpcmd(sockftp,"PASV");
tmp = sizeof(addr);
sockdata = socket(AF_INET,SOCK_STREAM,0); //data socket
if (sockdata == -1)
goto out;
memset(&addr,0,tmp);
addr.sin_family = AF_INET;
addr.sin_port = htons(err1);
addr.sin_addr.s_addr = inet_addr(host);;
sockxfer = connect(sockdata,&addr,tmp);
if (sockxfer == -1)
goto out;
err=ftpcmd(sockftp,"RETR %s",filename);
/*savefd = open(savefile,O_RDWR | O_CREAT); //create file
if (savefd == -1)
goto out; */
len=read(sockdata,ftp_buf,sizeof(ftp_buf));
client_ip=strtok(ftp_buf,del);
port=strtok(NULL,del);
client_port=atoi(port);
printf("the cilentip is %s,the port is %dn",client_ip,client_port);
/*write(savefd,ftp_buf,len);
close(savefd);*/
out:
close(sockxfer);
close(sockdata);
close(sockftp);
}
|
这种shell多的跟马一样。
--------------------------------
cd $PORTAL_WAP/$LogYear$LogMonth$LogDay
ftp -in $IP
--------------------------------
cd $PORTAL_WAP/$LogYear$LogMonth$LogDay
ftp -in $IP