hostYP.c
2.2 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define Port 23
#define Board_addr "192.168.2.99"
int sockfd;
typedef struct {
char filename[256];
char result_filename[256];
char sender[30];
char pwd[256];
int pass;
} Info;
main(int argc, char *argv[])
{
if(argc != 3) {
printf("Usage: %s destination_dir sender_ID@sender_IP\n", argv[0]);
exit(1);
}
Info info;
info.pass = 0;
int n, len;
struct sockaddr_in boardAddr;
char board_dir[256];
// info 붙여넣기
strcpy(info.sender, argv[2]);
if(getcwd(info.pwd, 256) == NULL) {
perror("getcwd");
exit(1);
}
while(1) {
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
bzero((char *)&boardAddr, sizeof(boardAddr));
boardAddr.sin_family = PF_INET;
boardAddr.sin_addr.s_addr = inet_addr(Board_addr);
boardAddr.sin_port = htons(Port);
if (connect(sockfd, (struct sockaddr*)&boardAddr, sizeof(boardAddr)) < 0) {
perror("connect");
exit(1);
}
printf("\nsend file name : ");
scanf("%s", &info.filename);
char scp[256] = "scp ";
char root[30] = " root@";
char fname[256];
//scp 명령어 만들기
strcat(scp, info.filename);
strcat(root, Board_addr);
len = strlen(Board_addr);
root[6+len] = ':';
strcat(scp, root);
strcat(scp, argv[1]);
len = strlen(scp);
scp[len] = '\n';
// 결과 파일 이름 만들기
strcpy(fname, info.filename);
char* ptr = strtok(fname, ".");
char result_name[256];
strcpy(result_name, ptr);
char result[256] = "_result.jpg"; // 출력이 jpg로 고정되어있음
strcat(result_name, result);
strcpy(info.result_filename, result_name);
printf("Do : %s\n", scp);
if(system(scp)) { // scp 명령어 실행
perror("system");
exit(1);
}
if(write(sockfd, (char*)&info, sizeof(info)) < 0) { // info 전달
perror("write");
exit(1);
}
printf("\nwait result\n");
if((n = read(sockfd, (char*)&info, sizeof(info))) < 0) {
perror("read");
exit(1);
}
if(info.pass == 1)
printf("Success\n======================================================\n");
close(sockfd);
}
}