Mailing List Archive

How to read file content and send email on Debian Bullseye
Hi guys,

On a Debian Bullseye server I have a lftp upload and after it I should
send an email.

I thought to read the lftp log file where I have these lines:

2023-01-30 18:30:02
/home/my_user/local_folder/upload/my_file_30-01-2023_18-30.txt ->
sftp://ftp_user@ftpserver_ip:2201/remote_ftp_folder/my_file_30-01-2023_18-30.txt
0-1660576 4.92 MiB/s

2023-02-02 18:30:02
/home/my_user/local_folder/upload/my_file_02-02-2023_18-30.txt ->
sftp://ftp_user@ftpserver_ip:2201/remote_ftp_folder/my_file_02-02-2023_18-30.txt
0-603093 3.39 MiB/s

I'd like to use Python to check, from monday to friday (the lftp script
runs in crontab from monday to friday) when the upload works is finished
and I should send an email.

I could read by Python lftp.log and after it if there's a line with the
same day of the machine I could send an email with ok otherwise the
email will send a message with "no upload".

How could I do by Python?

Regards.
^Bart
--
https://mail.python.org/mailman/listinfo/python-list
RE: How to read file content and send email on Debian Bullseye [ In reply to ]
Bart, you may want to narrow down your request to something quite specific.
For example, try to do whatever parts you know how to do and when some part
fails or is missing, ask.

I might have replied to you directly if your email email address did not
look like you want no SPAM, LOL!

The cron stuff is not really relevant and it seems your idea is to read a
part or all of a log file, parse the lines in some way and find a line that
either matches what you need or fail to find it. Either way you want to send
an email out with an appropriate content.

Which part of that do you not know how to do in python? Have you done some
reading or looking?



-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of ^Bart
Sent: Saturday, February 4, 2023 10:05 AM
To: python-list@python.org
Subject: How to read file content and send email on Debian Bullseye

Hi guys,

On a Debian Bullseye server I have a lftp upload and after it I should send
an email.

I thought to read the lftp log file where I have these lines:

2023-01-30 18:30:02
/home/my_user/local_folder/upload/my_file_30-01-2023_18-30.txt ->
sftp://ftp_user@ftpserver_ip:2201/remote_ftp_folder/my_file_30-01-2023_18-30
.txt
0-1660576 4.92 MiB/s

2023-02-02 18:30:02
/home/my_user/local_folder/upload/my_file_02-02-2023_18-30.txt ->
sftp://ftp_user@ftpserver_ip:2201/remote_ftp_folder/my_file_02-02-2023_18-30
.txt
0-603093 3.39 MiB/s

I'd like to use Python to check, from monday to friday (the lftp script runs
in crontab from monday to friday) when the upload works is finished and I
should send an email.

I could read by Python lftp.log and after it if there's a line with the same
day of the machine I could send an email with ok otherwise the email will
send a message with "no upload".

How could I do by Python?

Regards.
^Bart
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: How to read file content and send email on Debian Bullseye [ In reply to ]
On 2/4/2023 10:05 AM, ^Bart wrote:
> Hi guys,
>
> On a Debian Bullseye server I have a lftp upload and after it I should
> send an email.
>
> I thought to read the lftp log file where I have these lines:
>
> 2023-01-30 18:30:02
> /home/my_user/local_folder/upload/my_file_30-01-2023_18-30.txt ->
> sftp://ftp_user@ftpserver_ip:2201/remote_ftp_folder/my_file_30-01-2023_18-30.txt 0-1660576 4.92 MiB/s
>
> 2023-02-02 18:30:02
> /home/my_user/local_folder/upload/my_file_02-02-2023_18-30.txt ->
> sftp://ftp_user@ftpserver_ip:2201/remote_ftp_folder/my_file_02-02-2023_18-30.txt 0-603093 3.39 MiB/s
>
> I'd like to use Python to check, from monday to friday (the lftp script
> runs in crontab from monday to friday) when the upload works is finished
> and I should send an email.
>
> I could read by Python lftp.log and after it if there's a line with the
> same day of the machine I could send an email with ok otherwise the
> email will send a message with "no upload".
>
> How could I do by Python?

Not Python, but you could run a shell script that sends the file with
lftp, and depending on the return code uses xdg-email to send one of the
two messages. This would probably be simpler than using Python.

Otherwise you haven't said what part of the process you need help with.
Reading the log file? Checking the date? Triggering the Python script?
Sending the email message with Python?

For the latter, it's covered in the Python docs -

https://docs.python.org/3.10/library/email.examples.html


