Changing linux username or password with python script

Changing the Linux username or password using a Python script can be a useful task for system administrators or individuals who want to automate the process. In this article, we will explore three different ways to achieve this goal.

Option 1: Using the subprocess module

The subprocess module in Python allows us to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. We can leverage this module to execute Linux commands and change the username or password.

import subprocess

def change_username(username, new_username):['usermod', '-l', new_username, username])

def change_password(username, new_password):['passwd', username], input=new_password.encode())

# Usage example
change_username('old_username', 'new_username')
change_password('username', 'new_password')

In the above code, we define two functions: change_username and change_password. The function is used to execute the Linux commands usermod and passwd with the appropriate arguments.

Option 2: Using the pexpect module

The pexpect module is a Python module for spawning child applications and controlling them automatically. It is particularly useful for automating interactive applications such as changing passwords.

import pexpect

def change_password(username, new_password):
    child = pexpect.spawn('passwd ' + username)

# Usage example
change_password('username', 'new_password')

In the above code, we use the pexpect.spawn function to spawn the passwd command with the username as an argument. We then use the child.expect function to wait for the password prompt and send the new password using child.sendline.

Option 3: Using the paramiko module

If you want to change the Linux username or password on a remote machine, you can use the paramiko module, which provides an implementation of the SSHv2 protocol.

import paramiko

def change_password(hostname, username, password, new_password):
    ssh = paramiko.SSHClient()
    ssh.connect(hostname, username=username, password=password)
    ssh.exec_command('echo -e "{}n{}" | passwd {}'.format(new_password, new_password, username))

# Usage example
change_password('hostname', 'username', 'password', 'new_password')

In the above code, we establish an SSH connection to the remote machine using the paramiko.SSHClient class. We then execute the passwd command with the new password using the ssh.exec_command method.

After exploring these three options, it is clear that the best option depends on the specific use case. If you are changing the username or password on the local machine, option 1 using the subprocess module is a straightforward and reliable choice. If you need to automate interactive applications, option 2 using the pexpect module is a suitable solution. Finally, if you are changing the username or password on a remote machine, option 3 using the paramiko module provides the necessary functionality.

Choose the option that best fits your requirements and enjoy the convenience of automating the process of changing Linux usernames or passwords with Python!

Rate this post

9 Responses

    1. I understand your concern about trusting a Python script with sensitive information. However, its crucial to evaluate the scripts security measures and the reputation of its developers. With proper encryption and rigorous testing, Python scripts can handle sensitive info effectively. Its all about taking necessary precautions to mitigate risks.

    1. Ive tried Option 3 and its nothing but a disappointment. Its overhyped and definitely not a game-changer. Dont waste your time on it, trust me. Stick with the tried and true methods instead.

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents