Hallo und guten Morgen,
so nach unendlichen Stunden der Suche schreibe ich jetzt hier und bitte um Hilfe.
Ich möchte das mein Raspi mir wenn eine Netzwerkverbindung zustande kommt eine E-Mail sendet mit der aktuellen IP Nummer. Dazu habe ich ein Python Script.
E-Mail senden mit Python aus der Shell mit Anhang funktioniert.
Nur wenn ich es per ifupdown auslösen will startet das Python Script nicht.
Ich verstehe aber nicht warum nicht.....
Der Ablauf ist ifupdown ruft ein Bash Script auf in dem wird die IP in eine Dateigeschrieben.
Das funktioniert auch also ifupdown ruft mein Bash Script auf.
Aber das Anschließende Python Script wird nicht abgearbeiet.
Wenn ich in die nano /var/log/syslog schau dann bekomme ich folgendes raus
Aug 1 10:30:06 raspcam1 kernel: [39113.114073] smsc95xx 1-1.1:1.0 eth0: link up, 100Mbps, full-duplex, lpa 0xC5E1
Aug 1 10:30:06 raspcam1 dhcpcd[2264]: eth0: carrier acquired
Aug 1 10:30:06 raspcam1 dhcpcd[2264]: all: IPv6 kernel autoconf disabled
Aug 1 10:30:06 raspcam1 dhcpcd[2264]: eth0: IAID eb:7d:08:15
Aug 1 10:30:06 raspcam1 dhcpcd[2264]: eth0: rebinding lease of 192.168.2.18
Aug 1 10:30:06 raspcam1 ifplugd(eth0)[1605]: Link beat detected.
Aug 1 10:30:07 raspcam1 ifplugd(eth0)[1605]: Executing '/etc/ifplugd/ifplugd.action eth0 up'.
Aug 1 10:30:07 raspcam1 ifplugd(eth0)[1605]: client: Traceback (most recent call last):
Aug 1 10:30:07 raspcam1 ifplugd(eth0)[1605]: client: File "/home/pi/scripte/email/mail.py", line 63, in <module>
Aug 1 10:30:07 raspcam1 ifplugd(eth0)[1605]: client: server.send(sys.argv[1:])
Aug 1 10:30:07 raspcam1 ifplugd(eth0)[1605]: client: File "/home/pi/scripte/email/mail.py", line 33, in send
Aug 1 10:30:07 raspcam1 ifplugd(eth0)[1605]: client: server = smtplib.SMTP(self.smtpserver)
Aug 1 10:30:07 raspcam1 ifplugd(eth0)[1605]: client: File "/usr/lib/python2.7/smtplib.py", line 249, in __init__
Aug 1 10:30:07 raspcam1 ifplugd(eth0)[1605]: client: (code, msg) = self.connect(host, port)
Aug 1 10:30:07 raspcam1 ifplugd(eth0)[1605]: client: File "/usr/lib/python2.7/smtplib.py", line 309, in connect
Aug 1 10:30:07 raspcam1 ifplugd(eth0)[1605]: client: self.sock = self._get_socket(host, port, self.timeout)
Aug 1 10:30:07 raspcam1 ifplugd(eth0)[1605]: client: File "/usr/lib/python2.7/smtplib.py", line 284, in _get_socket
Aug 1 10:30:07 raspcam1 ifplugd(eth0)[1605]: client: return socket.create_connection((port, host), timeout)
Aug 1 10:30:07 raspcam1 ifplugd(eth0)[1605]: client: File "/usr/lib/python2.7/socket.py", line 553, in create_connection
Aug 1 10:30:07 raspcam1 ifplugd(eth0)[1605]: client: for res in getaddrinfo(host, port, 0, SOCK_STREAM):
Aug 1 10:30:07 raspcam1 ifplugd(eth0)[1605]: client: socket.gaierror: [Errno -2] Name or service not known
Aug 1 10:30:07 raspcam1 ifplugd(eth0)[1605]: client: run-parts: /etc/ifplugd/action.d//ifupdown exited with return code 1
Aug 1 10:30:07 raspcam1 ifplugd(eth0)[1605]: Program execution failed, return value is 1.
Leider werde ich daraus nicht schlau (bin noch nicht so erfahren)
Hier mal mein Python Script und das Bash Script. Wie schon geschrieben das Bash Script s.sh wird aufgerufen sobald eine IP Adresse vom Raspberry bezogen wurde.
#!/bin/sh
set -e
case "$2" in
up)
/sbin/ifup $1
#/etc/ifplugd/action.d/ipsend.sh
/home/pi/scripte/email/s.sh
;;
down)
/sbin/ifdown $1
;;
esac
Alles anzeigen
Bash Script s.sh welches durch ifupdown aufgerufen wird.
#!/bin/sh
set -e
case "$2" in
up)
/sbin/ifup $1
#/etc/ifplugd/action.d/ipsend.sh
/home/pi/scripte/email/s.sh
;;
down)
/sbin/ifdown $1
;;
esac
Alles anzeigen
Python Scrip zum senden der E-Mail mit Anhang
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, smtplib, os, time
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import formatdate
from email import Encoders
class SendMail(object):
mailadress = 'xxxxxx@xxxxxx.de'
smtpserver = 'xxxxxxxxxxxxxxxxxxx:587'
username = 'xxxxxxxxxxxxxxxx'
password = 'xxxxxxxxxxxxxxxxxxxx'
def send(self, files):
to = self.mailadress
From = self.mailadress
if len(files) <= 3: subjAdd = ','.join(files)
if len(files) > 3: subjAdd = ','.join(files[:3]) + '...'
subject = time.strftime("Alarmmeldung vom: %d.%m.%Y um %H:%M:%S")
msg = self.prepareMail(From, to, subject, files)
server = smtplib.SMTP(self.smtpserver)
server.ehlo()
server.starttls() # Use encrypted SSL mode
server.ehlo()
server.login(self.username, self.password)
failed = server.sendmail(From, to, msg.as_string())
server.quit()
def prepareMail(self, From, to, subject, attachments):
msg = MIMEMultipart()
msg['From'] = From
msg['To'] = to
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.set_charset("utf-8")
msg.attach( MIMEText("") )
for file in attachments:
#We could check for mimetypes here, but I'm too lazy
part = MIMEBase('application', "octet-stream")
part.set_payload( open(file,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file))
msg.attach(part)
#Delete created Tar
return msg
server = SendMail()
server.send(sys.argv[1:])
Alles anzeigen
Ich danke euch allen schon mal für eure Mühe.
Gruß
Michael