popen telnet 中的子命令
我正在尝试在 C++ 中建立本地 telnet 会话并发送命令/接收数据。现在我有:
const char *cmd = "telnet 127.0.0.1 2006";
char buffer[256];
FILE *pipe = popen(cmd, "rw");
//if( !pipe ) { perror("popen"); exit(-1); }
while( fgets(buffer, sizeof(buffer), pipe) != NULL &&
!feof(pipe) )
{
if( ferror(pipe) ) { perror("fgets"); break; }
/* Here you do whatever you want with the data. */
printf("%s", buffer);
}
pclose(pipe);
正在打开 telnet 连接。我需要发送一个命令,例如:“/neighbors”,然后接收它返回的数据。理想情况下,会话将保持打开状态,并且我会每隔 20 秒左右重新查询“/neighbors”。
我认为我需要使用 fork() 创建一个子进程,但我对这个过程非常陌生。
I am trying to establish a local telnet session in C++ and send commands/receive data. Right now I have:
const char *cmd = "telnet 127.0.0.1 2006";
char buffer[256];
FILE *pipe = popen(cmd, "rw");
//if( !pipe ) { perror("popen"); exit(-1); }
while( fgets(buffer, sizeof(buffer), pipe) != NULL &&
!feof(pipe) )
{
if( ferror(pipe) ) { perror("fgets"); break; }
/* Here you do whatever you want with the data. */
printf("%s", buffer);
}
pclose(pipe);
Which is opening the telnet connection. I need to send a command like: "/neighbors" and then receive the data it would return. Ideally, the session would remain open and I would re-query "/neighbors" every 20 seconds or so.
I think I need to create a child process with fork(), but I am very new to this process.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 telnet 似乎是一种相当迂回的方法。您是否考虑过使用常规套接字与远程进程通信?例如,尝试一下套接字编程指南。
Using
telnet
seems like a rather roundabout way to do this. Have you considered using regular sockets to talk to the remote process? For example, try this guide to socket programming.