Linuxen befaller fodrar kan vara mycket kraftig, när du vet hur man använder den. Du kan parse data, att övervaka bearbetar, och gör en radda annat användbart och kyler saker genom att använda det. Det kommer ofta ett behov att frambringa en rapport och att posta den ut. Det kunde vara så enkelt ett krav som ett meddelande, att dagens reserv gick till och med bot, eller inte. Jag ska hjälp som du får startad med att överföra postar från Linuxen befaller fodrar och beskjuter in skrivar. Vi ska täcker också överföring av tillbehör från befalla fodrar. Vi ska börjar med ”postar” befaller.
POSTA
Första körning en ömt ställe testar för att se till att ”den sendmail” applikationen installeras och arbetet korrekt. Utför efter befalla som byter ut ”you@youremailid.com” med din e-post, tilltalar.
# posta - s ”hälsningvärlden” you@youremailid.com
Slå det återgångt stämm, och du ska kommet till en ny rad. Skriv in texten, ”som denna är en testa från min server”. Uppföljning som texten, genom att slå det återgångt, stämm igen. Slå därefter den nyckel- kombinationen av Control+D att fortsätta. Den ska befallabetalningspåminnelsen frågar dig, att om du önskar att markera en kopiera av posta till någon annan, tilltala, slå Control+D igen. Kontrollera din brevlåda. Detta befaller ska överför ut en posta till e-postIDet som nämns med betvinga, ”hälsningvärlden”.
För att tillfoga nöjt till förkroppsliga av postastundspringet befalla kan du använda efter alternativen. Om du önskar att tillfoga text på ditt eget:
# eka ”ska detta går in i förkroppsliga av posta.”, | posta - s ”hälsningvärlden” you@youremailid.com
Och om du önskar, posta för att läsa det nöjt från en spara:
# posta - s ”hälsningvärlden” you@youremailid.com
Några andra användbara alternativ i posta befaller är:
- s betvingar (Betvinga av posta)
- c-email-address (Markera en kopiera till denna ”email-address” eller CC),
- b-email-address (Markera ett blint kol kopierar till denna ”email-address” eller BCC),
Är här hur dig styrkabruk dessa alternativ:
# eka ”välkomnandet till världen av Calvin n Hobbes”, | posta - s ”hälsningvärld” calvin@cnh.com - c hobbes@cnh.com - b susie.derkins@cnh.com
BYRACKA
En av ha som huvudämne nackdelar av att använda posta befaller är att den inte stöttar överföringen av tillbehör. byrackan, stöttar å ena sidan det. Jag har funnit detta särdrag som bestämt är användbart för, skrivar som frambringar non-text- rapporter, eller reserver, som är förhållandevis lilla in, storleksanpassar, som jag skulle något liknande till säkerhetskopia någon annanstans. 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