1

I write python script that connected to a Linux server and run a bash script that create user and password. The password is not set and I don't know why, I thought the random password from python don't send the value to bash script, but if in the bash script I create directory with the password value its create!

python script:

import boto3
import botocore
import paramiko
import string
import secrets

def add_user(username, subfolder):
    key = paramiko.RSAKey.from_private_key_file("x.x.x")
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    alphabet = string.ascii_letters + string.digits
    password = ''.join(secrets.choice(alphabet) for i in range(8))
    cmd = "add_user %s %s %s" % (username, subfolder, password)

    # Connect/ssh to an instance
    try:
        # Here 'test' is user name and 'instance_ip' is public IP of EC2
        client.connect(hostname="x.x.x.x", username="test", pkey=key)
        # Execute a command(cmd) after connecting/ssh to an instance
        #stdin, stdout, stderr = client.exec_command(f"add_user {username} {subfolder} {password}")
        stdin, stdout, stderr = client.exec_command(cmd)
        print (cmd)
        print (password)

        client.close()
    except:
        print ("Something wrong")

add_user('test29', '12asd3')

Bash script:

#!/bin/bash

    USERNAME=$1
    SUBFOLDER=$2
    PASSWORD=$3
    sudo useradd -d /home/FTPserver/$USERNAME/ -m $USERNAME
    sudo mkdir -p /home/FTPserver/$USERNAME/$SUBFOLDER
    sudo chown -R $USERNAME:$USERNAME /home/FTPserver/$USERNAME/$SUBFOLDER
    sudo echo -e "${PASSWORD}\n${PASSWORD}" | passwd $USERNAME
    sudo echo $PASSWORD
3
  • Can you try to run your script with sudo? Commented Feb 13, 2020 at 12:53
  • like that sudo python your_python_file.py Commented Feb 13, 2020 at 14:28
  • It did not work Commented Feb 13, 2020 at 14:34

1 Answer 1

1

passwd is not the right tool for the job, you can use chpasswd instead. From man chpasswd:

The chpasswd command reads a list of user name and password pairs from standard input and uses this information to update a group of existing users. Each line is of the format: user_name:password

To put it in your script, also make sure to move the sudo to execute the right side of the pipe:

echo "${USERNAME}:${PASSWORD}" | sudo chpasswd 
Sign up to request clarification or add additional context in comments.

3 Comments

Can you print(stderr) to see if you get an error message?
Output: add_user test33 12asd3 vG8UyQ6Q vG8UyQ6Q <paramiko.ChannelFile from <paramiko.Channel 0 (open) window=2097152 -> <paramiko.Transport at 0x298fe6a0 (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>> link for output: ibb.co/Z2n2RBB
It still does not work, but I must say that if I run the bash script from the Linux its success and when I run the python script with the same bash script its filed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.