-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPID_client.c
80 lines (78 loc) · 2.8 KB
/
PID_client.c
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*************************************************************************
> File Name: my_client.c
> Author: wangyanquan
> Mail:
> Created Time: 2018年03月07日 星期三 08时57分26秒
************************************************************************/
#include<stdio.h>
#include "my.h"
//#define host "192.168.0.114"
//#define port 6666
int main() {
int PID;
int count = 0;
int sockfd;
for (int i = 0; i < 3; i++) {
count++;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
printf("count = %d, sockfd = %d\n", count, sockfd);
PID = fork();
if (PID == 0) break;
}
int port = 6666;
char host[] = "192.168.1.82";
struct sockaddr_in dest_addr;
memset(&dest_addr, 0, sizeof (dest_addr));
dest_addr.sin_family = AF_INET;
dest_addr.sin_addr.s_addr = inet_addr(host);
dest_addr.sin_port = htons(port);
if (PID != 0)
return 0;
else {
while(1) {
if (count == 1) {
printf("count = %d, sockfd = %d\n", count, sockfd);
connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
sleep(5);
sleep(0.3);
FILE *f = popen("ls", "r");
char *buffer = calloc(1000, sizeof(char));
while (fgets(buffer, 1000, f)) {
send(sockfd, buffer, strlen(buffer), 0);
// printf("buffer = %s\n", buffer);
memset(buffer, 0, sizeof(buffer));
}
pclose(f);
}
else if (count == 2) {
printf("count = %d, sockfd = %d\n", count, sockfd);
connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
sleep(5);
sleep(0.6);
FILE *f = popen("uname -a", "r");
char *buffer = calloc(1000, sizeof(char));
while (fgets(buffer, 1000, f)) {
send(sockfd, buffer, strlen(buffer), 0);
// printf("buffer = %s\n", buffer);
memset(buffer, 0, sizeof(buffer));
}
pclose(f);
}
else if (count == 3) {
printf("count = %d, sockfd = %d\n", count, sockfd);
connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
sleep(5);
sleep(0.9);
FILE *f = popen("pwd", "r");
char *buffer = calloc(1000, sizeof(char));
while (fgets(buffer, 1000, f)) {
send(sockfd, buffer, strlen(buffer), 0);
// printf("buffer = %s\n", buffer);
memset(buffer, 0, sizeof(buffer));
}
pclose(f);
}
}
}
return 0;
}