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