Hello, my name is Matteo Mattei and this is my personal website. I am computer engineer with a long experience in Linux system administration and web software development.
The following script performs GET and POST requests to [httpbin.org] using only builtin python 3 modules. There is also a class to support file encoding for upload.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The following script can be used to statically cross compile wget for ARM.
Requirements:
You need openssl and zlib already present in the current $ROOTPATH directory with related libraries and included respectively inside libs and include folders.
You need a glibc compiled with --enable-static-nss flag so that getaddrinfo and gethostbyname cannot complain at link time.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I have a router with a public static ip address provided by the ISP and I need to share internet access to all pc in the LAN. To do it I need a server with two ethernet interfaces (eth0 and eth1) that will act as a firewall and dhcp server. That server will also be used as a web server to publish some contents in the LAN and in internet.
My configuration is this:
eth0 with public static IP address provided by the ISP.
eth1 with private static IP address assigned by me and connected to a switch.
Install shorewall:
apt-get install shorewall
Start configuration with two interface shorewall example:
cd /usr/share/doc/shorewall/examples/two-interfaces/
cp interfaces /etc/shorewall/
cp masq /etc/shorewall/
cp policy /etc/shorewall/
cp rules /etc/shorewall/
cp zones /etc/shorewall/
cp stoppedrules /etc/shorewall/
Now configure /etc/network/interfaces (I am using Debian jessie):
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address xxx.xxx.xxx.xxx
netmask yyy.yyy.yyy.yyy
gateway zzz.zzz.zzz.zzz
auto eth1
iface eth1 inet static
address 192.168.0.1
netmask 255.255.255.0
where:
xxx.xxx.xxx.xxx is the public IP address provided by the ISP
yyy.yyy.yyy.yyy is the netmask provided by the ISP
zzz.zzz.zzz.zzz is the gateway provided by the ISP
And /etc/resolv.conf:
# Google DNSnameserver8.8.8.8
nameserver 8.8.4.4
Now configure shorewall to act as firewall and share internet to all LAN devices.
File: /etc/shorewall/interfaces
#ZONE INTERFACE OPTIONS
net eth0 dhcp,tcpflags,nosmurfs,routefilter,logmartians,sourceroute=0
loc eth1 dhcp,tcpflags,nosmurfs,routefilter,logmartians
File: /etc/shorewall/zones
#ZONE TYPE OPTIONS IN OUT# OPTIONS OPTIONS
fw firewall
net ipv4
loc ipv4
File: /etc/shorewall/policy
#SOURCE DEST POLICY LOG LEVEL LIMIT:BURST$FW net ACCEPT
$FW loc ACCEPT
loc net ACCEPT
net all DROP info
# THE FOLLOWING POLICY MUST BE LAST
all all REJECT info
File: /etc/shorewall/rules
?SECTION ALL
?SECTION ESTABLISHED
?SECTION RELATED
?SECTION INVALID
?SECTION UNTRACKED
?SECTION NEW# Don't allow connection pickup from the net#
Invalid(DROP) net all tcp
## Accept DNS connections from the firewall to the network#
DNS(ACCEPT) $FW net
DNS(ACCEPT) loc $FW## Accept SSH connections from the local network for administration#
SSH(ACCEPT) loc $FW## Allow Ping from the local network#
Ping(ACCEPT) loc $FW## Drop Ping from the "bad" net zone.. and prevent your log from being flooded..#
Ping(DROP) net $FW
ACCEPT $FW loc icmp
ACCEPT $FW net icmp
## custom rules
SSH(ACCEPT) net $FW
Web(ACCEPT) net $FW
Web(ACCEPT) loc $FW# DNAT rules (useful for natting a service on a device)# this rule opens port 8080 from internet to port 80 of 192.168.0.2 in TCP#DNAT net loc:192.168.0.2:80 tcp 8080
The above configuration allows SSH connections from local and from remote as well as Web access.
File: /etc/shorewall/masq
#INTERFACE:DEST SOURCE ADDRESS PROTO PORT(S) IPSEC MARK USER/ SWITCH ORIGINAL# GROUP DEST
eth0 eth1
File: /etc/shorewall/stoppedrules
#ACTION SOURCE DEST PROTO DEST SOURCE# PORT(S) PORT(S)
ACCEPT eth1 -
ACCEPT - eth1
Now edit /etc/shorewall/shorewall.conf and set:
STARTUP_ENABLED=YesIP_FORWARDING=On
In particular the last line is necessary to share internet to the LAN. Edit now /etc/default/shorewall and set:
startup=1
Now start shorewall:
/etc/init.d/shorewall start
And try to configure a pc in the LAN with a static ip address, for example:
Yeah, the pc should be able to access internet! But we need to go a little bit ahead because given we don’t want to assign a static ip address to all pc (or devices) in the LAN. So we have to install a DHCP server.
apt-get install dnsmasq
Now backup the default configuration of dnsmasq and create a new /etc/dnsmasq.conf with something like this:
The first line specifies the interface where the DHCP server is running (eth1 for me1). The second line (dhcp-range) is the range of IP addresses that the DHCP server will provide with a lease of 12 hours. In this case from address 192.168.0.50 to address 192.168.0.150. All other lines are used to define devices with static IPs. The syntax is:
Dnsmasq beyond dhcp and DNS caching provides also another interesting feature… every entry you set in /etc/hosts of the server is automatically forwarded to all devices in the LAN. This means that if we want to set a simple name for accessing the webserver from the LAN it’s just a matter of editing /etc/hosts and add the server’s name:
127.0.0.1 localhost
192.168.0.1 myserver
This allows all devices in the LAN to access the server using myserver.
Now restart dnsmasq:
/etc/init.d/dnsmasq restart
To list all devices that have received a new IP address the file to look at is /var/lib/misc/dnsmasq.leases
configure database for phpmyadmin with dbconfig-common? Yes
Password of the database’s administrative user: xxx
Password for phpmyadmin: xxx
Password confirmation: xxx
Postfix:
Select Internet Site
System mail name: (insert here the FQDN, for example web1.myserver.com)
Setup FTP
Stop VSFTP server:
/etc/init.d/vsftpd stop
Create backup configuration:
mv /etc/vsftpd.conf /etc/vsftpd.conf.backup
Add new configuration:
listen=YESlisten_port=21anonymous_enable=NOlocal_enable=YESguest_enable=YESguest_username=nobodyuser_sub_token=$USERlocal_root=/var/www/vhosts/$USERvirtual_use_local_privs=YESuser_config_dir=/etc/vsftpd/userspam_service_name=vsftpd_local_and_virtualchroot_local_user=YESchroot_list_enable=YESchroot_list_file=/etc/vsftpd/chroot_listftpd_banner=Welcome to my ftp serverwrite_enable=YESdownload_enable=YESdirlist_enable=YESlocal_umask=022dirmessage_enable=YESxferlog_enable=YESxferlog_file=/var/log/xferlogconnect_from_port_20=YESconnect_timeout=60data_connection_timeout=300idle_session_timeout=300local_max_rate=0max_clients=0max_per_ip=3
Create an empty chroot_list file:
mkdir /etc/vsftpd
touch /etc/vsftpd/chroot_list
Install PAM module for virtual users:
apt-get install libpam-pwdfile
And configure it creating the file /etc/pam.d/vsftpd_local_and_virtual with this content:
# Standard behaviour for ftpd(8).
auth required pam_listfile.so item=user sense=deny file=/etc/ftpusers onerr=succeed
# first try to authenticate local users
auth sufficient pam_unix.so
# if that failed, login with virtual user
auth required pam_pwdfile.so pwdfile /etc/vsftpd/passwd
# pam_pwdfile doesn't come with account, so we just permit on success
account required pam_permit.so
Now we have to make some changes also to systemd scripts (this step is mandatory for Debian Jessie!) since systemd does not consider /etc/default/varnish settings:
sed -i "{s/^key_buffer/key_buffer_size/g}" /etc/mysql/my.cnf
Enable MySQL slow query logging (often useful during slow page load debugging):
sed -i "{s/^#slow_query_log_file /slow_query_log_file /g}" /etc/mysql/my.cnf
sed -i "{s/^#slow_query_log /slow_query_log /g}" /etc/mysql/my.cnf
sed -i "{s/^#long_query_time /long_query_time /g}" /etc/mysql/my.cnf
sed -i "{s/^#log_queries_not_using_indexes/log_queries_not_using_indexes/g}" /etc/mysql/my.cnf
MySQL is now configured, so restart it:
/etc/init.d/mysql restart
Configure Shorewall firewall rules
Copy the default configuration for one interface:
cd /usr/share/doc/shorewall/examples/one-interface
cp interfaces /etc/shorewall/
cp policy /etc/shorewall/
cp rules /etc/shorewall/
cp zones /etc/shorewall/
Now open /etc/shorewall/policy file and change the line:
net all DROP info
removing info directive given it fills the system logs:
net all DROP
Now open /etc/shorewall/rules and add the following rules at the bottom of the file:
HTTP/ACCEPT net $FW
SSH/ACCEPT net $FW
FTP/ACCEPT net $FW# real apache since varnish listens on port 80#ACCEPT net $FW tcp 8080
NOTE: in case you want to allow ICMP (Ping) traffic from a specific remote hosts you need to add a rule similar to the following where xxx.xxx.xxx.xxx is the remote IP address, before the Ping(DROP) rule:
Ping(ACCEPT) net:xxx.xxx.xxx.xxx $FW
Now edit /etc/default/shorewall and change startup=0 to startup=1 You are now ready to start the firewall:
/etc/init.d/shorewall start
Setup Postfix
Stop postfix server:
/etc/init.d/postfix stop
Edit /etc/mailname and set your server domain name, for example:
server1.mycompany.com
Then, in order to monitor mail traffic coming from PHP you need to edit /etc/php5/apache2/php.ini. Go to [mail function] section and set the following two options:
Download also the tools that will be used with cron:
cd /root/cron_scripts
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian7/LAMP_24/cron_scripts/backup_mysql.sh
wget https://raw.githubusercontent.com/matteomattei/servermaintenance/master/Debian7/LAMP_24/cron_scripts/mysql_optimize.sh
chmod 770 *.sh
Edit /root/ADD_DOMAIN.sh and change ADMIN_EMAIL variable with your email address.
Edit /root/MYSQL_CREATE.sh and change the variable MYSQL_ROOT_PASSWORD with your MySQL root password.
Edit /root/cron_scripts/backup_mysql.sh and change the variable DB_PASSWORD with your MySQL root password and MAIL_NOTIFICATION with your email address.
Edit /root/cron_scripts/mysql_optimize.sh and change the variable MYSQL_ROOT_PASSWORD with your MySQL root password.
Configure CRON
Edit /etc/crontab and add the following lines at the bottom:
Now add tty group to pi user because pi needs to handle /dev/ttyX devices and adjust permissions accordingly at every system startup:
gpasswd -a pi tty
sed -i '/^exit 0/c\chmod g+rw /dev/tty?\nexit 0' /etc/rc.local
Now create a startup script:
vi /home/pi/startkiosk.sh
#!/bin/bash
# disable DPMS (Energy Star) features.
xset -dpms
# disable screen saver
xset s off
# don't blank the video device
xset s noblank
# disable mouse pointer
unclutter &
# run window manager
matchbox-window-manager -use_cursor no -use_titlebar no &
# run browser
midori -e Fullscreen -a http://www.google.com
and make it executable:
chmod +x /home/pi/startkiosk.sh
Then add the following lines at the end of /home/pi/.bashrc:
if [ -z "${SSH_TTY}" ]; then
xinit ~/startkiosk.sh
fi
This three lines allow starting X only when we login as pi directly but not from SSH.
Make the SD read-only
Backup your current /etc/fstab and create a new one with the following content:
This is a very quick guide on how to create self contained Python executables (for all platforms). First of all install PyInstaller (I am using pip3 because I work with Python 3.x):
sudo pip3 install pyinstaller
Now install upx for a better compression:
sudo apt-get install upx
Now you are ready to create your self-contained executable:
Resulting executable will be placed inside dist folder and it will be called myscript. This is what each parameter does:
--onefile allows to create a single self contained binary.
--noconfirm replaces output directory without asking for confirmation.
--noconsole should be used in GUI application with no console. --console should be used otherwise.
--clean cleans PyInstaller cache and remove temporary files.
--log-level=WARN shows only warnings and errors during build.
--key=yourkey uses the given key to encrypt the Python bytecode (yes it’s secure!).
--strip removes debug information to executable and shared libraries.
For windows, if you want to add also the icon to the resulting exe file you can add this additional parameter:
--icon-file=myapplication.ico
The application have only to be recompiled on every platform you want to release your application for. I know, the resulting binary will be a little heavy (~24MB for a PySide GUI application on Linux) but we have to consider that it contains the interpreter itself and all the needed libraries!!!