bash and telnet to test an email - bash

I'm trying to find out whether an email address is valid.
I've accomplished this by usign telnet, see below
$ telnet mail.example.com 25
Trying 0.0.0.0...
Connected to mail.example.com.
Escape character is '^]'.
220 mail.example.com Mon, 14 Jan 2013 19:01:44 +0000
helo email.com
250 mail.example.com Hello email.com [0.0.0.0]
mail from:blake#email.com
250 OK
rcpt to:gfdgsdfhgsfd#example.com
550 Unknown user
with this 550 request i know that the address is not valid on the mail server... if it was valid i would get a response like the below:
250 2.1.5 OK
How would I automate this in a shell script? so far I have the below
#!/bin/bash
host=`dig mx +short $1 | cut -d ' ' -f2 | head -1`
telnet $host 25
Thanks!

Try doing this :
[[ $4 ]] || {
printf "Usage\n\t$0 <domain> <email> <from_email> <rcpt_email>\n"
exit 1
}
{
sleep 1
echo "helo $2"
sleep 0.5
echo "mail from:<$3>"
sleep 0.5
echo "rcpt to:<$4>"
echo
} | telnet $1 25 |
grep -q "Unknown user" &&
echo "Invalid email" ||
echo "Valid email"
Usage :
./script.sh domain email from_email rcpt_email

You could always enter your commands into a plain text file, line after line, just as if you typed them on the command line. Then you can use something like
cat commands.txt | telnet mail.example.com 25 | grep -i '550 Unknown User'
Since you will probably need to consider this text file as template, (I am assuming you will probably want to parameterize the e-mail address) you may need to insert a call to awk to take the output of 'cat commands.txt' and insert your e-mail address.

variables to change
BODY="open realy smtp test"
SMTP-SRV="server_ip"
SMTP-PORT="25"
RCPT="name#domain"
SRC="name#domain"
then run in bash
/bin/nc ${SMTP-SRV} ${SMTP-PORT} << EOL
ehlo example_domain.com
mail from:${SRC}
RCPT to:${RCPT}
data
From:${SRC}
To:${RCPT}
subject: Telnet test
${BODY}
.
quit
EOL

Related

match a dd:dd:dd and do something in bash

I am trying to match some digits like "38:00:00" from device_output file, collected from sftp server and put some if condition as sending mail with the that output. I did something but I want to have something more advanced code there beside egrep ":" :))
device_output file
<style>
table,td{blbalba}
</style>
Device Name Ip Address Flaps ASN Uptime
Nexus 182.168.2.2 0 300 38:00:20
ASA Firewall 182.168.2.3 0 400 44:01:20
ASR CUBE 182.168.2.4 0 400 22w02d
VMWare 182.168.2.5 0 400 12:03:20
Nexus 182.168.2.5 0 400 12w03d
Nexus 182.168.2.5 0 400 12:03:20
bash script:
#!/bin/bash
#Variables
SFTPHOSTNAME="192.168.1.1"
SFTPUSERNAME="user"
SFTPPASSWORD="pass"
FOLDER="/home/$USER/ftp"
#SFTP CONNECTION
output=$(sshpass -p $SFTPPASSWORD sftp $SFTPUSERNAME#$SFTPHOSTNAME << !
cd $FOLDER
get device_status
exit
!);
#!/bin/bash
if egrep ":" device_output ; then
cat device_output | egrep ":|style|table|Device" | mailx -s "$(echo -e "BGP Sessions Uptime Issue \nContent-Type: text/html")" -r from_user#yah to_user#yah
else
exit 0
fi
I guess you could use egrep this way :
egrep ".:..:.."
which would match 1:00:00 or 38:00:00
or, if you care about the digits :
egrep "[0-9]:[0-9][0-9]:[0-9][0-9]|[0-9]:" would also match

print last occurrence of each unique line by IP in file

I need to parse a log file so that the following entries like this:
Jul 23 17:38:06 192.168.1.100 638 "this message will always be the same"
Jul 23 17:56:11 192.168.1.100 648 "this message will always be the same."
Jul 23 18:14:17 192.168.1.101 "this message will always be the same."
Jul 23 18:58:17 192.168.1.101 "this message will always be the same."
Look like this:
Jul 23 17:56:11 192.168.1.100 648 "this message will always be the same."
Jul 23 18:58:17 192.168.1.101 "this message will always be the same."
Basically what I am doing is taking a file that has duplicate IP addresses but with different timestamps, and finding the last occurrence (or most recent by time) of each IP address, and printing that to the screen or directing it into another file.
What I have tried:
I have written a bash script that I thought would allow me to do this but it is not working.
#!/bin/bash
/bin/grep 'common pattern to all lines' /var/log/file | awk '{print $4}' | sort - u > /home/user/iplist
while IFS='' read -r line || [[ -n "$line" ]]; do
echo "$line"
done < "/home/user/iplist"
awk '/'$line'/ {a=$0}END{print a} ' /var/log/logfile
The script runs and outputs each IP address, but it does not print the whole line except for the last one.
ex..
192.168.100.101
192.168.100.102
192.168.100.103
Jul 23 20:20:55 192.168.100.104 "this message will always be the same."
The first command in the script takes all unique occurrences of an IP and sends that to a file. The while loop assigns a "$line" variable to each line which is then passed to awk which I thought would take each IP then search the actual file and print out the last occurrance of each one. How can I get this to work, either with a script or perhaps an awk one liner?
$ tac file | awk '!seen[$4]++' | tac
Jul 23 17:56:11 192.168.1.100 648 "this message will always be the same."
Jul 23 18:58:17 192.168.1.101 "this message will always be the same."
You can use this awk command:
awk 'NF{a[$4]=$0} NF && !seen[$4]++{ips[++numIps]=$4} END {
for (i=1;i<=numIps;i++) print a[ips[i]] }' file
Jul 23 17:56:11 192.168.1.100 648 "this message will always be the same."
Jul 23 18:58:17 192.168.1.101 "this message will always be the same."

