The Linux command line can be very powerful once you know how to use it. Linux kommandolinjen kan være svært kraftig når du vet hvordan du skal bruke den. You can parse data, monitor processes, and do a lot of other useful and cool things using it. Du kan analysere data, overvåke prosesser, og gjøre en masse andre nyttige og kule ting å bruke det. There often comes a need to generate a report and mail it out. Det kommer ofte et behov for å generere en rapport og sende den ut. It could be as simple a requirement as a notification that the day's backup went through fine, or did not. Det kan være så enkelt et krav som et varsel om at dagens sikkerhetskopien gikk gjennom fin eller ikke. I'll help you get started with sending mails from the Linux command line and in shell scripts. Jeg skal hjelpe deg å komme i gang med å sende post fra Linux kommandolinjen og shell scripts. We will also cover sending attachments from the command line. Vi vil også dekke sende vedlegg fra kommandolinjen. We will begin with the “mail” command. Vi skal begynne med "post"-kommandoen.
MAIL MAIL
First run a quick test to make sure the “sendmail” application is installed and working correctly. Først kjøre en rask test for å kontrollere at "sendmail" programmet er installert og fungerer. Execute the following command, replacing “you@youremailid.com” with your e-mail address. Utfør følgende kommando, og erstattet "you@youremailid.com" med din e-postadresse.
# mail -s “Hello world” you@youremailid.com # Post-s "Hello world" you@youremailid.com
Hit the return key and you will come to a new line. Hit retur-tasten, og du vil komme til en ny linje. Enter the text “This is a test from my server”. Skriv inn teksten "Dette er en test fra serveren min. Follow up the text by hitting the return key again. Følge opp teksten ved å trykke retur-tasten igjen. Then hit the key combination of Control+D to continue. Deretter trykker du tastekombinasjonen av Ctrl + D for å fortsette. The command prompt will ask you if you want to mark a copy of the mail to any other address, hit Control+D again. Ledeteksten vil spørre deg om du vil merke en kopi av e-post til en annen adresse, trykk Ctrl + D igjen. Check your mailbox. Sjekk postkassen. This command will send out a mail to the email id mentioned with the subject, “Hello world”. Denne kommandoen vil sende ut en mail til e id nevnt med emnet, "Hello world".
To add content to the body of the mail while running the command you can use the following options. For å legge til innhold i kroppen på e-post mens du kjører kommandoen kan du bruke følgende alternativer. If you want to add text on your own: Hvis du vil legge til tekst på egen hånd:
# echo “This will go into the body of the mail.” | mail -s “Hello world” you@youremailid.com # Echo "Dette vil gå inn i selve e-posten." | Mail-s "Hello world" you@youremailid.com
And if you want mail to read the content from a file: Og hvis du ønsker e-post til å lese innholdet fra en fil:
# mail -s “Hello world” you@youremailid.com < /home/calvin/application.log # Post-s "Hello world" you@youremailid.com </ home / Calvin / application.log
Some other useful options in the mail command are: Noen andre nyttige alternativer i post-kommandoen er:
-s subject (The subject of the mail) -s emne (Emnet for post)
-c email-address (Mark a copy to this “email-address”, or CC) -c epostadresse (Mark kopi til denne "e-postadressen", eller CC)
-b email-address (Mark a blind carbon copy to this “email-address”, or BCC) -b e-postadresse (Mark en blindkopi til denne "e-postadressen", eller BCC)
Here's how you might use these options: Her er hvordan du kan bruke disse alternativene:
# echo “Welcome to the world of Calvin n Hobbes” | mail -s “Hello world” calvin@cnh.com -c hobbes@cnh.com -b susie.derkins@cnh.com # Echo "Velkommen til verden av Tommy n Hobbes" | mail-s "Hello world" calvin@cnh.com-c hobbes@cnh.com-b susie.derkins @ cnh.com
MUTT Mutt
One of major drawbacks of using the mail command is that it does not support the sending of attachments. Ett av store ulemper med å bruke e-kommandoen er at den ikke støtter sending av vedlegg. mutt, on the other hand, does support it. mutt, derimot, støtter det. I've found this feature particularly useful for scripts that generate non-textual reports or backups which are relatively small in size which I'd like to backup elsewhere. Jeg har funnet denne funksjonen spesielt nyttig for skript som genererer ikke-tekstlige rapporter eller sikkerhetskopier som er relativt lite i størrelse, som jeg gjerne sikkerhetskopien et annet sted. Of course, mutt allows you to do a lot more than just send attachments. Selvfølgelig kan mutt du å gjøre mye mer enn bare å sende vedlegg. It is a much more complete command line mail client than the “mail” command. Det er en mye mer komplett kommandolinje postklient enn "post"-kommandoen. Right now we'll just explore the basic stuff we might need often. Akkurat nå får vi bare utforske grunnleggende ting vi kan trenge ofte. Here's how you would attach a file to a mail: Her ser du hvordan du vil legge ved en fil til en e-post:
# echo “Sending an attachment.” | mutt -a backup.zip -s “attachment” calvin@cnh.com # Echo "Sende et vedlegg." | Mutt-en backup.zip-s "vedlegg" 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. Denne kommandoen vil sende en mail til calvin@cnh.com med subjektet (-r) "vedlegg" hovedteksten "Sende et vedlegg.", Som inneholder vedlegget (-a) backup.zip. Like with the mail command you can use the “-c” option to mark a copy to another mail id. I likhet med den post-kommandoen kan du bruke "-c" for å merke en kopi til en annen post id.
SENDING MAIL FROM A SHELL SCRIPT SENDE-post fra et shell script
Now, with the basics covered you can send mails from your shell scripts. Nå, med det grunnleggende dekket kan du sende mails fra 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. Her er en enkel shell script som gir deg en lesning av bruk av plass på partisjoner og poster dataene for deg.
#!/bin/bash #! / bin / bash
df -h | mail -s “disk space report” calvin@cnh.com df-h | mail-s "diskplass rapporten" calvin@cnh.com
Save these lines in a file on your Linux server and run it. Lagre disse linjene i en fil på din Linux-server og kjøre den. You should receive a mail containing the results of the command. Du vil motta en mail som inneholder resultatet av kommandoen. 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. Hvis derimot, må du sende mer data enn bare dette må du skrive data til en tekstfil og skriv den inn i posten kroppen mens du skriver i posten. 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: Her er og eksempel på et shell script som får diskbruken samt minnebruk, skriver dataene til en midlertidig fil, og deretter går det inn i kroppen på e-post blir sendt ut:
#!/bin/bash #! / bin / bash
df -h > /tmp/mail_report.log df-h> / tmp / mail_report.log
free -m >> /tmp/mail_report.log free-m>> / tmp / mail_report.log
mail -s “disk and RAM report” calvin@cnh.com < /tmp/mail_report.log mail-s "disk og RAM-rapporten" calvin@cnh.com </ tmp / mail_report.log
Now here's a more complicated problem. Nå her en mer komplisert problem. You have to take a backup of a few files and mail then out. Du må ta en sikkerhetskopi av noen filer og post deretter ut. First the directory to be mailed out is archived. Først katalogen sendes ut er arkivert. Then it is sent as an email attachment using mutt. Da er det sendt som e-postvedlegg bruker mutt. Here's a script to do just that: Her er et skript til å gjøre nettopp det:
#!/bin/bash #! / bin / bash
tar -zcf /tmp/backup.tar.gz /home/calvin/files tjære-zcf / tmp / backup.tar.gz / home / Calvin / filer
echo | mutt -a /tmp/backup.tar.gz -s “daily backup of data” calvin@cnh.com ekko | mutt-a / tmp / backup.tar.gz-s "daglig backup av 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. Ekkoet i starten av den siste linjen legger en tom i kroppen av posten blir fastsatt.
This should get you started with sending mails form the Linux command line and from shell scripts. Dette bør komme i gang med å sende post danne Linux kommandolinjen og fra shell scripts. Read up the “man page” for both mail and mutt for more options. Les opp "mannen siden" for både post og mutt for flere alternativer.























