Bash script for creating new user generating key and adding to another server (oh, and also it`s sen

Aug 31, 2018 00:48


Баш скрипт для создания пользователя, генерация ключа и отправки ключа на другой сервер.

Сразу признаюсь не весь скрипт мой, я взял часть с stackoverflow и заапгрейдил ее.

#!/bin/bash

#Creating a script that creates a new user

ROOT_UID=0 #Root has $UID 0

SUCCESS=0

E_USEREXISTS=70

E_NOTROOT=65 #Not root

#Run as root, and this checks to see if the creater is in root. If not, will not run

if [ «$UID» -ne «$ROOT_UID» ]; then

echo «Sorry must be in root to run this script»

exit $E_NOTROOT

fi

read -p «Enter username : » username

read -p «Enter password for user :» passwd

cat /etc/passwd | grep -q «$username»

if [ $? -eq $SUCCESS ];

then

echo «User $username already exists»

echo «Please choose another username»

exit $E_USEREXISTS

else

useradd $username -d /home/$username -m ;

echo $passwd | passwd $username -stdin;

mkdir -p /home/$username/.ssl/

ssh-keygen -t rsa -N «» -f /home/$username/.ssl/my.key

cat /home/$username/.ssl/my.key.pub | sshpass -p «your_password» ssh username@ip_of_server ‘dd of=.ssh/authorized_keys oflag=append conv=notrunc’

mail -s «user=$username passwd=$passwd» your@email < /dev/null

echo «the account is setup»

exit 1

fi

Gendar from Where the time is

#not, #run, #creating, #root

Previous post Next post
Up