in reply to Run command as a user
It might not wise to keep the plaintext passwords in the script. You could provide sudo privilage to your user to run the required command as the needed user without needing password, and call system(sudo -u user ...) on *nix env.