Die Linux Befehl Linie kann sehr leistungsfähig sein, sobald Sie sie benutzen können. Sie können Daten, Monitorprozesse analysieren und tun eine Menge andere nützliche und kühle Sachen mit ihr. Kommt häufig eine Notwendigkeit, einen Report zu erzeugen und ihn heraus zu verschicken. Es könnte eine Anforderung wie eine Mitteilung so einfach sein, daß die Unterstützung des Tages Geldstrafe durchlief, oder nicht. Ich helfe Ihnen, begonnen mit dem Senden von Post von der Linux Befehl Linie und in Oberteilindexe zu erhalten. Wir umfassen auch das Senden der Zubehöre von der Befehl Linie. Wir fangen mit dem „Post“ Befehl an.
POST
Erster Lauf ein schneller Test, zum sicherzustellen die „sendmail“ Anwendung ist angebracht und richtig arbeitend. Führen Sie den folgenden Befehl durch und „you@youremailid.com“ mit Ihrer E-mail Adresse ersetzen.
# Post - s „hallo Welt“ you@youremailid.com
Schlagen Sie den Rückholschlüssel und Sie kommen zu einem neuem Zeilen-. Tragen Sie den Text ein, „, das dieses ein Test von meinem Bediener ist“. Anschluß der Text, durch den Rückholschlüssel wieder schlagen. Schlagen Sie dann die Tastenkombination von Control+D fortfahren. Das Befehlseingabeformat fragt Sie, wenn Sie eine Kopie der Post zu irgendeiner anderen Adresse kennzeichnen möchten, Erfolg Control+D wieder. Überprüfen Sie Ihren Briefkasten. Dieser Befehl sendet eine Post zur email Kennzeichnung aus, die mit dem Thema, „hallo Welt“ erwähnt wird.
Inhalt dem Körper der Post, beim Laufen lassen des Befehls hinzufügen Sie die folgenden Wahlen verwenden können. Wenn Sie Text auf Ihren Selbst hinzufügen möchten:
# hallen Sie „dieses einsteigt in den Körper der Post.“ wider | Post - s „hallo Welt“ you@youremailid.com
Und wenn Sie Post den Inhalt von einer Akte lesen wünschen:
# Post - s „hallo Welt“ you@youremailid.com
Etwas andere nützliche Wahlen im Postbefehl sind:
- s Thema (Das Thema der Post)
- c email address (Kennzeichnen Sie eine Kopie zu diesem „email address“ oder cm)
- b email address (Kennzeichnen Sie einen blinden Durchschlag zu diesem „email address“ oder BCC)
Ist hier, wie Sie diese Wahlen verwenden konnten:
# hallen Sie „Willkommen zur Welt von Calvin n Hobbes“ wider | Post - s „hallo Welt“ calvin@cnh.com - c hobbes@cnh.com - b susie.derkins@cnh.com
KÖTER
Eine von Hauptbeeinträchtigungen des Verwendens des Postbefehls ist, daß sie nicht das Senden der Zubehöre stützt. Köter einerseits stützt es. Ich habe diese Eigenschaft besonders nützlich für Indexe gefunden, die nicht-Textreports oder Unterstützungen erzeugen, die in der Größe verhältnismäßig klein sind, die ich zur Unterstützung anderwohin möchte. Of course, mutt allows you to do a lot more than just send attachments. It is a much more complete command line mail client than the “mail” command. Right now we’ll just explore the basic stuff we might need often. Here’s how you would attach a file to a mail:
# echo “Sending an attachment.” | mutt -a backup.zip -s “attachment” calvin@cnh.com
This command will send a mail to calvin@cnh.com with the subject (-s) “attachment”, the body text “Sending an attachment.”, containing the attachment (-a) backup.zip. Like with the mail command you can use the “-c” option to mark a copy to another mail id.
SENDING MAIL FROM A SHELL SCRIPT
Now, with the basics covered you can send mails from your shell scripts. Here’s a simple shell script that gives you a reading of the usage of space on your partitions and mails the data to you.
#!/bin/bash
df -h | mail -s “disk space report” calvin@cnh.com
Save these lines in a file on your Linux server and run it. You should receive a mail containing the results of the command. If, however, you need to send more data than just this you will need to write the data to a text file and enter it into the mail body while composing the mail. Here’s and example of a shell script that gets the disk usage as well as the memory usage, writes the data into a temporary file, and then enters it all into the body of the mail being sent out:
#!/bin/bash
df -h > /tmp/mail_report.log
free -m >> /tmp/mail_report.log
mail -s “disk and RAM report” calvin@cnh.com
Now here’s a more complicated problem. You have to take a backup of a few files and mail then out. First the directory to be mailed out is archived. Then it is sent as an email attachment using mutt. Here’s a script to do just that:
#!/bin/bash
tar -zcf /tmp/backup.tar.gz /home/calvin/files
echo | mutt -a /tmp/backup.tar.gz -s “daily backup of data” calvin@cnh.com
The echo at the start of the last line adds a blank into the body of the mail being set out.
This should get you started with sending mails form the Linux command line and from shell scripts. Read up the “man page” for both mail and mutt for more options.





























{ 3 comments… read them below or add one }
You may want to have a look at smtp-client.pl. Check out the usage examples. I’d say it’s the ultimate command line smtp client, but I’m a little biased
echo | mutt -a -s /tmp/backup.tar.gz ?daily backup of data? calvin@cnh.com
should be:
echo | mutt -a /tmp/backup.tar.gz -s ?daily backup of data? calvin@cnh.com
Matt -
Thanks very much for catching that! I’ve made the correction. Cheers!
Leave a Comment