Skip to content

Instantly share code, notes, and snippets.

@nfedyashev
Created March 9, 2023 19:37
Show Gist options
  • Save nfedyashev/bf2cc05c09f1e1ce59729f986f9c8c15 to your computer and use it in GitHub Desktop.
Save nfedyashev/bf2cc05c09f1e1ce59729f986f9c8c15 to your computer and use it in GitHub Desktop.
Using expect to drive netcat - Simply piping the required sequence of commands into netcat is unlikely to work because the SMTP commands and responses would not be properly interleaved. If you want to use the method above from a script then you will need to make use of a program such as expect. Here is an example:
#!/usr/bin/expect
set timeout 30
proc abort {} { exit 2 }
spawn nc -C mail.example.org 25
expect default abort "220 "
send "HELO example.com\r"
expect default abort "\n250 "
send "MAIL FROM:bar@example.org\r"
expect default abort "\n250 "
send "RCPT TO:foo@example.org\r"
expect default abort "\n250 "
send "DATA\r"
expect default abort "\n354 "
send "From: bar@example.org\r"
send "To: foo@example.com\r"
send "Subject: Test\r"
send "Date: Thu, 20 Dec 2012 12:00:00 +0000\r"
send "\r"
send "Testing\r"
send ".\r"
expect default abort "\n250 "
send "QUIT\r"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment