如何用 telnet 來測試 SMTP 已是基本常識了,但 swaks 這工具可以更省力些。
如何測試SMTP?
telnet 的指令很單純,網路上也很多的教學,但最早怎麼測 SMTP 的文件,應該是這篇 RFC 821,說明了telnet之後下什麼指令測試。透過swaks把這些常重覆會用到的語法包裝成下參數的方式來執行。
簡易安裝
swaks 已在Linux裡有現成的套件。
# pacman -S swaks
resolving dependencies...
looking for inter-conflicts...
Packages (3): perl-net-dns-0.72-2 perl-net-ip-1.26-1 swaks-20130209.0-1
Total Download Size: 0.34 MiB
Total Installed Size: 0.91 MiB
:: Proceed with installation? [Y/n]
:: Retrieving packages ...
perl-net-ip-1.26-1-any 24.4 KiB 4.77M/s 00:00 [################################] 100%
perl-net-dns-0.72-2-x86_64 262.8 KiB 7.55M/s 00:00 [################################] 100%
swaks-20130209.0-1-any 59.4 KiB 11.6M/s 00:00 [################################] 100%
(3/3) checking keys in keyring [################################] 100%
(3/3) checking package integrity [################################] 100%
(3/3) loading package files [################################] 100%
(3/3) checking for file conflicts [################################] 100%
(3/3) checking available disk space [################################] 100%
(1/3) installing perl-net-ip [################################] 100%
(2/3) installing perl-net-dns [################################] 100%
(3/3) installing swaks [################################] 100%
但觀察這套件其實就是只有一個 perl script
快速使用
若沒有指定 from 的話,會用 linux 本身的帳號及主機名稱為 from 來寄測試信:
$ swaks --to root@othercompany.com --server mycompany.com
=== Trying mycompany.com:25...
=== Connected to mycompany.com.
<- 220 mycompany.com ESMTP Postfix
-> EHLO AP
<- 250-mycompany.com
<- 250-PIPELINING
<- 250-SIZE 10240000
<- 250-VRFY
<- 250-ETRN
<- 250-ENHANCEDSTATUSCODES
<- 250-8BITMIME
<- 250 DSN
-> MAIL FROM:<singernew@ithelp>
<- 250 2.1.0 Ok
-> RCPT TO:<root@othercompany.com>
<** 454 4.7.1 <root@othercompany.com>: Relay access denied
-> QUIT
<- 221 2.0.0 Bye
=== Connection closed with remote host.
指定 --from 能指定寄件者名稱,
$ swaks --from singernew@mycompany.com --to root@othercompany.com --helo=unqualified --server othercompany.com
=== Trying othercompany.com:25...
=== Connected to othercompany.com.
<- 220 othercompany.com ESMTP Postfix
-> EHLO unqualified
<- 250-othercompany.com
<- 250-PIPELINING
<- 250-SIZE 10240000
<- 250-VRFY
<- 250-ETRN
<- 250-ENHANCEDSTATUSCODES
<- 250-8BITMIME
<- 250 DSN
-> MAIL FROM:<singernew@mycompany.com>
<- 250 2.1.0 Ok
-> RCPT TO:<root@othercompany.com>
<- 250 2.1.5 Ok
-> DATA
<- 354 End data with <CR><LF>.<CR><LF>
-> Date: Sat, 12 Oct 2013 07:34:49 +0800
-> To: root@othercompany.com
-> From: singernew@mycompany.com
-> Subject: test Sat, 12 Oct 2013 07:34:49 +0800
-> X-Mailer: swaks v20130209.0 jetmore.org/john/code/swaks/
->
-> This is a test mailing
->
-> .
<- 250 2.0.0 Ok: queued as 964DB49A53
-> QUIT
<- 221 2.0.0 Bye
=== Connection closed with remote host.
若把 --server 的參數拿掉,則會自動找收件者的 MX 記錄,再往該 MX 去測試:
$ swaks --from singernew@mycompany --to root@othercompany.com --helo=unqualified
=== Trying nopam.othercompany.com:25...
=== Connected to nopam.othercompany.com.
<- 220 nopam.othercompany.com SW-Nopam+ ****
-> EHLO unqualified
<- 250-nopam.othercompany.com
<- 250-PIPELINING
<- 250-SIZE 209915200
...
指定內容
測試的信件內容預設為 This is a test mailing,若要指定內容可以用這語法指到文字檔:
--body /path/to/gtube/file
若要測試寄病毒信件會有什麼結果,可用這樣子的指令:
swaks -t user@example.com --attach - --server test-server.example.com --suppress-data </path/to/eicar.txt
網管就是希望能用最省力的工具來做測試。
參考資料:
swaks 官網
swaks - Swiss Army Knife SMTP, the all-purpose smtp transaction tester
Testing SMTP servers with SWAKS