REMOTE COMMAND EXECUTION USING SOCKETS

Server program
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<errno.h>
int main()
{
int sd,acpt,len,bytes,port;
char send[50],receiv[50];
struct sockaddr_in serv,cli;
if((sd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("Error in socket\n");
exit(0);
}
bzero(&serv,sizeof(serv));
printf("Enter the port number  :  ");
scanf("%d",&port);
serv.sin_family=AF_INET;
serv.sin_port=htons(port);
serv.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sd,(struct sockaddr *)&serv,sizeof(serv))<0)
{
printf("Error in bind\n");
exit(0);
}
if(listen(sd,3)<0)
{
printf("Error in listen\n");
exit(0);
}
if((acpt=accept(sd,(struct sockaddr*)NULL,NULL))<0)
{
printf("\n\t Error in accept");
exit(0);
}
while(1)
{

bytes=recv(acpt,receiv,50,0);
receiv[bytes]='\0';
if(strcmp(receiv ,"end")==0)
{
close(acpt);
close(sd);
exit(0);
}
else
{
printf("Command received : %s",receiv);
system(receiv);
printf("\n");
}
}
}

Client program

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<errno.h>
int main()
{
int sd,acpt,len,bytes,port;
char send1[50],receiv[50];
struct sockaddr_in serv,cli;
if((sd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("Error in socket\n");
exit(0);
}
bzero(&serv,sizeof(serv));
printf("Enter the port number  :  ");
scanf("%d",&port);
serv.sin_family=AF_INET;
serv.sin_port=htons(port);
serv.sin_addr.s_addr=htonl(INADDR_ANY);
if(connect(sd,(struct sockaddr *)&serv,sizeof(serv))<0)
{
printf("Error in connection\n");
exit(0);
}

while(1)
{

printf("Enter the command:");
gets(send1);
if(strcmp(send1,"end")!=0)
{
send(sd,send1,50,0);
}
else
{
send(sd,send1,50,0);
close(sd);
break;
}

}
}

OUTPUT

SERVER

[11ca013@mcalinux network]$ cc 8rcpser.c
[11ca013@mcalinux network]$ ./a.out
Enter the port number  :  8596

Command received : ls
1.c               4tcpcli.c     7pingclie.c  exno2a.c               hello.txt      sevan.txt
2.c               4tcpser.c               7pingser.c             exno2client.c        mca1.txt      tcpchclient.c
3tcpcli.c      4udpcli.c              8rcpcli.c      exno2server.c       menu.sh      tcpchserver.c
3tcpser.c     4udpser.c             8rcpser.c    exnola.sh              payroll.sh    third.txt
3udpcli.c      6tcpftpcli.c  9rcpcli.c              febser.sh                         rajiv



Command received : cat sample.txt
rajivgandhi
hi
how are you

Command received : date

Fri Mar  1 11:39:14 IST 2013

Command received : cal
     March 2013
Su     Mo    Tu     We    Th     Fr      Sa
                                                          1        2
          3        4        5        6        7        8        9
10      11      12      13      14      15      16
17      18      19      20      21      22      23
24      25      26      27      28      29      30
31

Command received : end

CLIENT

[11ca013@mcalinux network]$ cc 8rcpcli.c
 [11ca013@mcalinux network]$ ./a.out

Enter the port number  :  1806

Enter the command:Enter the command:ls
Enter the command:cat sample.txt
Enter the command:date
Enter the command:cal

Enter the command:end

3 Response to "REMOTE COMMAND EXECUTION USING SOCKETS"

  1. hashi says:

    client code doesn't executing

    Unknown says:

    when i was eneter my port number i gor an error msg:error in connection

    in client output, enter the command is two times printing

Post a Comment



Your IP Address is:

Browser: