简体   繁体   English

如何使用system()在c中执行2个连续的系统命令,例如连接ftp并使用程序登录?

[英]How to execute 2 sucessive system command in c using system() like connecting to ftp and logging in using program?

I want to use the ftp command using system function in c. 我想在c中使用系统功能使用ftp命令。 I need to execute 3 commands. 我需要执行3条命令。

1 FTP 1个FTP
2 USERNAME 2个用户名
3 PASSWORD 3个密码

how do i perform the above 3 using system().... i am stucked up here 我如何使用system()执行以上3个...。我被卡在这里
system(ftp); 系统(FTP);

You might want to check if your version of ftp allows you to specify the user name and password on the command line (some do). 您可能要检查您的ftp版本是否允许您在命令行上指定用户名和密码(有些需要这样做)。 Of course, you could also implement ftp either on your own (using sockets directly), or via a library (there are several around). 当然,您也可以自己(直接使用套接字)或通过库(周围有几种)实现ftp。

Otherwise, if you really want to run the system's ftp command, you probably want to use popen (or, on Windows, _popen ) instead. 否则,如果您确实要运行系统的ftp命令,则可能要使用popen (或在Windows上为_popen )。

FILE *cmd = popen("cmd", "w");

if (NULL != cmd)
    fprintf(cmd, "%s\n%s", user_name, password);

What you write to the FILE * goes to the standard input of the child process (and if you open with "r" instead of "w" , you can read from the child's standard output). 您写入FILE *将进入子进程的标准输入(并且如果使用"r"而不是"w"打开,则可以从子级的标准输出中读取内容)。 Many (but not all) recent versions of popen also support "rw" , so you can write to the child's standard input and read from its standard output. popen许多(但不是全部)最新版本也支持"rw" ,因此您可以写入子级的标准输入并从其标准输出中读取。

Note, however, that this will only work for a child process that reads from its standard input. 但是请注意,这仅适用于从其标准输入读取的子进程。 Some use other methods to read directly from the console/keyboard, and it won't (necessarily) work with those. 有些使用其他方法直接从控制台/键盘读取,并且(无法)与之配合使用。

I recommend you using some other API instead of system(). 我建议您使用其他一些API代替system()。 System() is considered as non standard way for supplying commands. System()被视为提供命令的非标准方式。 I would suggest use only FTP command for opening that terminal and then use ftp apis overs winsock api's. 我建议仅使用FTP命令打开该终端,然后使用ftp apis overs winsock api。

These are few library I know. 这些是我所知道的图书馆。 please use if, 请使用

libftp 
ftplib 

enough documentation can be found in web for this. 在网络上可以找到足够的文档。 Please let me know if this answers your question. 如果这能回答您的问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM