• linux 下远程执行命令 socket实现


    服务端:

    /*************************************************************************
            > File Name: srv.c
            > Author: zhoulin
            > Mail: 715169549@163.com 
            > Created Time: Sat 02 Jan 2016 04:39:53 PM EST
     ************************************************************************/
    
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #define  size 4096
    #define  port 6000
    #define  backlog  5
    int main(void) {
        int sk;
        int rsk;
        struct sockaddr_in server;
        struct sockaddr_in client;
        char send_buf[size];
        char recv_buf[size];
        
        //open a socket
        sk= socket(AF_INET,SOCK_STREAM,0); //建立socket
        if(sk == -1) {
            perror("socket");
            return -1;
        }
        
        //seting server addr
        memset((char *)&server,0,sizeof(server));
        server.sin_family = AF_INET;
        server.sin_addr.s_addr = htonl(INADDR_ANY);
        server.sin_port =  port;
    
        if(bind(sk, (struct sockaddr *)&server, sizeof(server)) == -1) { //绑定到指定的port上
            perror("bind");
            return -1;
        }
        if(listen(sk, backlog) == -1) { //监听服务端的端口
            perror("listen");
            return -1;
        }
        int len=sizeof(client);
        printf("****************listen socket****************
    ");
        while(1) {
            if((rsk = accept(sk,(struct sockaddr *)&client, &len)) < 0){ //循环监听
                perror("accept");
                return -1;
            }
            fprintf(stdout,"Conencted from %s
    ", inet_ntoa(client.sin_addr)); //输出连接到服务端的IP地址
            printf("TCP>");
            memset(send_buf,'',size);
            fgets(send_buf, size, stdin); //输入命令
            size_t slen=send(rsk, send_buf, strlen(send_buf), 0); //发送指正字节到客户端
            size_t rlen=recv(rsk, recv_buf, size, 0); //接受来自客户端的信息
            if(strncmp(recv_buf,"quit",4) == 0){  //如果收到的是是quit,则退出
                break;
            }
            if(rlen > 0 ) { //重置接受信息的缓冲区
                fprintf(stdout,"%s",recv_buf);
                memset(recv_buf,'',size);
            }
        }
        return 0;
    }

    客户端 代码如下

    /*************************************************************************
            > File Name: cli.c
            > Author: zhoulin
            > Mail: 715169549@163.com 
            > Created Time: Sat 02 Jan 2016 06:37:32 PM EST
     ************************************************************************/
    #include <stdio.h>
    #include <unistd.h>
    #include <time.h>
    #include <string.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #define size 4096
    #define port 6000
    #define sleep_time 1
    int exec_command() {
        int exec_flag=0;
        char log[2048]={''};
        time_t c_now;
        struct tm *c_tm_now;
        time(&c_now);
        c_tm_now=localtime(&c_now);
        char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
        sprintf(log,"[%d-%d-%d %s %d:%d:%d] : ", (1900+c_tm_now->tm_year), (1+c_tm_now->tm_mon),
                c_tm_now->tm_mday,wday[c_tm_now->tm_wday], c_tm_now->tm_hour, c_tm_now->tm_min, c_tm_now->tm_sec);
        char send_buf[size];
        char recv_buf[size];
        int skc = socket(AF_INET,SOCK_STREAM,0);
        if(skc == -1){
            perror("socket");
            exec_flag=-1;
            return exec_flag;
        }
        struct sockaddr_in saddr;
        memset(&saddr,0,sizeof(saddr));
        saddr.sin_family = AF_INET;
        saddr.sin_port = port;
        saddr.sin_addr.s_addr = inet_addr("192.168.100.10");
    
        //connect a socket
        if(connect(skc,  (struct sockaddr *)&saddr, sizeof(saddr)) <0) {
            perror("conenct");
            exec_flag=-1;
            return exec_flag;
        }
        FILE *fp = NULL;
        size_t len = recv(skc,recv_buf,sizeof(recv_buf), 0);
        if(len > 0) {
            if(strncmp(recv_buf,"quit",4) == 0) {
                strncpy(recv_buf,"quit",4);
                memset(send_buf,'',size);
                strncpy(send_buf,"quit",4);
                send(skc,send_buf,size,0);
                exec_flag=-1;
                return exec_flag;
            }
            recv_buf[len] = '';
            fp = popen(recv_buf, "r");
            memset(send_buf, 0, size);
            int rsize=fread(send_buf,1,size,fp);
            if(rsize <= 0) {
                strncpy(send_buf,"******execute command fail **********
    ",size);
            }
            send(skc,send_buf,size,0);
            strncpy(log+strlen(log),recv_buf,strlen(recv_buf));
            printf("%s",log);
            memset(recv_buf,'',size);
            pclose(fp);
            exec_flag = 0;
        }
        return exec_flag;
    }
    int main(void) {
        while(1) {
            if(exec_command() == -1){
                break;
            }
            sleep(sleep_time);
        }
    }

    运行结果 :

      服务端的输出:

    ****************listen socket****************
    Conencted from 192.168.100.20
    TCP>pwd
    /home/zhoulin/socket
    Conencted from 192.168.100.20
    TCP>ls -l -a
    total 64
    drwxrwxr-x. 2 zhoulin zhoulin  4096 Jan  3 15:38 .
    drwx------. 4 zhoulin zhoulin  4096 Jan  3 15:38 ..
    -rw-rw-r--. 1 zhoulin zhoulin     0 Jan  2 22:48 1
    -rw-rw-r--. 1 zhoulin zhoulin     0 Jan  2 22:49 1.log
    -rwxrwxr-x. 1 zhoulin zhoulin  9839 Jan  3 15:38 cli
    -rw-rw-r--. 1 zhoulin zhoulin  2494 Jan  2 22:59 cli.c
    -rw-rw-r--. 1 zhoulin zhoulin 13483 Jan  2 16:12 sp
    -rw-rw-r--. 1 zhoulin zhoulin  2602 Jan  2 16:12 spair.c
    -rw-rw-r--. 1 zhoulin zhoulin 13546 Jan  2 22:54 srv
    -rw-rw-r--. 1 zhoulin zhoulin  1930 Jan  2 22:47 srv.c
    Conencted from 192.168.100.20
    TCP>du /
    1512    /usr/lib64/nss/unsupported-tools
    4       /usr/lib64/nss/saved
    2216    /usr/lib64/nss
    68      /usr/lib64/pm-utils/sleep.d
    16      /usr/lib64/pm-utils/module.d
    32      /usr/lib64/pm-utils/bin
    8       /usr/lib64/pm-utils/power.d
    148     /usr/lib64/pm-utils
    140     /usr/lib64/libuser
    248     /usr/lib64/pango/1.6.0/modules
    252     /usr/lib64/pango/1.6.0
    256     /usr/lib64/pango
    76      /usr/lib64/traceevent/plugins
    80      /usr/lib64/traceevent
    56      /usr/lib64/gettext
    10276   /usr/lib64/atlas
    312     /usr/lib64/ibdiagnet1.5.7
    108     /usr/lib64/pygtk/2.0/demos/images
    588     /usr/lib64/pygtk/2.0/demos
    616     /usr/lib64/pygtk/2.0
    620     /usr/lib64/pygtk
    5008    /usr/lib64/ibdm1.5.7/ibnl
    5684    /usr/lib64/ibdm1.5.7
    8       /usr/lib64/tk8.5
    12      /usr/lib64/p11-kit
    48      /usr/lib64/libnfsidmap
    704     /usr/lib64/pkgconfig
    88      /usr/lib64/libfakeroot
    200     /usr/lib64/ldb/modules/ldb
    204     /usr/lib64/ldb/modules
    208     /usr/lib64/ldb
    392     /usr/lib64/ibdiagui1.5.7
    16      /usr/lib64/apr-util-1
    52      /usr/lib64/sssd/modules
    2076    /usr/lib64/sssd
    820     /usr/lib64/evolution-data-server-1.2/camel-providers
    1020    /usr/lib64/evolution-data-server-1.2/extensions
    1844    /usr/lib64/evolution-data-server-1.2
    36      /usr/lib64/bonobo-2.0/samples
    40      /usr/lib64/bonobo-2.0
    1512    /usr/lib64/graphviz/tcl
    2248    /usr/lib64/graphviz
    4       /usr/lib64/tcl8.5
    60      /usr/lib64/perl5/Time
    268     /usr/lib64/perl5/B
    100     /usr/lib64/perl5/HTML
    12      /usr/lib64/perl5/DBI/Util
    84      /usr/lib64/perl5/DBI/Const/GetInfo
    96      /usr/lib64/perl5/DBI/Const
    20      /usr/lib64/perl5/DBI/Gofer/Transport
    16      /usr/lib64/perl5/DBI/Gofer/Serializer
    88      /usr/lib64/perl5/DBI/Gofer
    20      /usr/lib64/perl5/DBI/DBD
    28      /usr/lib64/perl5/DBI/SQL
    12      /usr/lib64/perl5/DBI/ProfileDumper
    664     /usr/lib64/perl5/DBI
    60      /usr/lib64/perl5/Compress/Raw
    108     /usr/lib64/perl5/Compress
    24      /usr/lib64/perl5/IO/Socket
    8       /usr/lib64/perl5/IO/Compress/Zip
    8       /usr/lib64/perl5/IO/Compress/Gzip
    16      /usr/lib64/perl5/IO/Compress/Zlib
    16      /usr/lib64/perl5/IO/Compress/Adapter
    28      /usr/lib64/perl5/IO/Compress/Base
    248     /usr/lib64/perl5/IO/Compress
    16      /usr/lib64/perl5/IO/Uncompress/Adapter
    272     /usr/lib64/perl5/IO/Uncompress
    628     /usr/lib64/perl5/IO
    12      /usr/lib64/perl5/gnu
    20      /usr/lib64/perl5/Filter/Util
    24      /usr/lib64/perl5/Filter
    12      /usr/lib64/perl5/Text
    24      /usr/lib64/perl5/threads
    36      /usr/lib64/perl5/Digest
    36      /usr/lib64/perl5/IPC
    4       /usr/lib64/perl5/machine
    8       /usr/lib64/perl5/DBD/SQLite
    24      /usr/lib64/perl5/DBD/Gofer/Policy
    44      /usr/lib64/perl5/DBD/Gofer/Transport
    72      /usr/lib64/perl5/DBD/Gofer
    308     /usr/lib64/perl5/DBD
    84      /usr/lib64/perl5/sys
    12      /usr/lib64/perl5/I18N
    36      /usr/lib64/perl5/Hash/Util
    56      /usr/lib64/perl5/Hash
    228     /usr/lib64/perl5/Devel
    3480    /usr/lib64/perl5/CORE
    100     /usr/lib64/perl5/asm
    4       /usr/lib64/perl5/5.10.0/x86_64-linux-thread-multi/CORE
    8       /usr/lib64/perl5/5.10.0/x86_64-linux-thread-multi
    12      /usr/lib64/perl5/5.10.0
    20      /usr/lib64/perl5/PerlIO
    44      /usr/lib64/perl5/Data
    16      /usr/lib64/perl5/linux
    20      /usr/lib64/perl5/Unicode
    20      /usr/lib64/perl5/Encode/JP
    12      /usr/lib64/perl5/Encode/CN
    8       /usr/lib64/perl5/Encode/KR
    8       /usr/lib64/perl5/Encode/Unicode
    8       /usr/lib64/perl5/Encode/MIME/Header
    24      /usr/lib64/perl5/Encode/MIME
    184     /usr/lib64/perl5/Encode
    160     /usr/lib64/perl5/bits
    8       /usr/lib64/perl5/Scalar/Util
    20      /usr/lib64/perl5/Scalar
    40      /usr/lib64/perl5/asm-generic
    36      /usr/lib64/perl5/File
    8       /usr/lib64/perl5/Bundle
    8       /usr/lib64/perl5/Math/BigInt
    12      /usr/lib64/perl5/Math
    24      /usr/lib64/perl5/XML/Parser/Style
    300     /usr/lib64/perl5/XML/Parser/Encodings
    368     /usr/lib64/perl5/XML/Parser
    400     /usr/lib64/perl5/XML
    12      /usr/lib64/perl5/List/Util
    24      /usr/lib64/perl5/List
    16      /usr/lib64/perl5/MIME
    24      /usr/lib64/perl5/vendor_perl/Crypt/SSLeay
    44      /usr/lib64/perl5/vendor_perl/Crypt
    20      /usr/lib64/perl5/vendor_perl/Net
    44      /usr/lib64/perl5/vendor_perl/auto/Crypt/SSLeay
    48      /usr/lib64/perl5/vendor_perl/auto/Crypt
    52      /usr/lib64/perl5/vendor_perl/auto
    120     /usr/lib64/perl5/vendor_perl
    52      /usr/lib64/perl5/Sys
    24      /usr/lib64/perl5/auto/Time/Piece
    32      /usr/lib64/perl5/auto/Time/HiRes
    60      /usr/lib64/perl5/auto/Time
    152     /usr/lib64/perl5/auto/B
    20      /usr/lib64/perl5/auto/DynaLoader
    36      /usr/lib64/perl5/auto/NDBM_File
    44      /usr/lib64/perl5/auto/GDBM_File
    68      /usr/lib64/perl5/auto/DB_File
    20      /usr/lib64/perl5/auto/Cwd
    5du /
    Conencted from 192.168.100.20
    TCP>qui

    客户端的执行日志:

    zhoulin@:~/socket:./cli
    [2016-1-3 Sun 15:38:38] : pwd
    [2016-1-3 Sun 15:38:43] : ls -l -a
    [2016-1-3 Sun 15:39:11] : du /
  • 相关阅读:
    Delphi XE2 之 FireMonkey 入门(36) 控件基础: TForm
    Delphi XE2 之 FireMonkey 入门(35) 控件基础: TFmxObject: 其它
    Delphi XE2 之 FireMonkey 入门(39) 控件基础: TScrollBox、TVertScrollBox、TFramedScrollBox、TFramedVertScrollBox
    人月神话之编程行业的乐趣与苦恼
    基于NHibernate的三层结构应用程序开发初步
    .NET设计模式(9):桥接模式(Bridge Pattern)
    Grove,.NET中的又一个ORM实现
    近期学习计划
    .NET设计模式(8):适配器模式(Adapter Pattern)
    [声明]关于春节回家期间不能更新Blog的说明
  • 原文地址:https://www.cnblogs.com/innobase/p/5097097.html
Copyright © 2020-2023  润新知