Skip to Content
Technical Articles
Author's profile photo Luca Falavigna

Change of user password via BAPI in Python

Background

Every now and then, I want to change the passwords for my user on several systems and clients. Even though I have single sign-on functionality enabled, this is an extra safety net in case the authentication via kerberos token fails for some reason.

In the company I work for we maintain several application servers and clients, and change passwords in all of them is a rather boring task. Given there’s a specific BAPI which allows to change passwords easily (SUSR_USER_CHANGE_PASSWORD_RFC), I wanted to automate the process using a script written in my top programming language: Python.

Although the example shown is very simple, it will cover setting up the prerequisites and help getting used to the basic commands of PyRFC module. Let’s get started!

 

Prerequisites

I need to get some components to be able to execute the script. I’m running a Windows 64-bit machine, so my instructions are specific for this configuration, but can be adjusted easily for GNU/Linux or other systems for which the prerequisites are met.

 

For the Python interpret, I chose to use WinPython because it’s a portable package (does not require a formal installation) and also provides some interesting libraries not included in the Python standard library, I also added the folder where I placed the WinPython files into my path environment variable, so I can execute Python interpret from anywhere:

 

Next, I need to download the NetWeaver RFC SDK from One Support Launchpad. I have to select my operating system (in my case WINDOWS ON X64 64BIT) and download the .ZIP file. Once fully downloaded, I extract it in my preferred destination folder and then add the exact location of the nwrfcsdk\lib subfolder into the path environment variable:

 

Finally, I need to download and install the PyRFC wheel specific for my environment. I’m running Python 3.6.X on Windows 64-bit, therefore I need to download the file named pyrfc-1.9.93-cp36-cp36m-win_amd64.whl. When download is complete, I have to make it available in my Python interpret running the command

python -m pip install pyrfc-1.9.93-cp36-cp36m-win_amd64.whl

 

If everything has been configured correctly, I should not get any message while trying to test import pyrfc module from the Python interpret:

If something is not working, most likely due to an erroneous configuration of the NetWeaver RFC SDK, I could receive an ugly error message like the one below:

 

Developing and executing the script

Now the prerequisites are met, I can start writing my script. It’s a very small piece of code which calls SUSR_USER_CHANGE_PASSWORD_RFC passing user and the old and new passwords as parameters:

from getpass import getuser, getpass
from pyrfc import Connection
from pyrfc._exception import LogonError


# Maintain a tuple of tuples of instances. Each tuple must contain application
# server hostname or IP, instance number and client number.
systems = (('mysapserver.company.com', '00', '100'),
           ('mysapserver.company.com', '00', '200'))

# Ask for user name, current system user will be used as default.
user = input('User name (default value: {0}): '.format(getuser())) or getuser()
# Ask for old and new passwords. Input will be hidden.
oldpasswd = getpass('Old password: ')
newpasswd = getpass('New password: ')

# Iterate for each system defined in the tuple of tuples above
for system in systems:
    try:
        # Connect to the specific instance
        conn = Connection(user=user,
                          passwd=oldpasswd,
                          ashost=system[0],
                          sysnr=system[1],
                          client=system[2])
        # Call SUSR_USER_CHANGE_PASSWORD_RFC BAPI to change password
        conn.call('SUSR_USER_CHANGE_PASSWORD_RFC',
                  BNAME=user,
                  PASSWORD=oldpasswd,
                  NEW_PASSWORD=newpasswd)
        # Close the RFC connection
        conn.close()
    except LogonError:
        print('ERROR! Logon error for host {0} system number {1} client {2}'
              .format(system[0], system[1], system[2]))
    else:
        print('Password for host {0} system number {1} client {2} changed'
              .format(system[0], system[1], system[2]))

 

Here’s the output when I launch it:

 

Further reading

This script is a quick example which demonstrates how to call BAPI’s from Python. There are many complex BAPI’s which require both input and tables as parameters, therefore it’s useful to have a look at the PyRFC documentation to get more insights on the usage of PyRFC.

Assigned tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Saptarshi Dutta Choudhury
      Saptarshi Dutta Choudhury

      Hi Luca,

       

      Thanks that helps, can you please help in getting the correct syntax (conn.call) function to update the new email address of User ID using BAPI_USER_CHANGE (Function Module) in Python ?

      Author's profile photo Luca Falavigna
      Luca Falavigna
      Blog Post Author

      Dear Saptarshi,

      The code snippet below should work:

      address = {'E_MAIL': 'email.address@company.com'}
      addressx = {'E_MAIL': 'X'}
      reply = conn.call('BAPI_USER_CHANGE', USERNAME='USER',
                        ADDRESS=address, ADDRESSX=addressx)