Categories
Linux Python RedHat Technology

How to send emails with Python 3

There are some situations in which System Admins must send emails from their servers. For those who are working with the newest Linux versions, Python version 3 has become its default. In this article, I will share how to send SMTP authenticated emails by using a Python 3 script.

The following script uses both libraries “smtplib” to establish the connection with the server, and “sys” to obtain parameters from the command line. In this case, the parameter “-a” is used to attach a text file to be sent, and the parameter “-s” is used to set the subject of the message. You should change the lines below for your environment.

1 – Set your mail server hostname and port.

mailobj = smtplib.SMTP('myserver.com',587)

2 – Username and password.

mailobj.login('user@myserver.com','secret')

3 – Correspondent and recipient.

sender = 'from@myserver.com'
receivers = 'to@myserver.com'

4 – This is the correct way to run the script:

./send-email.py -a "/some/text/file" -s "subject"

Finally, here is the script source code:

#!/usr/bin/python3

import smtplib, sys

param1 = sys.argv[1]
param2 = sys.argv[3]

if "-a" in param1:
    attached_file = sys.argv[2]
    f = open(attached_file, "r")
    body_text = f.read()

if "-s" in param2:
    subject = sys.argv[4]

mailobj = smtplib.SMTP('myserver.com',587)
mailobj.ehlo()
mailobj.starttls()
mailobj.login('user@myserver.com','secret')

sender = 'from@myserver.com'
receivers = 'to@myserver.com'
message = """From: FROM
To: TO
Subject: SUBJECT

TEXT
"""
message = message.replace("FROM",sender)
message = message.replace("TO",receivers)
message = message.replace("SUBJECT",subject)
message = message.replace("TEXT",body_text)

mailobj.sendmail(sender, receivers, message)
mailobj.quit()

Categories
Linux RedHat Technology

Setting up Centos / RedHat 8 extra repositories

One of the first tasks that system admins should do when they install a new Linux machine is to make sure they have a reliable repository to get the software they need.

Here, following some sources that I consider essential to obtain RPM packages.

1 – Install both those extra repositories epel and remi:

# yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
# yum -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm

2 – Install the package responsible for handling the yum configuration and enabling the repositories:

# yum -y install yum-utils
# yum config-manager --set-enabled powertools
# yum config-manager --set-enabled remi
# yum config-manager --set-enabled epel

3 – Now, you can update your Linux:

# yum update
Categories
Linux Network RedHat Technology

Using Tcptrack on Centos 8

This tutorial shows how to use the Tcptrack package on Centos 8, which does not have this RPM in its repository.

The Tcptrack is a console tool that displays the TCP connections that are currently active. It is also useful to see the amount of data and traffic that each connection consumes. Its usage is quite similar to the Tcpdump command.

To install this package on your Centos or RedHat version 8, follow the steps below. You should be logged in with root privileges.

1 – Enable the Powetools repository and Install both the RPM builder and those packages’ dependencies:

# yum config-manager --set-enabled powertools
# yum -y install rpm-build libpcap-devel gcc-c++ ncurses-devel make

2 – Install the Tcptrack source package:

# rpm -ivh http://packages.psychotic.ninja/7/base/SRPMS/tcptrack-1.4.3-7.el7.psychotic.src.rpm

3 – Now, go to the SPECS directory and build the new package:

# cd /root/rpmbuild/SPECS
# rpmbuild -ba tcptrack.spec

4 – It is done! You can install the package that was just created:

# rpm -ivh /root/rpmbuild/RPMS/x86_64/tcptrack-1.4.3-7.el8.x86_64.rpm

5 – There is a simple example of how to use it:

# tcptrack -i eth0
Categories
Essay

Is the Internet the best place to get information?

In recent years, the internet has become people’s preferred communication system. Some individuals argue that this environment is not reliable when it comes to obtaining information. Although this is a democratic type of media, I believe that the lack of regulation tends to increase the amount of false information spread among its users.

The advancement of technology has brought many benefits to society as a whole. Nowadays, not only are mobile devices affordable, but also access to the network is cheap and available even in remote places. Therefore, the more people adopt the internet as their main source of news, the more media vehicles will publish their content on this platform. As a result, people from various social backgrounds are now able to consume information as previous generations could not imagine. Usually, people who are better informed make the best decisions in many aspects of their lives.

On the other hand, the internet was born without any regulatory system. The concept behind its creation was to provide a space in which every citizen could freely manifest his or her thoughts and beliefs. Because of this chaotic environment, many groups have been using the web as a tool to spread misinformation, conspiracy theories, and many other content that will manipulate the opinion of readers. To illustrate, recently, so-called fake news played a huge role in the United States elections. If voters had checked the validity of the information, they might have changed the way they voted. In other words, if internet users do not carefully check the source of what they are reading, they will become an easy target for those who want to spread biased messages as true information.

To sum up, this is a controversial topic, and there is not a simple measure to fix these issues. The system is hugely complex and involves a multitude of players such as tech companies, governments, legislators and political regimes. In summary, people should think twice after reading a piece of information from the internet.

Categories
Essay

What the causes of crimes are and how to prevent them

Annually, the number of criminals on the street increases. Criminal activity has countless causes, such as poverty and lack of education. By improving such social conditions, the government might be able to decrease violence on the streets. This essay will discuss this topic in detail.

Although the overall sense of safety in large cities has been increasing, those places still have neighborhoods in which criminals have territorial dominance. In such areas, the state cannot provide its inhabitants with even basic public services. The lack of schools, for example, is the main reason behind the use of youngsters as a workforce by criminal organizations. It is not rare to see drug dealers and cartels luring teenagers with guns, gold jewelry, and, consequently, power in order to recruit them. As a result, uneducated people might think it is easier to obtain social status and wealth as gang members. If the government wants to ameliorate this situation, it must invest heavily in education in these sensible regions.

Moreover, destitute individuals also have a tendency to commit more crimes than fortunate ones. Nowadays, the media publishes an astonishing number of advertisements on television, social media, and billboards, to name a few. People who cannot afford such items may eventually consider engaging in illegal activity to make money. In this case, their lack of moral values will play a huge role in this decision. However, if they have a job that provides them with enough money to buy things, they will not have to handle this dilemma.

In summary, this is a controversial subject that not even specialists have a silver-bullet solution to. However, it is clear that the more educated a population is, the less crime its people will experience on a daily basis. This field should be the number one priority of every public authority.

Categories
Linux Network RedHat

RedHat 8 Basic Network Settings

For those who have choose to not set up the network connection during the RedHat 8 installation process, it is quintessential doing that soon after the system performs a reboot. Otherwise, you might not be able to install remote packages, receiving nor sending information anywhere.

The following tutorial provides the very basic configuration to make such connectivity possible.

1 – Change your “hostname”. Just edit the file “/etc/hostname” and overwrite the machine’s full hostname.

2 – Define your IP address.

Considering that your system has recognized your interface as “ens160“, and you do not have a DHCP system available on LAN. Edit the file “/etc/sysconfig/network-scripts/ifcfg-ens160.

2.1 – Change the parameter BOOTPROTO from dhcp to static

2.2 – In the end of file, you should add two parameters: IPADDR=X.X.X.X (for example, 192.168.0.2) IPMASK=X.X.X.X (for example, 255.255.255.0)

3 – Now, edit the file “/etc/sysconfig/network” to set up your default gateway. Add the following parameter: GATEWAY=X.X.X.X (for example, 192.168.0.1)

4 – To make this changes effective, type the commands below:

# ifdown ens160 ; ifup ens160

5 – You can check if those settings were rightly applied.

