Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 通过popen与telnet交谈,不需要';不会失败,但不会失败';I don’我没有表现得像预期的那样_C_Shell_Pipe_Telnet_Io Redirection - Fatal编程技术网

C 通过popen与telnet交谈,不需要';不会失败,但不会失败';I don’我没有表现得像预期的那样

C 通过popen与telnet交谈,不需要';不会失败,但不会失败';I don’我没有表现得像预期的那样,c,shell,pipe,telnet,io-redirection,C,Shell,Pipe,Telnet,Io Redirection,我想在user@tst3:23并与popen(“telnet tst3 23”,“w”)交谈(我对程序中的输出不感兴趣,但我会将其记录在一些文件中) 我首先尝试使用telnet的命令行执行此操作,结果成功了: root@b385fb38298f:/tmp# telnet tst3 23 Trying 172.18.0.2... Connected to tst3. Escape character is '^]'. ea719d50c59a login: user Pa

我想在
user@tst3:23
并与popen(“telnet tst3 23”,“w”)交谈(我对程序中的输出不感兴趣,但我会将其记录在一些文件中)

我首先尝试使用telnet的命令行执行此操作,结果成功了:

root@b385fb38298f:/tmp# telnet tst3 23
Trying 172.18.0.2...
Connected to tst3.
Escape character is '^]'.

ea719d50c59a login: user            
Password: 
Welcome to Alpine!

The Alpine Wiki contains a large amount of how-to guides and general
information about administrating Alpine systems.
See <http://wiki.alpinelinux.org/>.

You can setup the system with the command: setup-alpine

You may change this message by editing /etc/motd.

ea719d50c59a:~$ ls -l
total 0
ea719d50c59a:~$ exit
Connection closed by foreign host.
root@b385fb38298f:/tmp# 

为什么失败?

您需要在每次
fprintf()
后使用
fflush(telnet)
或关闭输出缓冲。并在发送密码之前等待密码提示。另一端的程序可能会在读取前清除其输入缓冲区。@spectras这大概就是
usleep()
调用的目的。他无法直接读取响应。@Barmar是的,这就是usleep()的原因。但是您仍然需要刷新缓冲区,否则直到最后才会发送任何内容。您需要在每次
fprintf()
之后使用
fflush(telnet)
,或者关闭输出缓冲。在发送密码之前等待密码提示。另一端的程序可能会在读取前清除其输入缓冲区。@spectras这大概就是
usleep()
调用的目的。他无法直接读取响应。@Barmar是的,这就是usleep()s的原因。但是您仍然需要刷新缓冲区,否则直到最后才会发送任何内容。
root@b385fb38298f:/tmp# cat | telnet tst3 23 1> log.out 2> log.err
user

ls -l
exit
root@b385fb38298f:/tmp# cat log.out 
Trying 172.18.0.2...
Connected to tst3.
Escape character is '^]'.

ea719d50c59a login: user
Password: 
Welcome to Alpine!

The Alpine Wiki contains a large amount of how-to guides and general
information about administrating Alpine systems.
See <http://wiki.alpinelinux.org/>.

You can setup the system with the command: setup-alpine

You may change this message by editing /etc/motd.

ea719d50c59a:~$ ls -l
total 0
ea719d50c59a:~$ exit
root@b385fb38298f:/tmp# ;17R;17R
bash: syntax error near unexpected token `;'
root@b385fb38298f:/tmp# cat log.err 
Connection closed by foreign host.
root@b385fb38298f:/tmp# 
root@b385fb38298f:/tmp# cat > test.c
#include <errno.h>
#include <stdio.h>

#include <unistd.h>

#include <error.h>


int main(void)
{
    FILE    *telnet;

    telnet  = popen("telnet tst3 23 1> log2.out 2> log2.err", "w");
    if (!telnet)
        error(1, errno, "error1");
    usleep(1000000);
    printf("a\n");

    if (fprintf(telnet, "user\n") <= 0)
        error(1, errno, "error2");
    usleep(1000000);
    printf("b\n");

    if (fprintf(telnet, "\n") <= 0)
        error(1, errno, "error3");
    usleep(1000000);
    printf("c\n");

    if (fprintf(telnet, "exe\n") <= 0)
        error(1, errno, "error4");
    usleep(1000000);
    printf("d\n");

    pclose(telnet);

    return  0;
}
root@b385fb38298f:/tmp# gcc test.c -o test             
root@b385fb38298f:/tmp# ./test 
a
b
c
d
root@b385fb38298f:/tmp# cat log2.out 
Trying 172.18.0.2...
Connected to tst3.
Escape character is '^]'.

ea719d50c59a login: root@b385fb38298f:/tmp# cat log2.err 
Connection closed by foreign host.
root@b385fb38298f:/tmp#