From 5664bba1f343fb2f987d136527ffc742afc76f7e Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 18 Nov 2020 15:43:27 +0530 Subject: add q7: remote command executor --- q7/client.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ q7/server.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ readme.txt | 12 ++++++++++-- 3 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 q7/client.c create mode 100644 q7/server.c diff --git a/q7/client.c b/q7/client.c new file mode 100644 index 0000000..32d89f3 --- /dev/null +++ b/q7/client.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char* argv[]) { + const char *server_name = "127.0.0.1"; + int sockfd, connfd; + struct sockaddr_in servaddr, cli; + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + servaddr.sin_family = AF_INET; + inet_pton(AF_INET, server_name, &servaddr.sin_addr); + + int port = atoi(argv[1]); + servaddr.sin_port = htons(port); + + if((connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) == 0) { + printf("Connected to server!\n"); + fflush(stdout); + } else { + printf("Invalid or busy server address\n"); + fflush(stdout); + exit(0); + }; + + int cont = 0; + char *buf = malloc(1024); + char recevied[1024]; + printf("Client input: "); + fgets(buf, 100, stdin); + while(strcmp(buf, "quit\n") != 0) { + send(sockfd, buf, 100, 0); + if ((cont = recv(sockfd, recevied, 1024, 0)) > 0) { + printf("Server response: %s\n", recevied); + fflush(stdout); + memset(recevied, 0, sizeof(recevied)); + } + printf("Client input: "); + fgets(buf, 100, stdin); + }; + + close(sockfd); +} diff --git a/q7/server.c b/q7/server.c new file mode 100644 index 0000000..12df5bb --- /dev/null +++ b/q7/server.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + int sockfd, connfd; + struct sockaddr_in servaddr, cli; + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + + int port = atoi(argv[1]); + servaddr.sin_port = htons(port); + + bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); + + listen(sockfd, 128); + printf("Server started at %s...\n", inet_ntoa(servaddr.sin_addr)); + fflush(stdout); + + while(1) { + socklen_t len = sizeof(cli); + connfd = accept(sockfd, (struct sockaddr *)&cli, &len); + int i = getpeername(connfd, (struct sockaddr*)&cli, &len); + + printf("Client connected at port %d\n", htons(cli.sin_port)); + int n; + char buf[100]; + int pid; + + if((pid=fork())==0){ + close(sockfd); + while((n = recv(connfd, buf, 100, 0)) > 0) { + printf("Client message: %s\n", buf); + int pipes[2]; + pipe(&pipes[0]); + if ((pid=fork()) == 0) { + dup2(pipes[1], STDOUT_FILENO); + close(pipes[0]); + execl("/bin/sh", "sh", "-c", buf, (char *) NULL); + } else { + close(pipes[1]); + char cmd_output[1024]; + int cmd_size = read(pipes[0], cmd_output, sizeof(cmd_output)); + send(connfd, cmd_output, cmd_size, 0); + wait(NULL); + } + fflush(stdout); + }; + exit(0); + } + close(connfd); + } + return 0; +} diff --git a/readme.txt b/readme.txt index 66b08b0..6aabaaf 100644 --- a/readme.txt +++ b/readme.txt @@ -1,6 +1,5 @@ 18CS54 - network programming and security ------------------------------------------------------------ - Q1: Write a networking program consisting of a client and server components. The client must request for a file by providing a file name, and the server must respond with the @@ -10,8 +9,8 @@ Q1: Write a networking program consisting of a client and A1: https://u.peppe.rs/qt.png ------------------------------------------------------------- +------------------------------------------------------------ Q6: Write a networking program to demonstrate the following: a) Concurrent TCP server b) Iterative TCP server @@ -21,5 +20,14 @@ A6: a) https://u.peppe.rs/E3.png b) https://u.peppe.rs/VA.png c) https://u.peppe.rs/ei.png + +------------------------------------------------------------ +Q7: Write a networking program that recieves a shell command + from a client and returns the output of the command to the + client. + +A7: https://u.peppe.rs/QD.png + + ------------------------------------------------------------ mirror of git.peppe.rs/university/nps -- cgit v1.2.3