linux telnet 重定向,一种将程序的标准输出重定向到telnet终端的方法

#include int stdio_redirect_daemon(char * tty)

{

int ret = -1;

if(tty==NULL)

{

printf("print_redirection,parameter is null.\n");

return -1;

}

close(1);

close(2);

ret = (int)open(tty, 1);

if(ret<0)

{

printf("redirect stdout to tty error:%s.\n",strerror(errno));

return ret;

}

ret = (int)open(tty, 2);

if(ret<0)

{

printf("redirect stderr to tty error:%s.\n",strerror(errno));

return ret;

}

fflush(NULL);

return 0;

}

int main()

{

char tty[32]={0};

//从客户端指定的文件中(/tmp/cttyname)读取

int fd=open("/tmp/cttyname",O_RDWR);

read(fd,tty,32);

while(1)

{

if(access("/tmp/cttyflag",F_OK)<0)

{

;

}

else

{

stdio_redirect_daemon(tty);

}

sleep(1);

}

return 0;

}

===============================================================

ctty客户端

int main()

{

char *tty=ttyname(1);

char cmd[64]={0};

strcpy(cmd,"echo ");

strcat(cmd,tty);

strcat(cmd, " /tmp/cttyname");

system(cmd);

system("touch /tmp/cttyflag");

while(1)

{

sleep(1);

}

return 0;

}

服务端需要嵌入到应用程序中。客户端单独编译后部署到设备中,当登陆到设备后,运行客户端,即可实时

查看到当前应用程序在标准终端的输出。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值