You may want to have a look at Det kan være lurt å ta en titt på smtp-client.pl smtp-client.pl . . Check out the usage examples. Sjekk ut bruk eksempler. I'd say it's the ultimate command line smtp client, but I'ma little biased Jeg vil si det er den ultimate kommandolinjen SMTP-klient, men jeg er litt partisk
echo | mutt -a -s /tmp/backup.tar.gz “daily backup of data” ekko | mutt-en-s / tmp / backup.tar.gz "daglig backup av data" calvin@cnh.com calvin@cnh.com
should be: skal være:
echo | mutt -a /tmp/backup.tar.gz -s “daily backup of data” ekko | mutt-a / tmp / backup.tar.gz-s "daglig backup av data" calvin@cnh.com calvin@cnh.com
Matt - Matt --
Thanks very much for catching that! Tusen takk for å fange det! I've made the correction. Jeg har gjort korreksjon. Cheers! Skål!
Hi Hei
You can actually send binary attachment via “mail” the following way : Du kan faktisk sende binære vedlegg via "post" på følgende måte:
uuencode /etc/hosts /etc/hosts | mail -s “mail with binary attachment” UUENCODE / etc / hosts / etc / hosts | mail-s "post med binære vedlegg" me@mydomain.org me@mydomain.org
BR BR
Jens Arnfelt Jens Arnfelt
Basically have a test script, whose output comes by mail to me with the use of the command taken from this article: I utgangspunktet har en testskriptet, hvis produksjonen kommer i posten til meg med bruk av kommandoen hentet fra denne artikkelen:
echo `my test script` | mail -s “Test Result” echo `min testskriptet` | mail-s "Test Result" my_mail@mail.com my_mail@mail.com
the problem here is that when i do this.. problemet her er at når jeg gjør dette .. it works correctly on the mailing front, however the mail that i then get ignores the line breaks in the output of the scripts. det fungerer riktig på post foran, men e-post som jeg da få ignorerer linjeskift i produksjon av manuskripter. I really need the line breaks for readability of the output results.. Jeg trenger virkelig den linjeskift for lesbarhet av utdataresultater ..
any pointers on how i could achieve this ?? noen tips om hvordan jeg kunne få til dette? .. .. would really like some help here ville virkelig ha litt hjelp her
I'm looking for a program that will run from a script and will send mail via gmail smtp server. Jeg leter etter et program som kan kjøres fra et manus og sender mail via gmail smtp-server. Any ideas? Any ideas?
martin: have a look at martin: ta en titt på smtp-client.pl smtp-client.pl – it is designed to run from a script or a cron job and send email through a specified server. - Det er utformet for å kjøre fra et skript eller en cron jobb og sende e-post gjennom en bestemt server. For example: For eksempel:
smtp-client.pl --server smtp.gmail.com --from someone@gmail.com --to some@body.else --subject Blah --attach /path/to/a/picture.pngsmtp-client.pl --server smtp.gmail.com --from someone@gmail.com --to some@body.else --subject Blah --attach /path/to/a/picture.pngsmtp-client.pl --server smtp.gmail.com --from someone@gmail.com --to some@body.else --subject Blah --attach /path/to/a/picture.pngsmtp-client.pl --server smtp.gmail.com --from someone@gmail.com --to some@body.else --subject Blah --attach /path/to/a/picture.pngsmtp-client.pl --server smtp.gmail.com --from someone@gmail.com --to some@body.else --subject Blah --attach /path/to/a/picture.pngIt can also authenticate with a Gmail username and password among other things. Det kan også godkjennes med et Gmail-brukernavn og passord blant annet.
Thanks Michal. Takk Michal. I'll check that out. Jeg skal sjekke det ut.
An excellent tutorial !! Et utmerket undervisningstimene! Thanks Much Takk Much