BASH grep with multiple parameters + n lines after one of the matches

I have a bunch of text as a output from command, I need to display only specific matching lines plus some additional lines after match "message" (message text is obviously longer than 1 line)
what I tried was:
grep -e 'Subject:' -e 'Date:' -A50 -e 'Message:'
but it included 50 lines after EACH match, and I need to pass that only to single parameter. How would I do that?
code with output command:
(<...> | telnet <mailserver> 110 | grep -e 'Subject:' -e 'Date:' -A50 -e 'Message:'
Part of the telnet output:
Date: Tue, 10 Sep 2013 16
Message-ID: <00fb01ceae25$
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="----=_NextPart_000_00FC_01CEAE3E.DE32CE40"
X-Mailer: Microsoft Office Outlook 12.0
Thread-Index: Ac6uJWYdA3lUzs1cT8....
Content-Language: lt
X-Mailman-Approved-At: Tue, 10 Sep 2013 16:0 ....
Subject: ...
X-BeenThere: ...
Precedence: list
Try following:
... | telnet ... > <file>
grep -e 'Subject:' -e 'Date:' <file> && grep -A50 -e 'Message:' <file>
Will need to dump the output to a file first.
This can be done with awk as well, without the need for dumping output to a file.
... | telnet ... | awk '/Date:/ {print}; /Subject:/ {print}; /Message:/ {c=50} c && c--'
With grep it would be hard to do. Better use awk for this
awk '/Subject:|Date:/;/Message:/ {while(l<=50){print $0;l++;getline}}'
Here the awk prints 50 lines below the Message: pattern and only one line is printed for all other patterns.

How to hand over bash variable to your python script?

I am rather new to bash scripting, more used to batch. Anyways what I am trying to do is to be able to get a string from a bash variable that is created from an nmap scan and make it a variable for a python script. I was going to use grep but it gets too much. Here are the results:
Starting Nmap 5.21 ( http://nmap.org ) at 2014-05-22 20:12 PDT
Nmap scan report for 192.168.1.201
Host is up (0.00020s latency).
Not shown: 96 filtered ports
PORT STATE SERVICE
135/tcp open msrpc
139/tcp open netbios-ssn
445/tcp open microsoft-ds
3389/tcp open ms-term-serv
MAC Address: 02:21:9B:88:3C:06 (Unknown)
Nmap done: 1 IP address (1 host up) scanned in 4.77 seconds
What I want to get is: MAC Address: 02:21:9B:88:3C:06 WITH the space at the end. SO it would be MAC=$
Thank you in advance
MAC=$(egrep -o '^MAC Address: (..:){5}.. ' filename.txt)
The -o option makes egrep just output the part of the line that matches the regexp, so it will just go up to the space after the address.
You can use grep and pipe (|) to awk :
MAC=`grep -a 'MAC' Nmapscan.txt | awk -F ' ' {'print $1" "$2" "$3" " '}`
Or
MAC=`nmap 192.168.1.201 | grep 'MAC' | awk -F ' ' {'print $1" "$2" "$3" " '}`
If possible I'd strongly recommend adding this change too for security:
NMAP_FILE=`mktemp`
# start
nmap 192.168.1.201 > $NMAP_FILE
# middle
MAC=`grep -a 'MAC' $NMAP_FILE | awk -F ' ' {'print $1" "$2" "$3" " '}`
# end
rm -f $NMAP_FILE
To pass the $MAC over to your python script you can use:
ALERT.py $MAC &
and add this to ALERT.py
import sys
mac_address = sys.argv[0]
# now `mac_address` is "MAC Address: 02:21:... "

Bash: Adding 1 to serial counter in a named.conf file, how is it done?

I have a script which updates DNS records on a DNS server. Every time the named.conf file is updated with a new site I have to raise the serial counter by at least 1.
So my scripts is running on a remote machine and I'm about to add the next line:
serial=`ssh root#172.19.214.X 'cat /var/named/named.booking.zone |grep serial |awk -F\" \" '{print $1}''`
It doesn't work well, I think i'm not escaping the "" correctly...
And then I thought of something like that:
ssh root#172.19.214.X "sed -e 'g/"$serial"/"$serial"+1/s' /var/named/named.booking.zone"
My source file:
$TTL 600
# IN SOA root. booking.local. (
2013030311 ; serial (d. adams)
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Minimum
;
IN MX 10 mail
IN NS dns
IN A 172.19.214.X
www IN A 172.19.214.X
Can you please show me how to do the escapes correctly?
Thanks!
Having this content for /var/named/named.booking.zone :
serial "1"
You can use something like this:
#!/usr/bin/bash
serial=$(ssh root#172.19.214.X 'grep serial /var/named/named.booking.zone' 2>/dev/null |awk '{print $1}' )
(( next_serial = serial + 1 ))
ssh root#172.19.214.X 'sed -i.bak -e 's_${serial}_${next_serial}_g' /var/named/named.booking.zone' 2>/dev/null

Resources