# ifconfig ens160
ens160: flags=4163 mtu 1500
inet 192.168.0.2 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 fe80::289b:a5a7:1282:9065 prefixlen 64 scopeid 0x20
inet6 2804:14c:5bb2:8e8f:72ff:9ed2:b6d9:81b4 prefixlen 64 scopeid 0x0
ether 00:0c:29:9a:b6:0e txqueuelen 1000 (Ethernet)
RX packets 611 bytes 71365 (69.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 569 bytes 67271 (65.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 ens160
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens160

6 – Finally, test if you are able to get a “ping” response from an public internet address.

# ping -c 3 1.1.1.1
PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
64 bytes from 1.1.1.1: icmp_seq=1 ttl=55 time=22.6 ms
64 bytes from 1.1.1.1: icmp_seq=2 ttl=55 time=22.6 ms
64 bytes from 1.1.1.1: icmp_seq=3 ttl=55 time=20.8 ms

If the IP address that you already set up has fully access to the Internet beyond the gateway, you will be able to get the correct answer from the destiny.

--- 1.1.1.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 6ms
rtt min/avg/max/mdev = 20.785/21.971/22.571/0.847 ms

Categories
Linux RedHat Technology

Changing System Runlevel on RedHat 8

Most of Centos, Fedora, and RedHat previous users are used to set Runlevel by changing the file “/etc/inittab”, however, the newest version of the Red Hat OS (version 8) changed the way how it is set.

You should use the following commands to perform this change in each scenario.

Multi-user terminal mode (alike to runlevel 3)

# systemctl set-default multi-user.target

Graphical mode (alike to runlevel 5)

# systemctl set-default graphical.target
Categories
Asterisk Linux

Recording and mixing Asterisk’s calls

One of the most valuable features of the Asterisk PBX server is its recording support. However, before playing the audio files, we must merge both sides of the recorded call. This article was written based on Asterisk version 16 and Sox v14.4.1.

The first step is enabling this feature in the extensions file /etc/asterisk/extensions.conf. The application used for recording is MixMonitor.

In this example, we used the parameter as follows:

exten => s,n,MixMonitor(${UNIQUEID}.wav49)

Note that, ${UNIQUEID} is the filename and .wav49 is the extension, which means that Asterisk will convert audio files by using the WAV format.

After settings are applied, the new recording files will be placed on the directory /var/spool/asterisk/monitor

Before listening to these audios, you should mix them whether they were created from incoming or outgoing calls. Here, we use the the program called sox.

Mixing incoming calls:

/usr/bin/sox -m DIR_MONITOR/FILENAME-in.wav DIR_MONITOR/FILENAME-out.wav OUTPUT-PATH

Where:

DIR_MONITOR: The full path of audio files (/var/spool/asterisk/monitor).

FILENAME: The unique ID given by Asterisk when it was created. In this case, we have (-in) for one side of the call and (-out) for the other one. For example, 1617641548.58651-in.wav and 1617641548.58651-out.wav

OUTPUT-PATH: The full path containing the directory and the filename of the new mixed audio. For example: /var/spool/asterisk/records/my-record.wav

Mixing outgoing calls:

In this case, we must set the frequency to 8000 Mhz, define the channels to mono, and its format to WAV. It is important to notice that we just handle one single file.

/usr/bin/sox DIR_MONITOR/FILENAME.WAV -r 8000 -c 1 -e signed-integer OUTPUT-PATH

I strongly recommend that you make a script to deal with these recordings, store the new files safety and erase the previous ones. This is important to save disc space of the Asterisk server.

Categories
Asterisk Database Linux

Asterisk + ODBC + Mysql

The latest versions of the Asterisk PBX server connect to the Mysql database server by using ODBC. In this tutorial, I will show you how to set this up.

My test environment is CentOS Linux released on 7.9.2009, Asterisk 16.16.0, unixODBC 2.3.1, and Mysql Community Server 8.

I am taking for granted that you already have your Mysql installed and running. If you have not taken this step yet, you should use a tutorial like this one.

1 – Install the Unix ODBC:

# yum -y install unixODBC

2 – Download the official Mysql library connector from: https://dev.mysql.com/downloads/connector/odbc/

# wget https://cdn.mysql.com//Downloads/Connector-ODBC/8.0/mysql-connector-odbc-8.0.23-linux-glibc2.12-x86-64bit.tar.gz

Decompress the package in /usr/local directory or wherever you like.

# tar -zxvf mysql-connector-odbc-8.0.23-linux-glibc2.12-x86-64bit.tar.gz -C /usr/local

3 – Edit the file /etc/odbcinst.ini and insert the following code block:

[MySQL_ANSI]
Driver=/usr/local/mysql-odbc/lib/libmyodbc8a.so
sageCount=1

4 – Edit the file /etc/odbc.ini and create your connection:

[asterisk-connector]
Description = MySQL connection
Driver = MySQL_ANSI
Database = asterisk
Server = 127.0.0.1
Port = 3306

5 – Now, you must configure your Asterisk to use the connection. The file which contains that information is /etc/asterisk/res_odbc.conf

[asterisk]
enabled => yes
dsn => asterisk-connector
username => user
password => secret
pre-connect => yes
max_connections => 100

6 – Finally, make sure that Asterisk is using the ODBC module, take a look at the file /etc/asterisk/modules.conf and find the following line:

preload => res_odbc.so

If you have any questions, please use the comment box below.

Categories
Database Linux

Running Mysql queries using Login Profile

The newest versions of the Mysql server, such as the 8, presents the following message when users try to use the database password in the command line:

# mysql -u root -p"password" -e "show databases"

mysql: [Warning] Using a password on the command line interface can be insecure.

In order to prevent this warning message, you just need to create a password profile, in this example we going to create one called “local”:

# mysql_config_editor set --login-path=local \
--host=localhost --port=3306 \
--socket=/var/lib/mysql/mysql.sock \
--user=root --password

Now, you can easily run queries without providing the password, just use the “local” profile:

# /usr/bin/mysql --login-path=local -h localhost -e "show databases"