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
Assuming that your machine and "sunblaze" are within a firewalled off, reasonably secure environment, I would use "ssh-keygen" and "ssh-copy-id root@sunblaze" to allow your user ID to log in to sunblaze without a password. That way, you don't have a password in your code that someone can look at.
Yes, I know, this wasn't what you were asking...
I don't actually see why fgets(str, size, fd);
- I will have a little play to figure out...
This definitely works:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tcl8.5/expect.h>
#include <errno.h>
int main()
{
char str[512];
FILE *f = exp_popen("ssh user@mybox ls -lR");
if (f==NULL)
{
printf("Failed (%s)\n", strerror(errno));
return 1;
}
while(fgets(str, sizeof(str)-1, f))
{
printf("%s", str);
}
return 0;
}
However, if the output from ssh doesn't have a newline, obviously, fgets() won't complete. When I first tried, it got stuck on a password question - but when I changed it to a machine that I can log in to without a password, it worked fine.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.