Sunday, February 12, 2012

Remote command execution server and client program


Remote command execution

server

ser.c
#include<unistd.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<sys/socket.h>
main()
{
        int sd,nsd,i=0,port=5160;
        unsigned int t;
        char content[30];
        struct sockaddr_in ser,cli;
        if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
        {
        printf("Error:Socket creation in Server\n");
        return 0;
        }
        bzero((char*)&ser,sizeof(ser));
        printf("The port address is %d \n",port);
        ser.sin_family=AF_INET;
        ser.sin_port=htons(port);
        ser.sin_addr.s_addr=inet_addr("192.168.0.200");
        if(bind(sd,(struct sockaddr*)&ser,sizeof(ser))==-1)
        {
        printf("Error: Binding problem port busy");
        return 0;
        }
        listen(sd,1);
        t=sizeof(cli);
        nsd=accept(sd,(struct sockaddr*)&cli,&t);
                        while(1)
        {
                        recv(nsd,content,15,0);
                        system(content);
        if(strcmp(content,"exit")==0)
           break;
       }
}
[cs2910007@localhost netlab]$ 



Server o/p

[cs2910007@localhost netlab]$ cc remoteser.c
[cs2910007@localhost netlab]$ ./a.out
The port address is 5160 
Fri Aug 19 11:43:42 IST 2011
2q                         chatserver.c  ex2b.c                 ex3s.c       udpcserchat.c
a.out                   echocli.c     ex2c.c                    ex3server.c                  udpserchat.c
chatclient1.c  echoclient.c  ex3.c                    remotecli.c                  udpserv.c
chatclient2.c  echoser.c     ex3c1.c                 remoteser.c                 udpservchat.c
chatclient.c   echoserver.c  ex3c.c                  udpclichat.c                 udpserver.c
chatserver1.c  ex2a        ex3client.c  udpclie.c
chatserver2.c  ex2a.c                            ex3s1.c                         udpclient.c
[cs2910007@localhost netlab]$  



client :

cli.c
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<sys/socket.h>
main()
{
        int sd,i=0,port=5160;
        char content[30]="\0";
        char cont[30]="\0";
        struct sockaddr_in ser,cli;
        if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
        {
                printf("Error:Socket creation in server\n");
                return 0;
        }
        bzero((char *)&ser,sizeof(ser));
        printf("The port address is %d \n",port);
        ser.sin_family=AF_INET;
        ser.sin_port=htons(port);
        ser.sin_addr.s_addr=inet_addr("192.168.0.200");
        if(connect(sd,(struct sockaddr *)&ser,sizeof(ser))==-1)
        {
                printf("Error in connecting port busy");
                return 0;
        }
        while(1)
        {
                        scanf("%s",&content);
        send(sd,&content[i],15,0);
        if(strcmp(content,"exit")==0)
        { 
         break;
        }
        }
        close(sd);
        return 0;
}



client :o/p:

[cs2910007@localhost netlab]$ cc remotecli.c
[cs2910007@localhost netlab]$ ./a.out
The port address is 5160 
date
ls
exit
[cs2910007@localhost netlab]$

No comments:

Post a Comment