--
https://mail.python.org/mailman/listinfo/python-list
Re: How to read file content and send email on Debian Bullseye [ In reply to ]
On 2023-02-04 at 17:59:11 -0500,
Thomas Passin <list1@tompassin.net> wrote:

> On 2/4/2023 10:05 AM, ^Bart wrote:
> > Hi guys,
> >
> > On a Debian Bullseye server I have a lftp upload and after it I should
> > send an email.

[...]

> [...] you could run a shell script that sends the file with lftp, and
> depending on the return code uses xdg-email to send one of the two
> messages. This would probably be simpler than using Python.

xdg-email appears to be for interactive use (it opens the user's
"preferred email composer"); I think sendmail would work much better
from a script.

Otherwise, I had the same initial thought, to add to and/or build a
wrapper around the existing lftp script.
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to read file content and send email on Debian Bullseye [ In reply to ]
On 2/4/2023 10:13 PM, 2QdxY4RzWzUUiLuE@potatochowder.com wrote:
> On 2023-02-04 at 17:59:11 -0500,
> Thomas Passin <list1@tompassin.net> wrote:
>
>> On 2/4/2023 10:05 AM, ^Bart wrote:
>>> Hi guys,
>>>
>>> On a Debian Bullseye server I have a lftp upload and after it I should
>>> send an email.
>
> [...]
>
>> [...] you could run a shell script that sends the file with lftp, and
>> depending on the return code uses xdg-email to send one of the two
>> messages. This would probably be simpler than using Python.
>
> xdg-email appears to be for interactive use (it opens the user's
> "preferred email composer"); I think sendmail would work much better
> from a script.
>
> Otherwise, I had the same initial thought, to add to and/or build a
> wrapper around the existing lftp script.

I have never used it, and I did see that wording in the man page, but it
also showed putting all the info on the command line - from, to,
subject, etc - so I thought it might be able to use a command line
client without needed any GUI interaction. But I don't know for sure.

--
https://mail.python.org/mailman/listinfo/python-list
Re: How to read file content and send email on Debian Bullseye [ In reply to ]
> Not Python, but you could run a shell script that sends the file with
> lftp, and depending on the return code uses xdg-email to send one of the
> two messages. This would probably be simpler than using Python.

This machine is a server without DE, just command line and I didn't
configure mail client so maybe I should use something like ssmtp.

> Otherwise you haven't said what part of the process you need help with.
> Reading the log file?  Checking the date?  Triggering the Python script?
>  Sending the email message with Python?

I need to understand "just" if the upload job is finished and, I think,
the best way is to read the log file so I should write a code like:

"if there's a line with the same date of the machine than send email
with ok else send an email with failed"

I do just an upload every day, from monday to friday so I don't have in
the same day other lines.

> For the latter, it's covered in the Python docs -
>
> https://docs.python.org/3.10/library/email.examples.html

I'm reading it! :)

Regards.
^Bart

--
https://mail.python.org/mailman/listinfo/python-list
Re: How to read file content and send email on Debian Bullseye [ In reply to ]
> xdg-email appears to be for interactive use (it opens the user's
> "preferred email composer"); I think sendmail would work much better
> from a script.

Like what I said in another post I think I could use ssmtp than
xdg-email or sendmail...

> Otherwise, I had the same initial thought, to add to and/or build a
> wrapper around the existing lftp script.

I'd like to know if there's a code made from lftp to understand when an
upload file is finished but certainly I can read it from the log file
and I think it couldn't be hard to find a value in a *.txt file and if
this value is inside of it to send an email like "ok" otherwise a
message with "k.o.".

Regards.
^Bart
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to read file content and send email on Debian Bullseye [ In reply to ]
> I have never used it, and I did see that wording in the man page, but it
> also showed putting all the info on the command line - from, to,
> subject, etc - so I thought it might be able to use a command line
> client without needed any GUI interaction.  But I don't know for sure.

This machine is a server without GUI so I must do everything by cli but
it's not a problem! :)

So mainly I should write a code to find a match between the date found
on the lftp's log file and the date of the pc, if there's a match I need
to send an email with "ok" otherwise an email with "ko".

I think it shouldn't be so hard to compare a value inside of a file with
the date of the machine but now I should understand which is the best
way! :)

Regards.
Gabriele

--
https://mail.python.org/mailman/listinfo/python-list
Re: RE: How to read file content and send email on Debian Bullseye [ In reply to ]
> For example, try to do whatever parts you know how to do and when some part
> fails or is missing, ask.

You're right but first of all I wrote what I'd like to do and if Python
could be the best choice about it! :)

> I might have replied to you directly if your email email address did not
> look like you want no SPAM, LOL!

