La línea de comando de Linux puede ser muy de gran alcance una vez que usted sepa utilizarla. Usted puede analizar los datos, procesos del monitor, y hace muchos de otras cosas útiles y frescas usándolos. Viene a menudo una necesidad de generar un informe y de enviarlo hacia fuera. Podría ser tan simple un requisito como una notificación que la reserva del día pasó a través de multa, o no. Le ayudaré a conseguir comenzado con enviar correos de la línea de comando de Linux y en shell scriptes. También cubriremos enviar los accesorios de la línea de comando. Comenzaremos con el comando del “correo”.
CORREO
El primer funcionamiento una prueba rápida a cerciorarse de el uso del “sendmail” es instalado y de trabajo correctamente. Ejecute el comando siguiente, substituyendo “you@youremailid.com” por su dirección del E-mail.
# correo - mundo” you@youremailid.com de s “hola
Golpee la llave de vuelta y usted vendrá a un de giro nuevo. Incorpore el texto “que esto es una prueba de mi servidor”. Carta recordativa el texto golpeando la llave de vuelta otra vez. Entonces golpee la combinación dominante de Control+D para continuar. El aviso de comando le preguntará si usted desea marcar una copia del correo a cualquier otra dirección, golpe Control+D otra vez. Compruebe su caja. Este comando enviará un correo a la identificación del email mencionada con el tema, “hola mundo”.
Para agregar el contenido al cuerpo del correo mientras que funciona el comando usted puede utilizar las opciones siguientes. Si usted desea agregar el texto en sus el propios:
# repita “esto entrará el cuerpo del correo.” | correo - mundo” you@youremailid.com de s “hola
Y si usted quisiera que el correo leyera el contenido de un archivo:
# correo - mundo” you@youremailid.com de s “hola
Algunas otras opciones útiles en el comando del correo son:
- tema de s (El tema del correo)
- email address de c (Marque una copia a este “email address”, o el cc)
- email address de b (Marque una copia a carbón oculta a este “email address”, o BCC)
Aquí es cómo usted puede ser que utilice estas opciones:
# repita la “recepción al mundo de Calvin n Hobbes” | correo - mundo” calvin@cnh.com de s “hola - c hobbes@cnh.com - b susie.derkins@cnh.com
MUTT
Una de desventajas importantes de usar el comando del correo es que no apoya enviar de accesorios. el mutt, por otra parte, lo apoya. He encontrado esta característica particularmente útil para las escrituras que generan los informes o las reservas no-textuales que son relativamente pequeños de tamaño que quisiera a la reserva a otra parte. 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.
Related Posts:





























{ 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