user-0 提问: linux c语言写个telnet自动输入密码？（关于pipe、重定向疑问）

回复:
user: user-1
content: 你的是怎么解决的？我怎么在telnet链接上了之后执行的指令输出打印不出来。手动链接时回出现的账号密码提示行也打印不出来，退出提示倒是能打印。

user: user-2
content: 向12楼学习！

user: user-0
content: 代码就是12楼这样。。。显示不了我认为是因为重定向改变了输出位置，所以没输出到屏幕。

user: user-3
content: lz可以共享下你代码，在写个c的telnet自动登录

user: user-4
content: 引用 10 楼 sand6174 的回复:有一段shell 脚本 ssh 自动输入密码的   echo "$0" function Help() {         echo "ip;password;cmd..."         exit 1 }  if [ $# -lt 3 ]; then        Help fi echo "ip: $1" echo "password: $2"  cmd="" i=0 for arg in "$@" do         let i++         if [ $i -le 2 ]; then                 continue         fi         cmd+="$arg " done  echo "cmd: $cmd "  expect -c "set timeout -1;         spawn ssh -l root $1 \"$cmd\";         expect {                 yes/no { send \"yes\r\"; exp_continue }                 *password* { send \"$2\r\"; interact}         };         expect eof;         " telnet 改改也能用

user: user-4
content: 有一段shell 脚本 ssh 自动输入密码的   echo "$0" function Help() {         echo "ip;password;cmd..."         exit 1 }  if [ $# -lt 3 ]; then        Help fi echo "ip: $1" echo "password: $2"  cmd="" i=0 for arg in "$@" do         let i++         if [ $i -le 2 ]; then                 continue         fi         cmd+="$arg " done  echo "cmd: $cmd "  expect -c "set timeout -1;         spawn ssh -l root $1 \"$cmd\";         expect {                 yes/no { send \"yes\r\"; exp_continue }                 *password* { send \"$2\r\"; interact}         };         expect eof;         "

user: user-5
content: 自定义函数rw_popen来打开一个支持读写的进程，用rw_pclose关闭，附带测试代码，已通过测试  pid_t rw_popen(char* cmd, FILE **rfile, FILE **wfile) {     int pipefd[2],pipefd2[2]; //管道描述符     pid_t pid; //进程描述符      if (pipe(pipefd) < 0) //建立管道     {         printf("rw_popen() pipe create error/n");         return 0;     }     if (pipe(pipefd2) < 0) //建立管道     {         printf("rw_popen() pipe create error/n");         return 0;     }      pid = fork(); //建立子进程      if (pid < 0)     return 0;      if (0 == pid) //子进程中     {         close(pipefd[0]);         dup2(pipefd[1], 1);         close(pipefd[1]);         dup2(pipefd2[0], 0);         close(pipefd2[0]);         close(pipefd[1]);         char *argv[] = { "/bin/sh", "-c", cmd, NULL };         if (execvp("/bin/sh", argv) < 0) //用exec族函数执行命令         exit(1);     }      close(pipefd[1]);     *rfile = fdopen(pipefd[0], "r");     close(pipefd2[0]);     *wfile = fdopen(pipefd2[1], "w");     return pid; }  void rw_pclose(pid_t pid, FILE *rfile, FILE *wfile) {     int status;     waitpid(pid, &status, 0);     fclose(rfile);     fclose(wfile); } int main() {     char buf1[1024];     FILE *file1, *file2;     pid_t pid;     pid = rw_popen("sh", &file1, &file2);     if (pid) {         fputs("pwd;exit;\n",file2);         fflush(file2);         if (fgets(buf1, 1400, file1)) {             puts(buf1);         }         rw_pclose(pid, file1, file2);     }     return 1; }

user: user-2
content: 我的代码是Windows上用的。 Linux下没搞过pipe，坐等楼下大神现身。

user: user-0
content: 谢谢各位帮忙。。。已经解决。利用抓包软件看到现象。（代码和思路都是对的）因为无法看到输出。可以fgets来读取后输出

user: user-0
content: 急阿，有人知道怎么处理吗

user: user-0
content: 谢谢楼上的大神，新手表示看不懂。好像不是我想要的

user: user-2
content: 仅供参考：#pragma comment(lib,"user32") #include <stdio.h> #include <windows.h> int main() {     SECURITY_ATTRIBUTES sa          = {0};     STARTUPINFO         si          = {0};     PROCESS_INFORMATION pi          = {0};     HANDLE              hPipeOutputRead  = NULL;     HANDLE              hPipeOutputWrite = NULL;     HANDLE              hPipeInputRead   = NULL;     HANDLE              hPipeInputWrite  = NULL;     BOOL                bTest = 0;     DWORD               dwNumberOfBytesRead = 0;     DWORD               dwNumberOfBytesWrite = 0;     CHAR                szMsg[100];     CHAR                szBuffer[256];      sa.nLength = sizeof(sa);     sa.bInheritHandle = TRUE;     sa.lpSecurityDescriptor = NULL;      // Create pipe for standard output redirection.     CreatePipe(&hPipeOutputRead,  // read handle             &hPipeOutputWrite, // write handle             &sa,      // security attributes             0      // number of bytes reserved for pipe - 0 default             );      // Create pipe for standard input redirection.     CreatePipe(&hPipeInputRead,  // read handle             &hPipeInputWrite, // write handle             &sa,      // security attributes             0      // number of bytes reserved for pipe - 0 default             );      // Make child process use hPipeOutputWrite as standard out,     // and make sure it does not show on screen.     si.cb = sizeof(si);     si.dwFlags     = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;     si.wShowWindow = SW_HIDE;     si.hStdInput   = hPipeInputRead;     si.hStdOutput  = hPipeOutputWrite;     si.hStdError   = hPipeOutputWrite;      CreateProcess (           NULL, "cmd.exe",           NULL, NULL,           TRUE, 0,           NULL, NULL,           &si, &pi);      // Now that handles have been inherited, close it to be safe.     // You don't want to read or write to them accidentally.     CloseHandle(hPipeOutputWrite);     CloseHandle(hPipeInputRead);      // Now test to capture DOS application output by reading     // hPipeOutputRead.  Could also write to DOS application     // standard input by writing to hPipeInputWrite.     sprintf(szMsg, "dir *.txt /b\nexit\n");     WriteFile(           hPipeInputWrite,      // handle of the write end of our pipe           &szMsg,               // address of buffer that send data           18,                   // number of bytes to write           &dwNumberOfBytesWrite,// address of number of bytes read           NULL                  // non-overlapped.           );      while(TRUE)     {        bTest=ReadFile(           hPipeOutputRead,      // handle of the read end of our pipe           &szBuffer,            // address of buffer that receives data           256,                  // number of bytes to read           &dwNumberOfBytesRead, // address of number of bytes read           NULL                  // non-overlapped.           );        if (!bTest){           sprintf(szMsg, "Error #%d reading pipe.",GetLastError());           MessageBox(NULL, szMsg, "WinPipe", MB_OK);           break;       }        // do something with data.       szBuffer[dwNumberOfBytesRead] = 0;  // null terminate       MessageBox(NULL, szBuffer, "WinPipe", MB_OK);     }      // Wait for CONSPAWN to finish.     WaitForSingleObject (pi.hProcess, INFINITE);      // Close all remaining handles     CloseHandle (pi.hProcess);     CloseHandle (hPipeOutputRead);     CloseHandle (hPipeInputWrite);      return 0; }

user: user-0
content: @killer1978 大神，搜到11年您发的帖子。。。写过用无阻塞2个piple结合fock实现了进程间双向管道调用telnet,再自动输入用户名，密码的代码。 请问我遇到的问题您怎么解决的？能否给份源码？

user: user-6
content: 我知道python比较好弄 不是一定用c的话, 可以试试

user: user-0
content: 急急急！