Ahaha! I think you know what is spam and what is a reply\answer to a
post request so you can feel free to use also my email! :)

> The cron stuff is not really relevant and it seems your idea is to read a
> part or all of a log file, parse the lines in some way and find a line that
> either matches what you need or fail to find it. Either way you want to send
> an email out with an appropriate content.

You got the point!

> Which part of that do you not know how to do in python? Have you done some
> reading or looking?

Like what I wrote above I didn't know if Python can does what I need and
if to use Python is a good way I'll start to study how to do it! :)

In my past I used Python for Arduino programming or to do easy things,
what I should do now is little more complex but I understood from years
and years by the Python's powers you can do everything! LOL! :)

Regards.
^Bart
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to read file content and send email on Debian Bullseye [ In reply to ]
On 2023-02-04 17:59:11 -0500, Thomas Passin wrote:
> On 2/4/2023 10:05 AM, ^Bart wrote:
> > On a Debian Bullseye server I have a lftp upload and after it I should
> > send an email.
> >
> > I thought to read the lftp log file where I have these lines:
[...]
> > I'd like to use Python to check, from monday to friday (the lftp script
> > runs in crontab from monday to friday) when the upload works is finished
> > and I should send an email.
> >
> > I could read by Python lftp.log and after it if there's a line with the
> > same day of the machine I could send an email with ok otherwise the
> > email will send a message with "no upload".
> >
> > How could I do by Python?
>
> Not Python, but you could run a shell script

If you can run a shell script you can also run a python script.
Linux/Unix is almost completely agnostic about interpreters. You
generally just invoke "the program" and then the kernel examines the
first few bytes to determine what to do with it.

> that sends the file with lftp, and depending on the return code uses
> xdg-email to send one of the two messages. This would probably be
> simpler than using Python.

Probably. But Python gives you more control. Tradeoffs, tradeoffs ...

For something like this I'd say go with what you're most familiar with
and what takes the least effort to get the job done.
For a simple one line notification I'd write a shell script invoking
/usr/sbin/sendmail. If I wanted it to be a bit fancier (e.g. a table
with the uploads over the last few days) I'd probably write a Python
script to get the MIME/HTML structure right (and still use sendmail to
actually send the mail).

xdg-mail can be anything or not be configured at all - so it might not
be the best choice.

> Otherwise you haven't said what part of the process you need help with.

Yeah, that was the reason I didn't reply to the original mail. I simply
couldn't figure out where the problem was.

hp

--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
Re: How to read file content and send email on Debian Bullseye [ In reply to ]
On 2023-02-05, ^Bart <gabriele1NOSPAM@hotmail.com> wrote:
>> For example, try to do whatever parts you know how to do and when some part
>> fails or is missing, ask.
>
> You're right but first of all I wrote what I'd like to do and if Python
> could be the best choice about it! :)

I'd say you want a simple shell script wrapped around your job, and a
program to send email (bsdmail/sendmail or similar or mstmp on linux for
instance).

>
>> I might have replied to you directly if your email email address did not
>> look like you want no SPAM, LOL!
>
> Ahaha! I think you know what is spam and what is a reply\answer to a
> post request so you can feel free to use also my email! :)
>
>> The cron stuff is not really relevant and it seems your idea is to read a
>> part or all of a log file, parse the lines in some way and find a line that
>> either matches what you need or fail to find it. Either way you want to send
>> an email out with an appropriate content.
>
> You got the point!
>
>> Which part of that do you not know how to do in python? Have you done some
>> reading or looking?
>
> Like what I wrote above I didn't know if Python can does what I need and
> if to use Python is a good way I'll start to study how to do it! :)
>
> In my past I used Python for Arduino programming or to do easy things,
> what I should do now is little more complex but I understood from years
> and years by the Python's powers you can do everything! LOL! :)
>
> Regards.
> ^Bart
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to read file content and send email on Debian Bullseye [ In reply to ]
On 2023-02-05, ^Bart <gabriele1NOSPAM@hotmail.com> wrote:
>> xdg-email appears to be for interactive use (it opens the user's
>> "preferred email composer"); I think sendmail would work much better
>> from a script.
>
> Like what I said in another post I think I could use ssmtp than
> xdg-email or sendmail...
>
>> Otherwise, I had the same initial thought, to add to and/or build a
>> wrapper around the existing lftp script.
>
> I'd like to know if there's a code made from lftp to understand when an
> upload file is finished

You make an lftp "script" to upload the file and write a log somewhere -
write a script to run lftp with the lftp script, when the lftp has
finished email the log. The log will show what happenned.

