zoukankan      html  css  js  c++  java
  • 伪终端实现sftp远程登录

    没用的不说了,直接上代码。

    # include <stdio.h>
    # include <stdlib.h>
    # include <string.h>
    # include <unistd.h>
    # include <sys/types.h>
    # include <linux/limits.h>
    # include <pty.h>
    #include <utmp.h>
    int pty, slave;
    void do_passwd( int pty);
    int main( int argc, char *argv[] )
    {

    char pty_name[PATH_MAX];
    int ret;
    pid_t child;

    ret = openpty( &pty, &slave, pty_name, NULL, NULL );
    if ( ret == -1 )
    {
    perror( "openpty" );
    exit( EXIT_FAILURE );
    }
    puts(pty_name);
    child = fork();
    if ( child == -1 )
    {
    perror( "fork" );
    exit( EXIT_FAILURE );
    }
    else if ( child == 0 )
    {
    //close( pty );
    login_tty(slave);
    execl( "/usr/sbin/sftp", "sftp","root@192.168.1.47", NULL );
    }
    //close( slave );
    printf( "pty name: %s ", pty_name );
    do_passwd(pty);
    exit( EXIT_SUCCESS );
    }
    void do_passwd( int pty)
    {
    char buffer[80];
    int ret;
    fd_set reads;
    char *buf="password";

    memset(buf,0,10);
    while(1)
    {
    FD_ZERO( &reads );
    FD_SET( pty, &reads );
    ret = select( pty+1, &reads, NULL, NULL, NULL );
    if ( ret == -1 )
    {
    perror( "select" );
    break;
    }
    ret = read( pty, buffer, sizeof(buffer) );
    if ( ret <= 0 )
    {
    break;
    }
    write( fileno(stdout), buffer, ret );
    fflush( stdout );
    if ( buffer[ret-1] == ' ' )
    {
    continue;
    }
    if(flag==1)
    {
    ret=write(pty,buf, strlen(buf));
    char buff=0x0d; //换行
    write(pty,&buff,1);
    write(fileno(stdout),"taihu123 ",strlen("taihu123 "));
    sleep(2);
    flag=0;
    continue;
    }
    if(flag==0)
    {
    char buff=0x0d;
    write(pty,"put /pic/pic.jpg /data/20130729.jpg",strlen("put /pic/pic.jpg /data3/20130729.jpg"));
    write(pty,&buff,1);
    write(fileno(stdout),"put /pic/pic.jpg /data/20130729.jpg",strlen("put /pic/pic.jpg /data3/20130729.jpg"));
    sleep(2);
    flag=2;
    }

    if(flag==2)break;
    }
    return;
    }

    gcc -lutil -o  *.c

    互相学习

  • 相关阅读:
    java解析xml
    支持向量机SVM
    资源-菜单
    GIT
    基于OpenCV的图书扫描识别程序开发
    最大公约数(gcd)还有最小公倍数(lcm)的共通之处
    python网页分析
    python爬虫的基本知识储备
    大数加法 (A + B Problem II)
    Andy's First Dictionary (set)
  • 原文地址:https://www.cnblogs.com/linux-huYZ/p/3224955.html
Copyright © 2011-2022 走看看