>> Read No. 724 article  
expect 를 사용한 갈무리 스크립트

등록 2000-09-22 12:48:00     조회 4
이름 Reiot    

		안녕하세요 레이옷입니다.
 불현듯 바람이 불고, 결국 밤새서 하이텔의 게시판을
갈무리해서 각각을 파일로 저장하는 스크립트를 제작
해봤습니다. 흠. 서비스로 GPL을 명시해봤어용. emoticon
그리고, 나중에 까먹지 않기 위해서 expect 문법을
친절하게 코멘트해봤습니다.
 ==
 <pre>
#!/usr/bin/expect
#
# hitel-grabber.exp
# Copyright (C) 2000 Reiot(reiot at hanmail.net), the Fallen Lord of
MUDMANIA
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
1307, USA.
#
 #
# configuration
#
# expect 는 tcl 로 구현되었기 때문에, 당연히 tcl 문법을 사용해야 한다.
#
# assignment : set var val
#
set command "downall"
 #
# telnet 을 사용해서 하이텔에 연결한다.
#
# spawn 을 사용해서 expect script 와 I/O 를 연결할 프로세스를 생성한다.
#
spawn telnet home.hitel.net
 #
# 아이디를 입력한다.
#
# expect 는 spawn 된 프로세스로부터의 입력을 기다리는 expect script
command 이다.
# interact 는 user key stroke 를 기다리는 command 이다.
# -nobuffer 를 사용하면 matching pattern 을 output process 로 출력할
수 있다.
# -re 는 다음에 오는 패턴이 regular expression 임을 의미한다.
# .* 는 "아무 글자나 여러 개"를 의미한다. ()로 둘러싸야 한다.
# 따라서, (.*)(
|) 은
 또는  으로 끝이 나는 한 라인의 스트링을
의미한다.
# send 는 spawn 된 프로세스로 출력을 보낸다.
# $interact_out(1,string) 은 사용자의 입력이 저장된 버퍼를 의미한다.
# 이때, return 을 해주지 않으면 interact 모드를 빠져나올 수 없다.
#
expect "이용자ID :" {
	interact -nobuffer -re "(.*)(
|)" {
		send "$interact_out(1,string)
"; return
	}
}
 #
# 패스워드를 입력한다.
#
expect "비밀번호 :" {
	interact -re "(.*)(
|)" {
		send "$interact_out(1,string)
"; return
	}
}
 #
# 사용자가 갈무리받을 게시판으로 이동해서, $command 를 실행하면 갈무리
를 시작한다.
#
# stty echo 를 해주면, expect_user 에서 사용자의 입력을 볼 수 있다. -
echo 는 반대.
# send_user 를 하면, spawn 된 process 대신 표준 출력으로 스트링을 전송
한다.
# expect_user 는 사용자로부터 입력을 기다린다. ( interact pattern
action 과 비슷함 )
# tcl의 expr 명령은 digit string + integer 를 integer 로 자동 변환해준
다.
# string trimright src token 은 src 의 오른편에서부터 token 안에 속하
는 캐릭터를 삭제한다.
# tcl 의 for 루프는 for { init } { condition } { increment } { body }
와 같이 사용한다.
# -ex 는 exact pattern 이라는 뜻이다. <> 는 -ex 와 함께 써서 항상  으
로 escape 시켜줘야 듯!
# log_file filename 은 spawn process 로부터의 모든 입력을 filename 파일
로 출력한다.
# logfile; 처럼 파라미터 없이 사용하게 되면 이전의 로깅 작업을 종료한다
는 뜻이다.
#
interact {
	-nobuffer -re "$command
" {
		stty echo;
		send_user "갈무리받을 시작글번호, 마침글번호, 파
일prefix를 입력하세요.";
		send_user "시작글번호 : ";
		expect_user -re "(.*)(
|)";
		send_user "";
		set from [expr $expect_out(buffer)+0];
		send_user "마침글번호 : ";
		expect_user -re "(.*)(
|)";
		send_user "";
		set to [expr $expect_out(buffer)+0];
		send_user "파일prefix : ";
		expect_user -re "(.*)(
|)";
		stty -echo;
		set prefix [string trimright $expect_out(buffer)

];
		send_user "downloading from $from to $to";
		for { set i $from } { $i <= $to } { incr i } {
			send "pr $i
";
			expect -ex "[ENTER] 를 누르십시오." {
send "
" };
			log_file "$prefix-$i.txt";
			expect -ex "PRINTER/CAPTURE를 OFF 하시고"
{ log_file; };
			expect -ex "[ENTER] 를 누르십시오." {
send "
" };
			expect "선택(H:도움말) >>"
		}
	}
}
</pre>
 ==
 그런데, 하이텔의 게시판을 긁어와서 DB에 저장해두는 것은 별 문제가
없겠지만, 이걸 웹상으로 공개하면 약관 위반이겠지요??? emoticon;
아냐.. 동호회 사람들한테 칼맞을꺼야.. emoticon
 ==
 BUG : 각 게시물의 마지막 3줄.. ---- PRINTER/CAPTURE를 OFF 하시고
[ENTER] 를 누르십시오. 를 파일에서 빼야 하는데.. 흐흑.. 이건 이 스크립
트에서는 잘 안되는군요. 흠.. 어떡해야 할까욥?
 --
Reiot, the Fallen Lord of MUDMANIA
이름
암호
  목록보기 윗글 아랫글
글쓰기
답장쓰기 수정 삭제
정규표현식 [ 상세 검색 ]
페이지로딩: [ 0.50 초 ] 작업시간: [ 0.03 초 ]

Copyleft 1999-2026 by JSBoard Open Project
Theme Designed by IDOO All right reserved
[TOP]

적수네 동네
+
| 적수네 동네
| 공부방
| 리눅스 잡지 서고
| LSN 소스
| 링크 모음
+---+
게시판
+
| 떠들어보세!
| 질문과 답변
| 새소식과 정보
| 1원짜리 팁?
| 대화방
+---+
칼럼?
+
| 세하 훔쳐보기
| Welcome2nite
| 혜진의 염장판
+---+
리눅스 상표권
+
| 반대 서명란
| 토론 게시판
+---+
GNU
+
| GNU 선언문
| GNU GPL
| GNU 미러 목록
+---+
프로젝트?
+
| 리눅스카운터
| RC5DES
| 실질헌법 제작
+---+
커널 소식
+
| 안정 버젼: 2.4.14
+---+
테마 선택
+
LSN 방송국?
+
| OFF AIR
+---+
회원
+
| 로그인
+---+
[ 적수네 동네 ] [ 리눅스 상표권 독점 반대 ] [ 한글 리눅스 문서 프로젝트 ] [ KrLine ] [ 사랑넷 ] [ Valid HTML 4.0! ] [ SlashDot ] [ Freshmeat ]
Copyleft (C) 1998-2001 Byeong-Chan Kim . License
TIME: 1791033680
System by WYZsoft, HDD by I.O.Linux, Mizi Research, Embryo, WOWLINUX, Domain by SarangNet, Network by KrLine.