Reboot Machines by Monitoring email Instructions
Purpose
To monitor for emails, then execute commands. In this case we want to
- Look for emails from users
- Verify the user sent the email
- Reboot their remote desktop
- Check for errors
- Report to the user the desktop is ready
For this run we want a Linux server to be the middle man to watch an inbox hosted on Office365, then tell Windows machines to reboot. If the Windows machine does not respond we want to use powercli to forcibly reboot using vsphere (and eventually KVM, which is ironically much easier to do. But when execs want to spend money on the main stream....)
Process
Here I am using Centos 7 with a basic install. Epel-release is installed as well.
Email Config
Sendmail
This is so we can send confirmation emails back to the user.
yum -y install sendmail sendmail-cf echo "xxx.xxx.xxx.xxx youremail.relay" >> /etc/hosts sed 's/dnl\ define\(\`SMART_HOST\'\,\ \`smtp\.your\.provider\'\)/define\(\`SMART_HOST\'\,\ \`youremail\.relay\'\)/' /etc/mail/sendmail.mc systemctl restart sendmail
Fetchmail
- Install fetchmail
yum -y install fetchmail
- Create a non-root user dedicated to the task
useradd mailuser passwd mailuser su mailuser cd ~/
- Create .fetchmailrc and add the following contents
poll outlook.office365.com protocol imap port 993 username "user@login.tld" password "password" ssl sslfingerprint "97:08:33:5A:74:09:CC:EA:28:2D:9C:A4:49:3B:A2:C7"
openssl s_client -connect outlook.office365.com:993 -showcerts | openssl x509 -fingerprint -noout -md5
- At this point you can run the following. If you created a new inbox your output should look similar[3]
[emailuser@testserver ~]$ fetchmail -v --sslproto TLS1.2+ --sslcertck fetchmail: 6.3.24 querying outlook.office365.com (protocol IMAP) at Tue 17 Apr 2018 02:29:42 PM EDT: poll started Trying to connect to 40.97.100.50/993...connected. fetchmail: Server certificate: fetchmail: Issuer Organization: DigiCert Inc fetchmail: Issuer CommonName: DigiCert Cloud Services CA-1 fetchmail: Subject CommonName: outlook.com fetchmail: Subject Alternative Name: *.clo.footprintdns.com fetchmail: Subject Alternative Name: *.nrb.footprintdns.com fetchmail: Subject Alternative Name: *.hotmail.com fetchmail: Subject Alternative Name: *.internal.outlook.com fetchmail: Subject Alternative Name: *.live.com fetchmail: Subject Alternative Name: *.office.com fetchmail: Subject Alternative Name: *.office365.com fetchmail: Subject Alternative Name: *.outlook.com fetchmail: Subject Alternative Name: *.outlook.office365.com fetchmail: Subject Alternative Name: attachment.outlook.live.net fetchmail: Subject Alternative Name: attachment.outlook.office.net fetchmail: Subject Alternative Name: attachment.outlook.officeppe.net fetchmail: Subject Alternative Name: ccs.login.microsoftonline.com fetchmail: Subject Alternative Name: ccs-sdf.login.microsoftonline.com fetchmail: Subject Alternative Name: hotmail.com fetchmail: Subject Alternative Name: mail.services.live.com fetchmail: Subject Alternative Name: office365.com fetchmail: Subject Alternative Name: outlook.com fetchmail: Subject Alternative Name: outlook.office.com fetchmail: Subject Alternative Name: substrate.office.com fetchmail: Subject Alternative Name: substrate-sdf.office.com fetchmail: outlook.office365.com key fingerprint: 97:08:33:5A:74:09:CC:EA:28:2D:9C:A4:49:3B:A2:C7 fetchmail: outlook.office365.com fingerprints match. fetchmail: SSL/TLS: using protocol TLSv1.2, cipher ECDHE-RSA-AES256-GCM-SHA384, 256/256 secret/processed bits fetchmail: IMAP< * OK The Microsoft Exchange IMAP4 service is ready. [QgBOADMAUABSADAAMwBDAEEAMAAxADAAOAAuAG4AYQBtAHAAcgBkADAAMwAuAHAAcgBvAGQALgBvAHUAdABsAG8AbwBrAC4AYwBvAG0A] fetchmail: IMAP> A0001 CAPABILITY fetchmail: IMAP< * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+ fetchmail: IMAP< A0001 OK CAPABILITY completed. fetchmail: IMAP> A0002 LOGIN "******@*******.com" * fetchmail: IMAP< A0002 OK LOGIN completed. fetchmail: IMAP> A0003 SELECT "INBOX" fetchmail: IMAP< * 0 EXISTS fetchmail: IMAP< * 0 RECENT fetchmail: IMAP< * FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent) fetchmail: IMAP< * OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)] Permanent flags fetchmail: IMAP< * OK [UIDVALIDITY 14] UIDVALIDITY value fetchmail: IMAP< * OK [UIDNEXT 4] The next unique identifier value fetchmail: IMAP< A0003 OK [READ-WRITE] SELECT completed. fetchmail: No mail for ******@*****.com at outlook.office365.com fetchmail: IMAP> A0004 LOGOUT fetchmail: IMAP< * BYE Microsoft Exchange Server IMAP4 server signing off. fetchmail: IMAP< A0004 OK LOGOUT completed. fetchmail: 6.3.24 querying outlook.office365.com (protocol IMAP) at Tue 17 Apr 2018 02:29:51 PM EDT: poll completed fetchmail: normal termination, status 1
Parse Received Email
In this poor example I will be parsing the emails through a custom script. It would probably be better to do this using established tools.
Resolving user names from email
LDAP Search
- Install openldap client
yum -y install openldap-clients
Parsing directly from email
In this sample we want to pull the email address and subject from the email. At the moment this only works with a single email at a time for obvious reasons. Does not scale well.
#!/bin/bash filetag1="/tmp/email_$(date +%s)" filetag2="/tmp/parse_$(date +%s)" filetag3="/tmp/address_$(date +%s)" fetchmail --sslproto TLS1.2+ --sslcertck if [ "$?" -ne "1" ] && [ "$?" -ne "0" ]; then /usr/bin/echo "$(date) : Failed to download emails" >> /home/email/fetch_log else /usr/bin/cp /var/mail/email $filetag1 grep -E Subject:\|From: $filetag1 | sed 'N;s/\n/ /' > $filetag2 :> /var/mail/email /usr/bin/rm -f $filetag1 while read line; do user=$(grep -Ei Subject:\ [a-z]+,[a-z].[a-z]+ <<<$line | awk 'BEGIN {FS=","} {print $2}') from1=$(grep -Eo "From:.*[a-zA-Z_'-']+@[a-zA-Z_'-']+.com>" <<<$line | sed -e 's/From:\ [A-Za-z]\+\ [A-Za-z]\+\ <//g; s/>//g') if grep -qiE domain1.tld\|domain2.tld\|domain3.tld <<< $from1 ; then /home/mailuser/restartdesktop.sh $user & fi done < $filetag2 /usr/bin/rm $filetag2 fi
Restarting Machines
Restart Windows Machines Using Native Tools
- Install samba-common-tools[4]
yum -y install samba-common-tools
- A command like the following will work[5]
net rpc shutdown -r -W <domain> -S <hostname> --user <username>
Full Script
#!/bin/bash netreboot_log="/home/mailuser/netreboot_log" filelocation="/mnt/" image=$(echo $(ls -t1 $filelocation | grep -i <common machine prefix> | grep -i $1 | head -1) | grep -Eio <common machine prefix-[0-9]\{1,2\}) net rpc shutdown -r -t 0 -W domain.tld -S $image --user user%password if [ "$?" -ne "0" ]; then echo "$(date) : Reboot of $image failed" >> $netreboot_log else ping="0%" count="0" until [ "$ping" = "100%" ]; do if [ "$count" -eq "60" ]; then echo "$(date) : $image has not gone down after 1 minute" >> $netreboot_log ping="100%" testout="failed" else count=$((count + 1)) ping=$(ping -c1 -W1 $image | grep -Eo [0-9]+\%) sleep 1 fi done if [ "$testout" != "failed" ]; then ping="100%" count="0" testout="other" until [ "$ping" = "0%" ]; do if [ "$count" -eq "300" ]; then echo "$(date) : $image has not come back up after 5 minutes" >> $netreboot_log ping="0%" testout="failed" else count=$((count + 1)) ping=$(ping -c1 -W1 $image | grep -Eo [0-9]+\%) fi done echo "Waiting 2 minutes for $image to finish booting" sleep 120 fi if [ "$testout" != "failed" ]; then echo "$image should be ready." fi fi
Restart VMs in vsphere
- ↑ https://michaelwiki.geekgalaxy.com/w/index.php/Check_IMAP_and_POP_with_OpenSSL
- ↑ http://www.aerus.net/2017/04/29/updatefix-fetchmail-unix-connection-to-outlook-365/
- ↑ http://www.fetchmail.info/fetchmail-man.html#2
- ↑ https://lifehacker.com/5275652/shut-down-your-windows-pc-remotely-from-linux
- ↑ https://www.peterdavehello.org/2015/05/remotely-shutdownrestart-windows-via-linux-on-debianubuntu-based-linux/
- ↑ https://virtualizationreview.com/articles/2017/06/01/how-to-install-and-use-powershell-and-powercli-on-linux.aspx