LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-13-2016, 02:59 PM   #1
giant45man
LQ Newbie
 
Registered: Sep 2015
Posts: 4

Rep: Reputation: Disabled
Script To Delete Users


Hello All,

The servers I am working with are Redhat 6.4

I am trying to create a script that will contain a list of server names, it will ssh into each server and delete a specific user account and home directory. I have sudo rights that give me root privileges. I am attempting to use the script below, without success. Can someone please assist me with this?

Thanks,

Kenneth

#!/bin/sh
#Variables
_list_of_servers="ROSCOE"
_list_of_users="tuser"


# Main Code
for _server in $_list_of_servers
do
echo "$_server OS Level:" ssh $_server "hostname"
#echo -e
ssh $_server
for user in $_list_of_users
do
sudo userdel -r $user
done
done
#
 
Old 05-13-2016, 03:30 PM   #2
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,093

Rep: Reputation: 197Reputation: 197
Quote:
Originally Posted by giant45man View Post
The servers I am working with are Redhat 6.4
Are you aware that RedHat 6.4 is End Of Life?

Quote:
Originally Posted by giant45man View Post
I am trying to create a script that will contain a list of server names, it will ssh into each server and delete a specific user account and home directory. I have sudo rights that give me root privileges. I am attempting to use the script below, without success. Can someone please assist me with this?
Sounds like a good way to accidentally screw things up.



Quote:
Originally Posted by giant45man View Post
#!/bin/sh
#Variables
_list_of_servers="ROSCOE"
_list_of_users="tuser"


# Main Code
for _server in $_list_of_servers
do
echo "$_server OS Level:" ssh $_server "hostname"
#echo -e
ssh $_server
for user in $_list_of_users
do
sudo userdel -r $user
done

done
#
When posting code use CODE tags for readablity. Look for the # button in the toolbar when composing.

Code:
echo "$_server OS Level:" ssh $_server "hostname"
What's that supposed to do? It looks like two commands on the same line, but there's no ; between then. What is presumably the second command is ssh in to the server and run hostname, which I don't see the point of. You know the hostname already, you used it to ssh in to the server.

If you want to ssh in to a machine and specify commands to run on it you need to do it like


Code:
ssh $_server "something; otherthing; morething"
Given you're asking for help to write a script like this you probably shouldn't be trying to use such a script, at least not on any machines which you care about the integrity of. How many servers are you dealing with? Unless it's more than about, I dunno, 10, 20, then you may very well be better of just doing this task manually until you are more confident enough with automating it you don't need to ask for help on the Internet. If you're having to log in to each server and then sudo you're doing to be typing your password a lot anyway. (Unless your user has rights to run sudo without a password, which is probably a bad idea.)


If you regularly need to make significant changes like this to a large number of servers you may wish to consider learning Ansible or similar. E.g. here's the documentation for the module in Ansible which lets you specify users to create or delete. http://docs.ansible.com/ansible/user_module.html
 
Old 05-13-2016, 03:32 PM   #3
Wells
Member
 
Registered: Nov 2004
Location: Florida, USA
Distribution: Debian, Redhat
Posts: 417

Rep: Reputation: 53
Hrm... looks to me like you need to combine some things on your ssh command...

Code:
#!/bin/sh
#Variables
_list_of_servers="ROSCOE"
_list_of_users="tuser"


# Main Code
for _server in $_list_of_servers
do
echo "$_server OS Level:" ssh $_server "hostname"
#echo -e
ssh $_server '_list_of_users=$_list_of_users; for user in $_list_of_users; do sudo userdel -r $user; done'
done
#
I haven't tested this yet, but basically you have to pass the list of users and then run the inner for loop within the same ssh command.
 
Old 05-13-2016, 03:34 PM   #4
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
There might be a way to one-line it, and I have not tested it so may require some tweaking of the quotes, but generally:

Code:
!/bin/sh
#Variables
_list_of_servers="ROSCOE BOSCO"
_list_of_users="tuser zuser"


for _server in $_list_of_servers
do
  echo "$_server OS Level:" ssh $_server "hostname"
  for user in $_list_of_users; do ssh $_server "'"sudo userdel -r $user"'"; done
done
Commands would be like

Code:
ROSCOE OS Level: ssh ROSCOE hostname
ssh ROSCOE 'sudo userdel -r tuser'
ssh ROSCOE 'sudo userdel -r zuser'
BOSCO OS Level: ssh BOSCO hostname
ssh BOSCO 'sudo userdel -r tuser'
ssh BOSCO 'sudo userdel -r zuser'
 
Old 05-13-2016, 04:23 PM   #5
giant45man
LQ Newbie
 
Registered: Sep 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
FYI.....I have about 100 servers to run this script on.

Thank you so much for your insight. I will try a few of your suggestions and I will let you know what happens.
 
Old 05-13-2016, 04:27 PM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
Quote:
Originally Posted by giant45man View Post
I have sudo rights that give me root privileges
On the whole list of servers?
Edit
100 servers? ouch...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[awk script] Help me delete lines in a file using script ? sieukid Programming 5 03-20-2012 01:23 PM
can i delete all the users except...... wangjinyi Linux - Security 10 10-23-2005 09:04 PM
Delete group users from /etc/passwd SCRIPT wesleywest Linux - General 5 03-08-2005 09:59 AM
how to delete users? centr0 Linux - Newbie 11 03-10-2003 12:22 PM
how do i delete users? ShawnD Linux - General 9 07-22-2002 10:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration