Wednesday, March 18, 2020

Installation of Python 3 in Linux

Beliefs

As our belief, every Linux flavor has Python as one of the Shell. but that shell was created long back based on the OS release time repositories. In the most common situation where built-in Python might be at Python 2.7.x version but the latest version is on Python 3.7.x. The big challenge here is 'how do I upgrade or install Python3 on Linux?' So I've chased this challenge and completed the latest version installed on my Oracle Linux box.

Python 3 installation on RHELflavors




How do I install Python3 latest version on Linux?

Python Programming is simple and kids can learn by doing! It's capabilities to interact with machine internals and the simple structure makes easy to write and understand it. The latest Python version for download you can find from the Python official site.

Here I'm installing Python3 on the Oracle Linux same steps will be followed on any RHEL flavors as well.
yum -y groupinstall development
yum -y install zlib-devel

Note: If you are working behind a proxy then enable them by exporting the HTTP, HTTPS proxy environment variables.

wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz

#Extract the downloaded file
tar xJf Python-3.7.3.tar.xz

cd Python-3.7.3
./configure
or 
./configure --enable-optimizations # if you want stable optimizations
make
make install

This binary installation might take a few minutes, you can relax and then resume back to check the installation is showing python3. Remember this, When you run the make command it will build the Python3 installer by compiling it with a local 'gcc' compiler because Python libraries and dependencies are written in C and C++.

Good References links:

  1. Python Source installation
  2. Python FTP link

Friday, March 13, 2020

Control your PC from Python Code

Hey guys, One of the easiest programming languages in the World is our Python. Here this post is intended for those who want to learn the hacking techniques! Here I would like to share simple 4lines of Python hack tricks that make shutdown or restart your PC. The logic here is that have the function contains the CMD Command line 'shutdown' with forceful(/s) timeout as 1 minute.


Shutdown logic is here...
import os
def shutdown():
    os.system("shutdown /s /t 1")
shutdown()

Restart logic is here
import os
def restart():
    os.system("shutdown /r /t 1")
restart()

Jump to Linux VM and same thought to execute the same script then it failed! "Must be root." The solution there you need to be superuser to shutdown your Linux machine.

To have better understand about the shutdown command in Linux operating system, used the --help option.
[vagrant@mydev ~]$ sudo shutdown --help
shutdown [OPTIONS...] [TIME] [WALL...]

Shut down the system.

     --help      Show this help
  -H --halt      Halt the machine
  -P --poweroff  Power-off the machine
  -r --reboot    Reboot the machine
  -h             Equivalent to --poweroff, overridden by --halt
  -k             Don't halt/power-off/reboot, just send warnings
     --no-wall   Don't send wall message before halt/power-off/reboot
  -c             Cancel a pending shutdown

On Linux the script will be as follows:
import os
def shutdown():
    os.system("shutdown -h")
shutdown()

The output is as follows for the Linux platform.
Linux Python Execution

Enhancement of the script
import os
import sys

def shutdown(platform):
    if platform=='linux2':
        os.system("shutdown -s")
    elif platform=='win32':
        os.system("shutdown /s /t 1")

def restart(platform):
    if platform=='linux2':
        os.system("shutdown -r")
    elif platform=='win32':
        os.system("shutdown /r /t 1")

# The main program starts here
platform=sys.platform
print platform
opt=sys.argv[1]
if opt=='s':
    print 'Shutting down your PC!!'
    shutdown(platform)
elif opt=='r':
    print 'Rebooting...'
    restart(platform)
else:
    print 'Invalid option'

DevOps Foundation course

DevOps Foundation course
Join us to learn DevOps from the Beginning