linux c语言写个telnet自动输入密码?(关于pipe、重定向疑问)

停止抱怨 2015-10-13 04:02:14
我需要用c语言写个telnet自动连接并执行一些命令。思路:利用execlp()来执行 telnet 192.168.xx.xx 。利用pipe函数、重定向来输入密码和命令(交互)。不过重定向后,execlp()执行了,但是输出看不到。密码输入管道也看不到,我不知道是不是成功登陆了。怎么处理?
我用fput输入密码和一句重启命令,,,,但是并没重启
...全文
579 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
dongshijian100 2017-03-14
  • 打赏
  • 举报
回复 1
你的是怎么解决的?我怎么在telnet链接上了之后执行的指令输出打印不出来。手动链接时回出现的账号密码提示行也打印不出来,退出提示倒是能打印。
赵4老师 2016-01-07
  • 打赏
  • 举报
回复
向12楼学习!
停止抱怨 2016-01-07
  • 打赏
  • 举报
回复
代码就是12楼这样。。。显示不了我认为是因为重定向改变了输出位置,所以没输出到屏幕。
hzfllz 2015-10-28
  • 打赏
  • 举报
回复
lz可以共享下你代码,在写个c的telnet自动登录
sand6174 2015-10-15
  • 打赏
  • 举报
回复
引用 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 改改也能用
sand6174 2015-10-15
  • 打赏
  • 举报
回复
有一段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; "
qldsrx 2015-10-15
  • 打赏
  • 举报
回复
自定义函数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;
}
赵4老师 2015-10-14
  • 打赏
  • 举报
回复
我的代码是Windows上用的。
Linux下没搞过pipe,坐等楼下大神现身。
停止抱怨 2015-10-14
  • 打赏
  • 举报
回复
谢谢各位帮忙。。。已经解决。利用抓包软件看到现象。(代码和思路都是对的)因为无法看到输出。可以fgets来读取后输出
停止抱怨 2015-10-13
  • 打赏
  • 举报
回复
急阿,有人知道怎么处理吗
停止抱怨 2015-10-13
  • 打赏
  • 举报
回复
谢谢楼上的大神,新手表示看不懂。好像不是我想要的
赵4老师 2015-10-13
  • 打赏
  • 举报
回复
仅供参考:
#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;
}
停止抱怨 2015-10-13
  • 打赏
  • 举报
回复
@killer1978 大神,搜到11年您发的帖子。。。写过用无阻塞2个piple结合fock实现了进程间双向管道调用telnet,再自动输入用户名,密码的代码。
请问我遇到的问题您怎么解决的?能否给份源码?
jiht594 2015-10-13
  • 打赏
  • 举报
回复
我知道python比较好弄
不是一定用c的话, 可以试试
停止抱怨 2015-10-13
  • 打赏
  • 举报
回复
急急急!

70,022

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