Alle Beiträge von Don Matteo

lebt in Zürich, ist System Engineer MCP bei A-Enterprise GmbH. Mitglied des UNBLOG Network. Author und Blogger zu den Themen, Linux und Open Source. Tutorials für Windows, VMware, Synology, Fortinet.

Python Version? How to check Version

How to check installed 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.

TR-069 CWMP Provisioning on ISC-DHCPD

Das Broadband World Forum hat das Fernwartungsprotokoll TR-069 entwickelt, um Kunden-Router aus der Ferne einrichten zu können. Damit zum Beispiel Updates, Konfigurationen oder das Aufschalten zusätzlicher Dienste schneller vonstatten gehen können.

Mit dem TR-069 Protokoll können DHCP-Server einen oder mehrere herstellerspezifische Parameter an den Client Router senden. Eine CWMP Konfiguration benötigt für die URL zum Auto-Configuration-Servern (ACS) zur Provisionierung über ISC-DHCPD folgende Parameter:

subnet 192.168.0.0 netmask 255.255.255.0 {
        option routers 192.168.0.1;
        range 192.168.0.100 192.168.0.111;
        append dhcp-parameter-request-list 43;
        option vendor-encapsulated-options 01:12:68:74:74:70:3a:2f:2f:61:63:73:2e:69:73:70:2e:6f:72:67;
}

Beispiel ACS URL https://acs.isp.org

Das Optionsfeld option vendor-encapsulated-options muss als codierte Sequenz identischer Syntax mit Code/Länge/Wertfeld zum DHCP Optionsfeld angegeben werden.

The code for this option is 43 and its minimum length is 1.
Code   Len   Vendor-specific information
+-----+-----+-----+-----+---
|  43 |  n  |  i1 |  i2 | ...
+-----+-----+-----+-----+---

When encapsulated vendor-specific extensions are used, the
information bytes 1-n have the following format:

 Code   Len   Data item        Code   Len   Data item       Code
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|  T1 |  n  |  d1 |  d2 | ... |  T2 |  n  |  D1 |  D2 | ... | ... |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Abschnitt: IETF rfc2132 – DHCP Options and BOOTP Vendor Extensions

url2hex

Zur Umwandlung dient folgender bash Script, diesen man wie folgt aufruft ./url2hex https://acs.isp.org

#!/bin/bash
if [[ -z $1 ]]; then
  echo "Argument missing! Use: url2hex url"
  exit 1
fi
echo ""
echo -e "[[[ URL2HEX Converter ]]]\nTR-069 CWMP request ACS URL provisioning on ISC-DHCPD option vendor-encapsulated-options"
echo "URL:$1"
wcount=`echo $1 | wc -m`
tcount=`echo ${wcount}-1 | bc`
length=`printf '%x\n' ${tcount}`
hexurl=`echo -n $1 | xxd -ps | sed 's/[[:xdigit:]]\{2\}/\:&/g'`
echo -e "URL-length:${tcount} HEX:${length}\n\nvalues for CWMP provisioning in dhcpd.conf:\n"
echo "append dhcp-parameter-request-list 43;"
echo "option vendor-encapsulated-options 01:${length}${hexurl};"

Die Code Zeilen in eine Datei url2hex speichern und ausführbar machen.

$ chmod u+x url2hex

Es wird die länge der übergebenen Zeichenkette berechnet und in den HEX Wert umwandelt, die Zeichenkette selbst wird ebenfalls in HEX umwandelt, danach werden die Werte zusammengehängt, beginnend von 0x01 für den Wert der CWMP Option für die ACS URL.

Die Ausgabe kann dann wie folgt aussehen:

[[[ URL2HEX Converter ]]]
TR-069 CWMP request ACS URL provisioning on ISC-DHCPD option vendor-encapsulated-options
URL:https://acs.isp.org
URL-length:19 HEX:13

values for CWMP provisioning in dhcpd.conf:

append dhcp-parameter-request-list 43;
option vendor-encapsulated-options 01:13:68:74:74:70:73:3a:2f:2f:61:63:73:2e:69:73:70:2e:6f:72:67;

Die letzten zwei Zeilen können in dhcpd.conf hinzugefügt werden.

Referenzen

TR-069 Boadband Forum, art. “3.1 ACS Discovery”
RFC2132 – DHCP Options and BOOTP Vendor Extensions, art. “8.4. Vendor Specific Information”.