[curl] curl을 사용한 FTP 업로드와 SHELL 스크립트와의 비교
Unix & Linux/Command-Utility / 2010/07/21 18:06
요약 : 다양한 프로토콜을 사용한 데이터 전송을 지원하는 명령어 기반의 프로그램과 C API 라이브러리를 제공한다.
curl 홈페이지 : http://curl.haxx.se/
FTP로 파일을 업로드시에는 간단하게 다음과 같이 사용할 수 있다.
curl -T ~/tmp.txt -u userid:passswd ftp://10.10.1.55/dstdir/
-T/--upload-file <file> Transfer <file> to remote site
-u/--user <user[:password]> Set server user and password
-T/--upload-file <file> Transfer <file> to remote site
-u/--user <user[:password]> Set server user and password
스크립트나, 프로그램상에서 curl을 invoke하여 실행한 후, 에러메시지만 처리할 경우 다음 옵션을 사용할 수 있다.
curl -T ~/tmp.txt -u userid:passswd ftp://10.10.1.55/dstdir/ -s -S
-s/--silent Silent mode. Don't output anything
-S/--show-error Show error. With -s, make curl show errors when they occur
-s/--silent Silent mode. Don't output anything
-S/--show-error Show error. With -s, make curl show errors when they occur
동작시간에 타임아웃을 설정할 경우 다음 옵션을 사용할 수 있다.
curl -T ~/tmp.txt -u userid:passswd ftp://10.10.1.55/dstdir/ -s -S -m 5
-m/--max-time <seconds> Maximum time allowed for the transfer
-m/--max-time <seconds> Maximum time allowed for the transfer
연결시간에 타임아웃을 설정할 경우 다음 옵션을 사용할 수 있다.
curl -T ~/tmp.txt -u userid:passswd ftp://10.10.1.55/dstdir/ -s -S --connect-timeout 3
--connect-timeout <seconds> Maximum time allowed for connection
--connect-timeout <seconds> Maximum time allowed for connection
remote server에 디렉토리를 자동으로 생성하고자 할 경우 다음옵션을 줄 수 있다.
curl -T ~/tmp.txt -u userid:passswd ftp://10.10.1.55/dstdir/ -s -S --connect-timeout 3 --ftp-create-dirs
--ftp-create-dirs Create the remote dirs if not present (F)
--ftp-create-dirs Create the remote dirs if not present (F)
C 소스내에서 popen()을 호출하여 실행할 경우, 다음과 같이 redirect 문자를 추가하여, 에러메시지를 catch할 수 있다.
snprintf(cmd, sizeof(cmd), "curl -T \"%s\" -u \"%s:%s\" ftp://%s:%d/%s/ -s -S -m %d --connect-timeout %d --ftp-create-dirs 2>&1",
src_file, userid, passwd,
ipaddr, port, dst_dir,
timeout, conn_timeout);
fp = popen(cmd, "r");
if(fp == NULL) {
LOG_ERR("%s:: popen() fail: errno=%d(%s)\n",
_func_, errno, strerror(errno));
return -1;
}
while(fgets(buff, sizeof(buff), fp) != NULL) {
LOG_ERR("%s:: ftp upload error: err=%s", _func_, buff);
success_flag = 0;
break;
}
pclose(fp);
C SHELL로 작성한 FTP 업로드 스크립트는 다음과 같다.
#!/bin/csh -f
set usagestring = "USAGE : $0 ipaddr port userid passwd src_file dst_dir"
if ($#argv != 6) then
echo $usagestring
exit 1
endif
set ipaddr = $1
set port = $2
set userid = $3
set passwd = $4
set src = $5
set dst = $6
set src_dir = `dirname $src`
set src_file = `basename $src`
echo "srcdir:$src_dir"
echo "srcfile:$src_file"
echo "[INFO] ipaddr=$ipaddr, port=$port, user=$userid, passwd=$passwd src_file=$src, dst_dir=$dst"
ftp -n $ipaddr $port << EOF
user $userid $passwd
ascii
lcd $src_dir
cd $dst
put $src_file
bye
EOF
exit 0
set usagestring = "USAGE : $0 ipaddr port userid passwd src_file dst_dir"
if ($#argv != 6) then
echo $usagestring
exit 1
endif
set ipaddr = $1
set port = $2
set userid = $3
set passwd = $4
set src = $5
set dst = $6
set src_dir = `dirname $src`
set src_file = `basename $src`
echo "srcdir:$src_dir"
echo "srcfile:$src_file"
echo "[INFO] ipaddr=$ipaddr, port=$port, user=$userid, passwd=$passwd src_file=$src, dst_dir=$dst"
ftp -n $ipaddr $port << EOF
user $userid $passwd
ascii
lcd $src_dir
cd $dst
put $src_file
bye
EOF
exit 0


