Archiv der Kategorie: DevOps Tutorials

Tutorials for DevOps & Scripting Deploying and Technical contribution for Professionals

Python Version? How to check Version

How to check installed Python Version

Python Version

How to check Python Version and query OS Platform using Python.

#!/usr/bin/python

import platform
import sys

def linux_distribution():
  try:
    return platform.linux_distribution()
  except:
    return "N/A"

print("""Python version: %s
Distribution: %s
OS: %s
System: %s
Machine: %s
Platform: %s
uname: %s
version: %s
""" % (
sys.version.split('\n'),
str(platform.dist()),
linux_distribution(),
platform.system(),
platform.machine(),
platform.platform(),
platform.uname(),
platform.version(),
))

Python should output the entire OS environment.

#!/usr/bin/env python

import os

for param in os.environ.keys():
    print "%20s %s" % (param,os.environ[param])

About Python

Python consistently ranks as one of the most popular programming languages.

Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation.

Python is dynamically typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly procedural), object-oriented and functional programming. It is often described as a „batteries included“ language due to its comprehensive standard library.

Guido van Rossum began working on Python in the late 1980s as a successor to the ABC programming language and first released it in 1991 as Python 0.9.0.[35] Python 2.0 was released in 2000. Python 3.0, released in 2008, was a major revision not completely backward-compatible with earlier versions. Python 2.7.18, released in 2020, was the last release of Python 2.

The Python Methods

Methods on objects are functions attached to the object’s class; the syntax instance.method(argument) is, for normal methods and functions, syntactic sugar for Class.method(instance, argument). The Python methods have an explicit self parameter to access instance data, in contrast to the implicit self (or this) in some other object-oriented programming languages (e.g., C++, Java, Objective-C, Ruby). Python provides methods, often called dunder methods (due to their names beginning and ending with double-underscores), to allow user-defined classes to modify how they are handled by native operations including length, comparison, in arithmetic operations and type conversion.

Python Libraries

Python’s large standard library provides tools suited to many tasks and is commonly cited as one of its greatest strengths. For Internet-facing applications, many standard formats and protocols such as MIME and HTTP are supported. It includes modules for creating graphical user interfaces, connecting to relational databases, generating pseudorandom numbers, arithmetic with arbitrary-precision decimals, manipulating regular expressions, and unit testing.

Most Python implementations (including CPython) include a read–eval–print loop (REPL), permitting them to function as a command line interpreter for which users enter statements sequentially and receive results immediately.

Also Python comes with an Integrated development environment (IDE) called IDLE, which is more beginner-oriented.

Other shells, including IDLE and IPython, add further abilities such as improved auto-completion, session state retention, and syntax highlighting.

Python Development

As well as standard desktop integrated development environments, there are web browser-based IDEs, including SageMath, for developing science- and math-related programs; PythonAnywhere, a browser-based IDE and hosting environment; and Canopy IDE, a commercial IDE emphasizing scientific computing.

htaccess und Dynamische IP Adressen

Apache htaccess Allow from mit DDNS Dynamischen IP Adressen

Apache Webserver Zugriffsteuerung über htaccess-Datei, für Autorisierten Zugang zu Webseiten.

htaccess-datei und Dynamische IP Adressen

Mit der Apache direktiveAllow fromist es möglich eine bestimmte IP von der Anmeldeaufforderung auszuschliessen. Dabei ist die Übergabe von Hostnamen und FQDN leider nicht möglich. Anhand des folgenden Scriptes wird die Dynamische IP eines Hostname ausgelesen und in die htaccess-Datei eingetragen.

htaccess mit Dynamischen IP Adressen

Der folgende Shell Script ermöglicht die Auflösung der IP Adresse und schreiben der IP Adresse in die htaccess-Datei. Die folgenden Zeilen in der Konsole mit copy & paste einfügen, damit wird die Script Datei erstellt.

cat << EOF > ./allow_myhost.sh
#!/bin/sh
htpath="/var/www/blog/"
grep -lr "#DDNS" $htpath | while read i; do
sed -i '/#DDNS-IP$/d' $i
grep -i "#DDNS$" $i | while read j; do
words=( $j )
ddns="${words[2]}"
ip="$(host $ddns)"
if [ "$ip" == "${ip%% has address *}" ]; then
continue;
fi
ip="${ip##* has address }"
sed -i 's/^\('"$j"'\)$/\1\nAllow from '"$ip"' #DDNS-IP/' $i
done
done
EOF

 Copy Paste

Der Script schreibt hier auf einem CentOS Host die htaccess-Datei um. Bei ZeileAllow frommit der Marke #DDNS wird der Hostname ausgelesen, auf der nächsten Zeile mit der Marke #DDNS-IP wird die aufgelöste IP des Host geschrieben. Die Pfad VariablehtpathkannDocumentRootoder ein Unterverzeichnis sein, dabei bearbeitet der Script alle vorkommenden .htaccess-Dateien rekursive ab htpath.

Die htaccess-Datei im Webverzeichnis

Die htaccess-Datei wird im Webverzeichnis gespeichert dieses geschützt werden soll. Mit cd in das gewünschten Verzeichnis wechseln und die folgenden Zeilen mit copy & paste in der Konsole einfügen, dies erzeugt die .htaccess-Datei.

cat << EOF > .htaccess
AuthName "A Blog"
AuthType Basic
AuthUserFile /home/jonny/.htpasswd
AuthGroupFile /dev/null
Order deny,allow
Deny from all
require valid-user
Allow from myhome.dyndns.org #DDNS
Allow from 198.51.100.64 #DDNS-IP
Satisfy Any
EOF

 Copy Paste

Die Zeilen mit #DDNS und #DDNS-IP (mit #) dienen zur Markierung.

Der Script muss noch ausführbar gemacht werden.

chmod 755 allow_myhost.sh

Dynamischen IP laufend aktualisieren

Damit die Auflösung der Dynamischen IP laufend aktualisiert wird, kann mitcrontab -eein Cron job erstellt werden.

*/5 * * * * root /home/jonny/allow_myhome.sh >/dev/null 2>&1

Nach restart des Cron Daemon ist der job aktive.

service crond restart

Hinweis:
Ab Apache 2.4 lautet die direktive von zuvorAllow fromnunRequire.
Apache 2.x mod_access_compat

Order deny,allow
Deny from all

Apache 2.4 mod_authz_host

Require all denied

Die von mod_access_compat bereitgestellten Allow-, Deny- und Order-Direktiven sind veraltet und werden in den zukünftigen Version nicht mehr unterstützt. Es wird empfohlen die neuen Direktiven zu verwenden.