Creating a Script that will create user accounts (using useradd) from a .txt file of usernames [closed] - linux

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to create a script that will create user accounts (using useradd) from a .txt file of usernames. Please help me because my professor has not been the best of help.
The text file's name is users.txt
The only in the file is usernames, we are suppose to set up a default password and have it where they must change it on the next logon. We are also supposed to add them into a group called interns.
Here is what I have so far:
#!/bin/bash
for i in users.txt
do
sudo echo $i
sudo useradd $i -m -d /home/$i -s /bin/bash $i -G sudo interns $i
passwd = echo "AIST2330password" | passwd -stdin $i
sudo passwd -e $i
echo "User must change password when they come back"
done

This line:
for i in users.txt
should be:
for i in $(cat users.txt)
Your code is iterating over the literal string users.txt, not the contents of the file.
And this line:
passwd = echo "AIST2330password" | passwd -stdin $i
should be:
echo "$i:AIST2330password" | chpasswd
because the --stdin option to passwd has been deprecated.

Related

(Linux-Bash) Create an account for each user and add the user to its group from a text file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a username.txt which contains their username and specific group. As of right now i'm trying to create a bash script which allows me to create an account for each user and add the user to its group in one command.
this is currently my failed bash script(i know pretty much everything is wrong but i hope you guys got a clear idea on it):
#!/bin/bash
sudo addgroup staff
sudo addgroup visitors
username="username.txt"
while read line; do
sudo useradd $-Eo '^[^,]+' $username;
if [grep staff $username]; then
sudo usermod -a -G staff
else
sudo usermod -a -G visitors
done < $username
This is what is inside my username.txt file:
ellipsiscoterie,visitor
magnetcommonest,visitor
belateddefensive,staff
bobstercaramelize,staff
Let's go through your script.
#!/bin/bash
sudo addgroup staff
sudo addgroup visitors
username="username.txt"
OK. There is some debate about using sudo in scripts, but I'm not against it.
while read line; do
You read the line from STDIN, which is your input file. The variable line contains ellipsiscoterie,visitor in the first iteration.
sudo useradd $-Eo '^[^,]+' $username;
$- prints The current set of options in your current shell. It will produce something like himBH. The next argument seems a regular expression, and the last argument is the filename that you use. So the command here is:
sudo useradd himBHEo '^[^,]+' username.txt
Hint: if you are unsure of the arguments, check with an echo (echo $-Eo '^[^,]+' $username) before you add them to a sudo-ed command.
This is not what you want. First, you probably want to use the variable line instead of username. Why would you otherwise loop through that file?
Second, read-up on variable expansion in bash. For now, try:
line=ellipsiscoterie,visitor
echo ${line%,*}
echo ${line#*,}
So the line would probably need to be:
sudo useradd ${line%,*}
if [grep staff $username]; then
This is wrong in almost everything.
the [ and ] require spaces to set them apart
but you dont want to do a test, you want to see if the grep succeeds, so the [ and ] are not needed anyway
you are again using the complete file. So the grep succeeds if there is any line with staff in it; even if the username would be johnfalstaff
What you really want to know is if the second column in your line is staff, so:
if [ "${line#*,}" = "staff" ] ; then
sudo usermod -a -G staff
else
sudo usermod -a -G visitors
So, where is the fi that closes the if statement?
done < $username
Also, quote the filename: done < "$username"
You can use awk to write it in one line. In this case, awk splits each row into different columns and you can access each field separately.
awk -F "," '{ if(system("grep -q -E "$2" /etc/group") != 0 ){system("groupadd "$2)}; system("useradd "$1" -G "$2)}' username.txt
The first argument (-F ",") defines the field-separator, but it could be also something else e.g. ";" or "/"
The part if(system("grep -q -E "$2" /etc/group") != 0 ) verifies if the group exist and if not, the part {system("groupadd "$2)} creates the group before the next command system("useradd "$1" -G "$2) creates the user and adds it to the group $2.
It's possible to simplify the command to remove the if part, but then you will get a warning message that the user already exists.
awk -F "," '{ system("groupadd "$2); system("useradd "$1" -G "$2)}' username.txt
groupadd: group 'visitor' already exists
groupadd: group 'staff' already exists
btw: the part system executes just an operating system command
The newusers command allows you to configure users from a file in batch:
https://www.man7.org/linux/man-pages/man8/newusers.8.html

bash file Linux Ubuntu [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I don't know why my bash file in Linux Ubuntu doesn't work.
#!/bin/bash`
echo -n "Write user name : "
read NAME
cd
if grep -c "$NAME" /etc/passwd -eq 0; then
echo "$NAME doesn't esist"
else
echo "$NAME already esist"
fi`
It gives the error:
$ bash ./name.sh
Write user name : root
grep: root: No such file or directory
/etc/passwd:3
grep: 0: No such file or directory
The code is not executing the grep in a sub-shell, and the if-condition needs to be wrapped in square brackets.
#!/bin/bash
echo -n "Write user name : "
read NAME
cd
if [ `grep -c "$NAME" /etc/passwd` -eq 0 ]; then
echo "$NAME doesn't esist"
else
echo "$NAME" already esist
fi
Output:
$ bash ./name.sh
Write user name : root
root already esist
$ bash ./name.sh
Write user name : ddd
ddd doesn't esist
You have some strange backquotes in your script which you should remove
#!/bin/bash
read -p "Write user name : " name
if grep -q "$name" /etc/passwd; then
echo "$name already exists"
else
echo "$name doesn't exist"
fi
as you can see
read can show a prompt
variables should be lowercase
the exit value of grep determines the case and no need for parenthesis or brackets
also notice that is looking for name everywhere, not just user names
because you included the shebang you can
$ chmod +x script
$ ./script

Perform SSH remote cmd exec on multiple local servers from input (sshpass?) [duplicate]

This question already has answers here:
Loop through a file with colon-separated strings
(2 answers)
Using a variable's value as password for scp, ssh etc. instead of prompting for user input every time
(10 answers)
How to automate password entry?
(5 answers)
Closed 4 years ago.
I am currently looking for a solution for executing remote commands on multiple local servers from an input file containing the 'user : password' in the following format:
jboss5:manager:192.168.1.101
database1:db01:192.168.20.6
server8:localnet:192.168.31.83
x:z:192.168.1.151
test:mynet:192.168.35.44
.... and others
Some commands I wish to execute remotely:
cd $HOME; ./start_script.sh; wget 192.168.1.110/monitor.sh; chmod +x monitor.sh; ./monitor.sh
I know there is a utility called "sshpass" but not sure how I could apply this utility for my needs.
I am open to any ideas in order to fulfill my need, any help would be very appreciated!
Thanks
Did you think about using ssh-keys (check man ssh-keygen)? You will be able to connect without password input ...
However if you cant, just try:
for i in $(< your_file); do
user=$(echo $i | cut -d: -f1)
pass=$(echo $i | cut -d: -f2)
ip=$(echo $i | cut -d: -f3)
sshpass -p $pass ssh $user#$ip bash -c "you commands &"
done
Instead of using cd $HOME use the full path with yours scripts names.And dont forget the & for send the process in background...

How to send email from remote server for particular user via unix shell script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I need to send mail to a particular user from host computer via shell script using command line argument.Mail should go to that particular user only.
Script execution will be like ./tesh.sh user emailid
cat test.sh
#!/bin/sh
export user=$1
export email=$2
list=`echo "$(cat ip.txt)"`
script=$(cat remote_cmds.sh)
for ip in ${list[#]} ; do
echo "***";
echo "IP: $ip"
ssh -o StrictHostKeyChecking=no -l ${user} ${ip} ${script}
done
# cat remote_cmds.sh
source pathto/test.sh
echo "";
echo "****Hostname****";
echo "`hostname`";
echo "";
echo "****Disk Allocated****";
echo "`df -h`";
echo "";
echo "****Ram Allocated****";
echo testmail | mailx -s "Testmail" $email
If I understand you correctly you have N computers with ip addresses saved in ip.txt. Each of these computers has a user named 'user' to which you can login via ssh. Each of these computers has a user named 'emailid' to which you want to send mail. You want to use ssh session to connect to these computers and run mail client on these computers. You want to print some information about hostname, disc space and some other infos from the remote computers on your computer and send simple mail with subject 'Testmail' and content 'testmail' to the local user 'emaillid' on these remote N computers.
These scripts are not good. Doing script=$(cat remote_cmds.sh) and then calling ssh ... ${scripts} is so dangerous... The line list=echo "$(cat ip.txt)" can be just shortened to list=$(cat ip.txt). list variable is not an array, so doing ${list[#]} is the same as $list. remote_cmds.sh has the line source pathto/test.sh, so that means that each of N computers has a test.sh file in the same path?
mailx is giving you arror, cause you are executing (literally) mailx -s Testmail $email and email address can't have $ characters (the email variable is not expanded).
Try smth like this:
test.sh
#!/bin/sh
# export is not needed anywhere here
user=$1
email=$2
# loop through the lines in ip.txt
cat ip.txt | while read ip; do
echo "***";
echo "IP: $ip"
# https://unix.stackexchange.com/questions/87405/how-can-i-execute-local-script-on-remote-machine-and-include-arguments
# login on computer with ip $ip on user $user and execute command on
# remote host 'bash -s', which will read commands from standard input,
# append our remote_cmds.sh script to stdin and pass "$email" as first
# argument to this script
ssh -o StrictHostKeyChecking=no -l "$user" "$ip" bash -c -- < remote_cmds.sh "$email"
done
# this loop is bad and inefficient, but it's safe and simple to write
# see https://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash
remote_cmds.sh
#!/bin/sh
# these lines will be executed on N computers
# email is passed as first argument to this script
email=$1
echo "";
echo "****Hostname****";
hostname
echo "";
echo "****Disk Allocated****";
df -h
echo "";
echo "****Ram Allocated****";
free -h # ;)
echo testmail | mailx -s "Testmail" "$email"

Show file contents after searching word Done [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to display the file contents after searching for a word. If the word is found, display the file.
My code is below:
GNU nano 2.2.6 File: work
#!/bin/bash
while read -p "Welcome what would you like to do (S) to search or (Q) to quit " option
do
case $option in
"S") echo "What is the name of the file you would like to search for?"
read file
echo "What word would you like to find in the file?"
read word
grep -q $word $file
if [ $? -eq 0 ]; then
echo "$word found in $file"
cat $file
else
echo "$word NOT found in $file"
fi
;;
"Q") echo "Goodbye!"
exit ;;
*) echo "invalid option" ;;
esac
done
Replace
echo $file
with
cat $file
I believe you are looking for command cat $file. Stick it inside of your if block.
I need to load up what a file says with out loading up the file.
There is no way to access the contents of the file without accessing the file.
grep -l word file | xargs -r cat
shows file content if word is found. This also shows name of file
grep -l word file | xargs -r -i bash -c "echo {}:; cat {}"

Resources