如何从linux上使用C ++在libexpect中创建的FILE *读取stdout? [英] How to read stdout from a FILE* created with libexpect in C++ on linux?

查看:654
本文介绍了如何从linux上使用C ++在libexpect中创建的FILE *读取stdout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C ++我使用libexpect创建了一个FILE *:

  FILE * fd = exp_popen(ssh root @ sunblaze) ; 

我使用以下命令行:

  exp_fexpectl(fp,exp_exact,password:,1,exp_end); 

现在bash shell中的其他属性,我想获取文件的内容,
所以我必须运行命令 cat / port1 / port ,并得到所有打印在一个字符缓冲区。
我如何做?



fgets 似乎不起作用... <提前感谢

解决方案

找到了一种方法,在创建连接后方法我是,shell是:

  [root @ sanblaze〜]#



我使用以下命令向其写入命令:

  fputs(echo LinkReset> / port4 / port\r,fp); 
exp_fexpectl(fp,exp_exact,]#,1,exp_end);

使用grep读取文件内容:

  fputs(cat / port4 / port | grep -w Mode\r,fp); 
exp_fexpectl(fp,exp_exact,]#,1,exp_end);在做了上面的exp_buffer之后,

witch是一个全局变量,保存所有来自远程shell从最后一次exp_fexpectl运行,witch只意味着我的命令的输出。剩下的就是解析它。


Using C++ I created a FILE* using libexpect:

FILE* fd = exp_popen("ssh root@sunblaze");

I got to the command line using:

exp_fexpectl(fp , exp_exact , "password: " , 1 , exp_end);

Now the other posses in bash shell and I want to get the contents of a file there, so I have to run the command cat /port1/port and get all it prints in a char buffer. How do I do that?

fgets doesn't seem to work...

Thanks in advance

解决方案

found a way, after creating the connection the way i did, the shell is:

[root@sanblaze ~]#

i write a command to it using for example:

fputs("echo LinkReset > /port4/port\r" , fp);
exp_fexpectl(fp, exp_exact , "]# " , 1 , exp_end);

reading a files content with grep:

fputs("cat /port4/port | grep -w Mode\r" , fp);
exp_fexpectl(fp, exp_exact , "]# " , 1 , exp_end);

after doing the above the "exp_buffer" witch is a global variable holds all the text that came from the remote shell from the last time "exp_fexpectl" ran, witch means only the output of my command. all that's left is to parse it.

这篇关于如何从linux上使用C ++在libexpect中创建的FILE *读取stdout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