Sunday, December 29, 2019

REMOTE COMMAND EXECUTION USING SOCKET SYSTEM CALL...*NP LAB*

vinaychinnu3213@gmail.com

 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;
}
}
}
SERVER: OUTPUT 
Enter the port number  :  1027
Command received :
mkdir: cannot create directory `vinay': File exists
Command received : mkdir vinay
cs302    pts/75       Dec 30 09:48 (192.168.1.200)
Command received : who am i
\      client     getname.c    getpname2.c  getsetsys.c  remcotecom1.c  server.c  tcp.c   tcpser.c  udpclient.c  udpserver.c
a.out  client.c  getpeername.c    getpname.c   getsocser.c  remotecom.c     syscal.c  tcpc.c  udpc.c    udps.c      vinay
Command received : ls
Mon Dec 30 10:39:43 IST 2019
Command received : date
    December 2019
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 : cal
What manual page do you want?
Command received : man
Command received : mkdir kum
\      client     getname.c    getpname2.c  getsetsys.c  kum         remotecom.c  syscal.c    tcpc.c      udpc.c       udps.c        vinay
a.out  client.c  getpeername.c    getpname.c   getsocser.c  remcotecom1.c  server.c     tcp.c    tcpser.c  udpclient.c  udpserver.c
Command received : ls

CLIENT:OUTPUT
Enter the port number  :  1027
Enter the command:Enter the command:mkdir vinay
Enter the command:who am i
Enter the command:ls   
Enter the command:date
Enter the command:cal
Enter the command:man
Enter the command:mkdir kum
Enter the command:ls