OS X CLI에서 텔넷 및 FTP를 사용하여 이메일 전송 자동화

OS X CLI에서 텔넷 및 FTP를 사용하여 이메일 전송 자동화

다음 스크립트를 실행합니다.

#!/bin/bash

cvs_domain=abc.com
cvs_mail_server=mail.${cvs_domain}
cvs_port=25
telnet $cvs_mail_server $cvs_port<<_EOF_
EHLO $cvs_domain
MAIL FROM:[email protected]
RCPT TO:[email protected]
DATA
Subject:Test!

Don't panic. This is only a test.
.
QUIT
_EOF_

Connection closed by host서버가 이스케이프 문자로 응답한 직후, 메시지를 제공하기 전에 메시지 와 함께 실패합니다 220.

대화형 모드에서 해당 시퀀스를 실행하면(물론 "here-doc" 없이) 목표가 달성됩니다.

서버에 명령줄을 "공급"하는 것이 줄의 다른 쪽 끝에서 예상한 대로 정확히 발생하지 않는 것 같습니다.

내 가정이 맞나요? 이 문제를 완화할 수 있는 방법이 있습니까?

답변1

대화형 명령줄 도구를 스크립트해야 하는 경우 일반적인 솔루션은 expect(1).

답변2

완전성을 기하기 위해 여기에 전체 "추악하지만 작동하는" 솔루션을 게시하고 있습니다(최종 형태에서는 더 많은 사람들에게 이메일을 보내고 첨부 파일을 제공하도록 수정함).

cd "$(dirname "$0")"
working_dir=$(pwd)  # switching to the folder this script has been started from

cvs_domain=mail.org
cvs_mail_server=mail.${cvs_domain}
cvs_port=25
[email protected]
cvs_recipients=([email protected] [email protected])
cvs_delimiter=-----nEXt_paRt_frontier!!VSFCDVGGERHERZZ@$%^zzz---  # MIME multi-part delimiter, do not change

{ echo HELO $cvs_domain; sleep 1
  # set up the email (sender, receivers): 
  echo MAIL FROM:$cvs_sender; sleep 1
  for r in ${cvs_recipients[@]}; do
    echo RCPT TO:$r; sleep 1
  done
  echo DATA; sleep 1
  echo From:$cvs_sender; sleep 1
  for r in ${cvs_recipients[@]}; do
    echo To:$r; sleep 1
  done
  echo Subject:Test for build; sleep 1
  # build the mail structure, according to the MIME standard:
  echo MIME-Version: 1.0; sleep 1
  echo "Content-Type: multipart/mixed; boundary=\"$cvs_delimiter\""; sleep 1
  echo --${cvs_delimiter}; sleep 1
  echo Content-Type: text/plain; sleep 1
  echo; sleep 1
  echo Don\'t panic. This is only a test.; sleep 1
  echo; sleep 1
  echo --${cvs_delimiter}; sleep 1
  echo "Content-Type: text/plain; name=\"test.txt\""; sleep 1
  echo "Content-Disposition: attachment; filename=\"test.txt\""; sleep 1
  echo "Content-Transfer-Encoding: base64"; sleep 1
  echo; sleep 1
  encoded_file=$( base64 ./change.log )  # encoding the contents of the file, according to the declaration above
  echo "$encoded_file"; sleep 1
  echo; sleep 1  
  echo --${cvs_delimiter}; sleep 1 
  echo .; sleep 1
  echo QUIT
  sleep 1; } | telnet $cvs_mail_server $cvs_port  

지연을 조정하기로 선택할 수도 있습니다. 그리고 (제가 생각하기에) 보다 강력한 솔루션을 위해서는 expect(1).

관련 정보