Разговор с тэ lnet с попеном не провалится, но не будет вести себя как положено - PullRequest
1 голос
/ 27 февраля 2020

Я хотел бы открыть соединение с сервером te lnet на user@tst3:23 и поговорить с ним с popen("telnet tst3 23", "w") (меня не интересует вывод в моей программе, но я войду в него некоторые файлы).

Сначала я попытался сделать это с помощью командной строки с te lnet, и это сработало:

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# 

Затем я добавил несколько перенаправлений для имитации программы, говорящей с ней. и это тоже сработало:

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# 

По какой-то причине я получил ;17R;17R в командной строке после cat ing log.out, но все остальное, как и ожидалось.

Но затем я просто реализую ту же структуру перенаправления из C программы (с некоторыми usleep (), чтобы избежать слишком быстрой записи для te lnet), и она не может войти (хотя я не получаю никаких ошибок от труба):

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# 

Почему происходит сбой?

...