Planet Collab

🔒
❌ About FreshRSS
There are new available articles, click to refresh the page.
Before yesterdayYour RSS feeds

Docker (Python 3) – Send emails from python script using (Ubuntu 20.04) Sendmail

By italchemy

This is the same send email solution as the previous post, but now you are installing sendmail on Ubuntu 20.04 LTS Docker image and sending emails from Docker.

 

# Step 1: Dockerfile #####################

#Dockerfile

FROM ubuntu:latest
MAINTAINER Brendan Choi, pynetauto@gmail.com

# Sets the timezone to your legion for correct time logs
ENV TZ=Australia/Sydney
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y –no-install-recommends tzdata && rm -rf /var/lib/apt/lists/*
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Install essential Linux packages
RUN apt-get update && apt-get install -y cron openssh-server nano tree iputils-ping traceroute
RUN apt install -y curl

RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
net-tools \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
sendmail \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Install Python related packages
RUN apt install -y python3 python3-pip
RUN pip3 install requests pandas scapy paramiko netmiko openpyxl xlsxwriter ansible nornir dpkt ansible colorama flask matplotlib xlrd XLsxWriter virtualenv

# SSH service configuration
RUN mkdir /var/run/sshd
RUN echo ‘root:Password’ | chpasswd
RUN sed -i ‘s/PermitRootLogin prohibit-password/PermitRootLogin yes/’ /etc/ssh/sshd_config
# SSH login fix. Otherwise user is kicked off after login
RUN sed ‘s@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g’ -i /etc/pam.d/sshd
ENV NOTVISIBLE “in users profile”
RUN echo “export VISIBLE=now” >> /etc/profile

######################################
# Copy hello-cron file to the cron.d directory
#COPY test-cron /etc/cron.d/test-cron
#COPY gday.py /home/gday.py

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/test-cron
# For scripts it is important to change the ownership to 0744.
RUN chmod 0744 /home/gday.py

RUN cron

# Apply cron job
RUN crontab /etc/cron.d/test-cron

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

# Run the command on container startup
CMD cron && tail -f /var/log/cron.log
######################################
ENV USER ftpuser
ENV PASS PassWord!

RUN apt-get install -y vsftpd supervisor && \
mkdir -p /var/run/vsftpd/empty

#RUN mkdir -p /var/log/supervisor

COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
ADD start.sh /usr/local/bin/start.sh
ADD vsftpd.conf /etc/vsftpd.conf

#RUN mkdir /ftp

VOLUME [“/ftp”]

EXPOSE 20 21 22 25
EXPOSE 12020 12021 12022 12023 12024 12025
ENTRYPOINT [“/usr/local/bin/start.sh”]
CMD [“/usr/bin/supervisord”]

 

# Step 2: Build a docker image from Dockerfile above #####################
[root@pynetauto pynetapps]# docker build –rm -t pynetauto/ubutu20_pycron:2 .
Sending build context to Docker daemon 1.669MB
Step 1/32 : FROM pynetauto/ubutu20_pycron:latest
—> 62bb16b0727a

[… ommitted for brevity]
Step 32/32 : CMD [“/usr/bin/supervisord”]
—> Running in 1bb649edb38d
Removing intermediate container 1bb649edb38d
—> 5af290bc552a
Successfully built 5af290bc552a
Successfully tagged pynetauto/ubutu20_pycron:2

# Step 3: Run the Docker ###############
[root@pynetauto pynetauto]# docker run -it –entrypoint=/bin/bash pynetauto/ubutu20_pycron:2

# Step 4: Check the ports on Docker ###############
root@ee7467603704:/# netstat -tuna
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State

# Step 5: Run sendmailconfi to open up the ports ###############
root@ee7467603704:/# sendmailconfig
Configure sendmail with the existing /etc/mail/sendmail.conf? [Y]
Reading configuration from /etc/mail/sendmail.conf.
Validating configuration.
Writing configuration to /etc/mail/sendmail.conf.
Writing /etc/cron.d/sendmail.
Configure sendmail with the existing /etc/mail/sendmail.mc? [Y]
Updating sendmail environment …
[… ommitted for brevity]
Updating /etc/mail/aliases…
WARNING: local host name (ee7467603704) is not qualified; see cf/README: WHO AM I?
cannot open: file /etc/mail/aliases changed after open
newaliases: cannot open /etc/mail/aliases: File changed after open
Reload the running sendmail now with the new configuration? [Y]
Reloading sendmail …

# Step 6: Check the port 25 again on the docker Docker ###############
root@ee7467603704:/# netstat -tuna
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:587 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN

# Step 7: From your Docker, send a quick test email. ###############
root@ee7467603704:/# echo “This is Docker test email 1.” | sendmail -v pynetauto3@gmail.com
WARNING: local host name (ee7467603704) is not qualified; see cf/README: WHO AM I?
pynetauto3@gmail.com… Connecting to [127.0.0.1] via relay…
220 ee7467603704 ESMTP Sendmail 8.15.2/8.15.2/Debian-18; Wed, 13 May 2020 15:31:33 +1000; (No UCE/UBE) logging access from: localhost(OK)-localhost [127.0.0.1]
>>> EHLO ee7467603704
250-ee7467603704 Hello localhost [127.0.0.1], pleased to meet you
250-ENHANCEDSTATUSCODES
[… ommitted for brevity]
050 >>> .
050 250 2.0.0 OK 1589348225 z18si11403227pgv.292 – gsmtp
050 <pynetauto3@gmail.com>… Sent (OK 1589348225 z18si11403227pgv.292 – gsmtp)
250 2.0.0 04D5VXI5000644 Message accepted for delivery
pynetauto3@gmail.com… Sent (04D5VXI5000644 Message accepted for delivery)
Closing connection to [127.0.0.1]
>>> QUIT
221 2.0.0 ee7467603704 closing connection

# Step 8: From your email account, check email. ###############
Email cofnirmed!

# Step 9: create a Python email script, send_e1.py ###############
root@ee7467603704:/pynetauto# nano send_e1.py

import smtplib

sender = ‘no_reply@italchemy.com’
receivers = [‘pynetauto3@gmail.com’]

message = “””From: No Reply <no_reply@italchemy.com>
To: Brendan Choi <pynetauto3@gmail.com>
Subject: Sendmail SMTP Email test 001

This is a Sendmail test e-mail message 1.
“””

try:
smtpObj = smtplib.SMTP(‘localhost’)
smtpObj.sendmail(sender, receivers, message)
print(“Successfully sent email”)
except SMTPException:
print(“Error: unable to send email”)

send_e1-2
# Step 10: From docker, send email running the script ###############
root@ee7467603704:/pynetauto# python3 send_e1.py
Successfully sent email
# Step 11: From your email account, check email. ###############
Email cofnirmed!

Docker (Python 3) – Send emails from python script using (Ubuntu 20.04) Sendmail

By italchemy

This is the same send email solution as the previous post, but now you are installing sendmail on Ubuntu 20.04 LTS Docker image and sending emails from Docker.

 

# Step 1: Dockerfile #####################

#Dockerfile

FROM ubuntu:latest
MAINTAINER Brendan Choi, pynetauto@gmail.com

# Sets the timezone to your legion for correct time logs
ENV TZ=Australia/Sydney
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y –no-install-recommends tzdata && rm -rf /var/lib/apt/lists/*
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Install essential Linux packages
RUN apt-get update && apt-get install -y cron openssh-server nano tree iputils-ping traceroute
RUN apt install -y curl

RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
net-tools \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
sendmail \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Install Python related packages
RUN apt install -y python3 python3-pip
RUN pip3 install requests pandas scapy paramiko netmiko openpyxl xlsxwriter ansible nornir dpkt ansible colorama flask matplotlib xlrd XLsxWriter virtualenv

# SSH service configuration
RUN mkdir /var/run/sshd
RUN echo ‘root:Password’ | chpasswd
RUN sed -i ‘s/PermitRootLogin prohibit-password/PermitRootLogin yes/’ /etc/ssh/sshd_config
# SSH login fix. Otherwise user is kicked off after login
RUN sed ‘s@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g’ -i /etc/pam.d/sshd
ENV NOTVISIBLE “in users profile”
RUN echo “export VISIBLE=now” >> /etc/profile

######################################
# Copy hello-cron file to the cron.d directory
#COPY test-cron /etc/cron.d/test-cron
#COPY gday.py /home/gday.py

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/test-cron
# For scripts it is important to change the ownership to 0744.
RUN chmod 0744 /home/gday.py

RUN cron

# Apply cron job
RUN crontab /etc/cron.d/test-cron

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

# Run the command on container startup
CMD cron && tail -f /var/log/cron.log
######################################
ENV USER ftpuser
ENV PASS PassWord!

RUN apt-get install -y vsftpd supervisor && \
mkdir -p /var/run/vsftpd/empty

#RUN mkdir -p /var/log/supervisor

COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
ADD start.sh /usr/local/bin/start.sh
ADD vsftpd.conf /etc/vsftpd.conf

#RUN mkdir /ftp

VOLUME [“/ftp”]

EXPOSE 20 21 22 25
EXPOSE 12020 12021 12022 12023 12024 12025
ENTRYPOINT [“/usr/local/bin/start.sh”]
CMD [“/usr/bin/supervisord”]

 

# Step 2: Build a docker image from Dockerfile above #####################
[root@pynetauto pynetapps]# docker build –rm -t pynetauto/ubutu20_pycron:2 .
Sending build context to Docker daemon 1.669MB
Step 1/32 : FROM pynetauto/ubutu20_pycron:latest
—> 62bb16b0727a

[… ommitted for brevity]
Step 32/32 : CMD [“/usr/bin/supervisord”]
—> Running in 1bb649edb38d
Removing intermediate container 1bb649edb38d
—> 5af290bc552a
Successfully built 5af290bc552a
Successfully tagged pynetauto/ubutu20_pycron:2

# Step 3: Run the Docker ###############
[root@pynetauto pynetauto]# docker run -it –entrypoint=/bin/bash pynetauto/ubutu20_pycron:2

# Step 4: Check the ports on Docker ###############
root@ee7467603704:/# netstat -tuna
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State

# Step 5: Run sendmailconfi to open up the ports ###############
root@ee7467603704:/# sendmailconfig
Configure sendmail with the existing /etc/mail/sendmail.conf? [Y]
Reading configuration from /etc/mail/sendmail.conf.
Validating configuration.
Writing configuration to /etc/mail/sendmail.conf.
Writing /etc/cron.d/sendmail.
Configure sendmail with the existing /etc/mail/sendmail.mc? [Y]
Updating sendmail environment …
[… ommitted for brevity]
Updating /etc/mail/aliases…
WARNING: local host name (ee7467603704) is not qualified; see cf/README: WHO AM I?
cannot open: file /etc/mail/aliases changed after open
newaliases: cannot open /etc/mail/aliases: File changed after open
Reload the running sendmail now with the new configuration? [Y]
Reloading sendmail …

# Step 6: Check the port 25 again on the docker Docker ###############
root@ee7467603704:/# netstat -tuna
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:587 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN

# Step 7: From your Docker, send a quick test email. ###############
root@ee7467603704:/# echo “This is Docker test email 1.” | sendmail -v pynetauto3@gmail.com
WARNING: local host name (ee7467603704) is not qualified; see cf/README: WHO AM I?
pynetauto3@gmail.com… Connecting to [127.0.0.1] via relay…
220 ee7467603704 ESMTP Sendmail 8.15.2/8.15.2/Debian-18; Wed, 13 May 2020 15:31:33 +1000; (No UCE/UBE) logging access from: localhost(OK)-localhost [127.0.0.1]
>>> EHLO ee7467603704
250-ee7467603704 Hello localhost [127.0.0.1], pleased to meet you
250-ENHANCEDSTATUSCODES
[… ommitted for brevity]
050 >>> .
050 250 2.0.0 OK 1589348225 z18si11403227pgv.292 – gsmtp
050 <pynetauto3@gmail.com>… Sent (OK 1589348225 z18si11403227pgv.292 – gsmtp)
250 2.0.0 04D5VXI5000644 Message accepted for delivery
pynetauto3@gmail.com… Sent (04D5VXI5000644 Message accepted for delivery)
Closing connection to [127.0.0.1]
>>> QUIT
221 2.0.0 ee7467603704 closing connection

# Step 8: From your email account, check email. ###############
Email cofnirmed!

# Step 9: create a Python email script, send_e1.py ###############
root@ee7467603704:/pynetauto# nano send_e1.py

import smtplib

sender = ‘no_reply@italchemy.com’
receivers = [‘pynetauto3@gmail.com’]

message = “””From: No Reply <no_reply@italchemy.com>
To: Brendan Choi <pynetauto3@gmail.com>
Subject: Sendmail SMTP Email test 001

This is a Sendmail test e-mail message 1.
“””

try:
smtpObj = smtplib.SMTP(‘localhost’)
smtpObj.sendmail(sender, receivers, message)
print(“Successfully sent email”)
except SMTPException:
print(“Error: unable to send email”)

send_e1-2
# Step 10: From docker, send email running the script ###############
root@ee7467603704:/pynetauto# python3 send_e1.py
Successfully sent email
# Step 11: From your email account, check email. ###############
Email cofnirmed!

Running Docker FTP server for IOS upgrade – a working test run~!

By italchemy

Docker FTP server on Ubuntu 18 server:

Prerequsite: Install docker on Ubuntu 18 server

Then pull the image from Docker/hub
$ docker pull gspeedy101/pynet-ftp:0.1
Pull “pynet-ftp latest 8c2ae1c4ea04 3 days ago 175MB” image from Docker/hub

Run docker FTP server with a full command as below.

User ID is ftp

Password is ftpftpftp

Target file to copy. I have mapped the /tmp directory on the host Ubuntu server to /home/vsftpd of the Docker FTP Container.

root@ubuntu:/tmp# ls /tmp/test*
/tmp/testfile1.txt

 

The most important command!!!
root@ubuntu:~# docker run -d -v /tmp:/home/vsftpd -p 20:20 -p 21:21 -p 47400-47470:47400-47470 -e FTP_USER=ftp -e FTP_PASS=ftpftpftp -e PASV_ADDRESS=192.168.185.3 pynet-ftp:latest

 

The second most important command:
Run the copy ftp command from your router or switch:

PytMelCT1-Sw02#copy ftp://ftp:ftpftpftp@192.168.185.3/testfile1.txt flash:/testfile1.txt flash:/testfile1.txt
Destination filename [testfile1.txt]?
Accessing ftp://ftp:ftpftpftp@192.168.185.3/testfile1.txt…
Loading testfile1.txt
[OK – 31/4096 bytes]

31 bytes copied in 0.067 secs (463 bytes/sec)
As you can see testfile1.txt has been downloaded to switch flash:

PytMelCT1-Sw02#show flash

Directory of flash:/

2 -rwx 556 Mar 1 1993 00:01:16 +00:00 vlan.dat
3 -rwx 12749374 Mar 2 1993 01:54:15 +00:00 c3560-ipservicesk9-mz.122-55.SE10.bin
4 -rwx 12752928 Mar 1 1993 00:20:25 +00:00 c3560-ipservicesk9-mz.122-55.SE5.bin
5 -rwx 5986 Sep 11 2015 05:47:18 +00:00 config.old
6 -rwx 31 Mar 1 1993 01:37:37 +00:00 testfile1.txt
7 -rwx 1768 Mar 1 1993 01:27:56 +00:00 config.text
8 -rwx 2967 Mar 1 1993 01:27:56 +00:00 private-config.text
9 -rwx 2072 Mar 2 1993 01:56:57 +00:00 multiple-fs

32514048 bytes total (6993920 bytes free)

Checking the downloaded file
PytMelCT1-Sw02#more testfile1.txt
dkdkdkg
d
skdkdkdkd
blabla
bla

PytMelCT1-Sw02#

Running Docker FTP server for IOS upgrade – a working test run~!

By italchemy

Docker FTP server on Ubuntu 18 server:

Prerequsite: Install docker on Ubuntu 18 server

Then pull the image from Docker/hub
$ docker pull gspeedy101/pynet-ftp:0.1
Pull “pynet-ftp latest 8c2ae1c4ea04 3 days ago 175MB” image from Docker/hub

Run docker FTP server with a full command as below.

User ID is ftp

Password is ftpftpftp

Target file to copy. I have mapped the /tmp directory on the host Ubuntu server to /home/vsftpd of the Docker FTP Container.

root@ubuntu:/tmp# ls /tmp/test*
/tmp/testfile1.txt

 

The most important command!!!
root@ubuntu:~# docker run -d -v /tmp:/home/vsftpd -p 20:20 -p 21:21 -p 47400-47470:47400-47470 -e FTP_USER=ftp -e FTP_PASS=ftpftpftp -e PASV_ADDRESS=192.168.185.3 pynet-ftp:latest

 

The second most important command:
Run the copy ftp command from your router or switch:

PytMelCT1-Sw02#copy ftp://ftp:ftpftpftp@192.168.185.3/testfile1.txt flash:/testfile1.txt flash:/testfile1.txt
Destination filename [testfile1.txt]?
Accessing ftp://ftp:ftpftpftp@192.168.185.3/testfile1.txt…
Loading testfile1.txt
[OK – 31/4096 bytes]

31 bytes copied in 0.067 secs (463 bytes/sec)
As you can see testfile1.txt has been downloaded to switch flash:

PytMelCT1-Sw02#show flash

Directory of flash:/

2 -rwx 556 Mar 1 1993 00:01:16 +00:00 vlan.dat
3 -rwx 12749374 Mar 2 1993 01:54:15 +00:00 c3560-ipservicesk9-mz.122-55.SE10.bin
4 -rwx 12752928 Mar 1 1993 00:20:25 +00:00 c3560-ipservicesk9-mz.122-55.SE5.bin
5 -rwx 5986 Sep 11 2015 05:47:18 +00:00 config.old
6 -rwx 31 Mar 1 1993 01:37:37 +00:00 testfile1.txt
7 -rwx 1768 Mar 1 1993 01:27:56 +00:00 config.text
8 -rwx 2967 Mar 1 1993 01:27:56 +00:00 private-config.text
9 -rwx 2072 Mar 2 1993 01:56:57 +00:00 multiple-fs

32514048 bytes total (6993920 bytes free)

Checking the downloaded file
PytMelCT1-Sw02#more testfile1.txt
dkdkdkg
d
skdkdkdkd
blabla
bla

PytMelCT1-Sw02#

Cisco & Microsoft: Making Containerized Apps Easier

By Kip Compton

Containers are an important technology for customers in their efforts to accelerate application innovation. At Cisco, we’ve been working across our ecosystem to offer solutions to help our customers deploy and manage container infrastructure and applications across on-premises and our partner’s clouds.

I’m pleased to share progress on our collaboration with Microsoft to make it easier for customers to rapidly — and at scale — deploy and run containerized applications across both Cisco-based on-premises environments and Microsoft Azure.

Today, I am happy to announce the addition of Azure Kubernetes Service (AKS) to the Kubernetes managed services that natively integrate with the Cisco Container Platform. This integration means that customers can now further simplify deploying and managing Kubernetes clusters on-premises and in AKS with one tool, using common identity and control policies, reducing manual tasks and ultimately time-to-market for their application environments.

With the integration of Microsoft AKS and the Cisco Container Platform customers can now further simplify deploying and managing Kubernetes clusters on-premises and in AKS with one tool, using common identity and control policies, reducing manual tasks and ultimately time-to-market for their application environments.

In future phases, the Cisco Container Platform will integrate functionality to support Microsoft Windows container applications with the potential to leverage virtual-kubelet or Windows node pools in Azure. In addition, we will support Azure Active Directory common identity integration for both on-prem and AKS clusters so customers and applications experience a single, consistent environment across hybrid cloud.

Both Microsoft and Cisco customers are adopting Kubernetes to accelerate their DevOps plans, removing infrastructure constraints of traditional application stacks and unleashing innovation anywhere, from on-prem to the edge, to the cloud. As customers choose to deploy Kubernetes and hybrid strategies, both Cisco and Microsoft are excited about the expansion of our growing collaboration to deliver this new integration.

Cisco is committed to helping customers maximize the value of containers to enable them to do more with their existing and new applications, using the best of what Microsoft Windows and Azure have to offer, with Cisco enterprise-class consistency and security built-in.

It’s exciting to see another milestone in our journey to develop solutions that help our customers innovate. You’ll continue seeing us work with our partners to offer choice across on-premises and public clouds.

Stay tuned for more!

 

Cisco and AWS Collaborate to Accelerate Innovation

By Kip Compton
Cisco is introducing a new solution built for Amazon Web Services (AWS) that makes running new containerized applications simple.

Power a Multicloud World – Enabling Next-Generation Hybrid Cloud with Google Cloud

By Kip Compton
The pressure to innovate has never been greater to compete in a rapidly evolving marketplace. Hybrid cloud offers one of the biggest IT transformations of our generation.

Setting a New Standard for Production-Grade Kubernetes

By Kip Compton
Cisco's CloudCenter 4.9 and AppDynamics for Kubernetes illustrate our commitment to open source technologies that help our customers adopt at scale, in production, on premises, and in cloud environments.

Cisco Announces Intent to Acquire ContainerX

By Rob Salvagno
I’m excited to share that today Cisco announced its intent to acquire ContainerX, Inc., Cisco’s first acquisition in the rapidly emerging container market. ContainerX is an early stage, privately held company focused on building technology to help enterprises manage, orchestrate and integrate containers across data centers. Many of our customers are starting to use containers […]
❌