In cron run the script.

> but certainly I can read it from the log file
> and I think it couldn't be hard to find a value in a *.txt file and if
> this value is inside of it to send an email like "ok" otherwise a
> message with "k.o.".
>
> Regards.
> ^Bart
--
https://mail.python.org/mailman/listinfo/python-list
RE: RE: How to read file content and send email on Debian Bullseye [ In reply to ]
Bart,

Some really decent cron jobs can be written without using anything complex.

I get it now that perhaps your motivation is more about finding an excuse
to learn python better. The reality is there is not much that python cannot
do if other programming languages and environments can do them so asking if
it can do it feels a tad na?ve. Like many languages, python is very
extendable with all kinds of modules so often instead of doing something
totally on your own, you can find something that does much of the hard work
for you, too.

Yes, python can nicely read in lines from a log and compare them to a fixed
string or pattern and either find or not find what you ask for.

But a shell script can be written in a few minutes that simply gets the
string for the current date in the format you want, interpolates it in a
regular expression, calls grep or a dozen other programs that handle a
command line argument, and if it returns a single line, you send one email
and if none you send another and if more than one, you may have done it
wrong. Some such programs, like AWK are designed mainly to do exactly
something like you ask and examine each input line against a series of
patterns. Sending an email though is not always something easy to do from
within a program like that but a shell script that checks how it ends may do
that part.

If you are on a specific machine and only need to solve the problem on that
machine or something similar, this seems straightforward.

My impression is you want to match a line in the log file that may look like
it should match "anything", then some keyword or so that specifies all
lines about this particular upload on every day, then matches another
"anything" up to something that exactly matches the date for today, and
maybe match another "anything" to the end of the line. It can be a fairly
straightforward regular expression if the data has a regular component in
the formatting. Grep, sed, awk, perl and others can do this and others.

Could you do this faster in python? Maybe. Arguably if speed is a major
issue, write it in some compiled language like C++.

But if your data is very regular such as the entry will have some key phrase
between the 12th and 19th characters and the date will be exactly in another
exact region, then you certainly can skip regular expressions and read each
line and examine the substrings for equality. You may also speed it a bit if
you exit any such loop as soon as you find what you are looking for.

I note if your log file is big but not very busy, and you are pretty sure
the entry will be in the last few (maybe hundred) lines, some may use the
tail command and pipe the text it returns to whatever is processing the
data. There are many ways to do what you want.

But you improve your chances of getting an answer if you ask it more
clearly. There have been people (maybe just one) who have been posing
questions of a rather vague nature and then not responding as others debate
it in seemingly random directions. You are interacting nicely but some of us
have become hesitant to jump in until they see if the request is
well-intended. You do sound like you know quite a bit and your question
could have been closer to saying that you know several ways to do it
(include examples or at least outlines) and wonder if some ways are better
or more pythonic or ...

So if people keep wondering what you want, it is because the people here are
not generally interested in doing homework or complete programs for people.
If you ask us how to generate a string with the current date, and cannot
just find it on your own, we might answer. If you want to know how to store
a date as an object including the current time, and also convert the text on
a line in the log file to make another such date object and then be able to
compare them and be able to include in your email how LONG AGO the upload
was done, that would be another more specific request. If you are not sure
how python does regular expressions, ...

Otherwise, what you are asking for may not be BASIC for some but seems
relatively straightforward to some of us and we sort of may be wondering
if we are missing anything?

Good Luck,

^Avi

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of ^Bart
Sent: Sunday, February 5, 2023 8:58 AM
To: python-list@python.org
Subject: Re: RE: How to read file content and send email on Debian Bullseye

> For example, try to do whatever parts you know how to do and when some
> part fails or is missing, ask.

You're right but first of all I wrote what I'd like to do and if Python
could be the best choice about it! :)

> I might have replied to you directly if your email email address did
> not look like you want no SPAM, LOL!

Ahaha! I think you know what is spam and what is a reply\answer to a post
request so you can feel free to use also my email! :)

> The cron stuff is not really relevant and it seems your idea is to
> read a part or all of a log file, parse the lines in some way and find
> a line that either matches what you need or fail to find it. Either
> way you want to send an email out with an appropriate content.

You got the point!

> Which part of that do you not know how to do in python? Have you done
> some reading or looking?

Like what I wrote above I didn't know if Python can does what I need and if
to use Python is a good way I'll start to study how to do it! :)

In my past I used Python for Arduino programming or to do easy things, what
I should do now is little more complex but I understood from years and years
by the Python's powers you can do everything! LOL! :)

Regards.
^Bart
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list