ユーザーを削除するには、Bashスクリプトを作成する必要があります。
私たちはRHELバージョン4、5、6を使用しています。
ユーザー名が Ray4 と Ray6 でスクリプト名が deal であるとします。
このスクリプトの具体的な操作は次のとおりです。
- ユーザーは存在しますか?
- ユーザーが存在する場合は、ユーザーの/homeディレクトリをバックアップし、ユーザー名を削除して/root/DeletedUsersに入れます。
- /root/DeletedUsers ディレクトリがない場合は新規作成します。
- このユーザーのファイアウォールルールがある場合は、そのルールの結果とどのノードの結果を電子メールで送信してください。
- ユーザーがsudoersに存在する場合は、削除せずにコメントアウトしてください。
これが私が今まで持っているものです。 RHN Satelliteで実行する前に動作していることを確認したいと思います。推奨事項を変更した後。これが今現れる新しいエラーです。
[root@localhost bin]# ./deal
./deal: line 7: [[!: command not found
Usage: userdel [options] LOGIN
Options:
-f, --force force removal of files,
even if not owned by user
-h, --help display this help message and exit
-r, --remove remove home directory and mail spool
-Z, --selinux-user remove SELinux user from SELinux user mapping
Usage: userdel [options] LOGIN
Options:
-f, --force force removal of files,
even if not owned by user
-h, --help display this help message and exit
-r, --remove remove home directory and mail spool
-Z, --selinux-user remove SELinux user from SELinux user mapping
Null message body; hope that's ok
./deal: line 22: [: -me: binary operator expected
This is source code:
[root@localhost bin]# cat -n deal
1 #!/bin/bash
2
3 count=$(egrep -c Ray[46] /etc/passwd)
4 firewall=$(grep -c "192.168.5.5" /etc/sysconfig/iptables)
5 doers=$(egrep -c Ray[46] /etc/sudoers)
6
7 if [[! -d /root/DeletedUsers]]
8 then mkdir /root/DeletedUsers
9
10 fi
11
12 cp -Rf /home/Ray[46] /root/DeletedUsers
13 userdel -rf Ray [4]
14 userdel -rf Ray [6]
15
16 if [ $firewall -ne 0 ]
17
18 then mail -s "$firewallrulesexist" emailaddress < /dev/null
19
20 fi
21
22 if [ $doers -me 0 ]
23 then sed ^Ray[46] /#/i
24
25 EOF
26 fi
ベストアンサー1
これを使用して、getent
ユーザーが存在することを確認することをお勧めします。このスクリプトが役に立ちます。
##Check if the user exists using getent command
user_exists=$(getent passwd $1)
##If the user doesn't exist, we need not perform the backup.
if [ -z "$user_exists" ]
then
echo "User $1 does not exist"
else
cd /root
#Incorporating derobert's suggestion here.
mkdir -p "DeletedUsers"
##Use tar or rsync to do the backup as cp will be inefficient.
tar czvf "$1".tar.gz /home/"$1"
firewall=$(grep -c "192.168.5.5" /etc/sysconfig/iptables)
doers=$(egrep -c "$1" /etc/sudoers)
userdel -rf "$1"
if [ $firewall -ne 0 ]
then
mail -s "$firewallrulesexist" emailaddress < /dev/null
fi
if [ $doers -ne 0 ]
then
sed ^"$1" /#/i
fi
fi
スクリプトを呼び出して./deal ray[4]
から他のユーザーに対して実行する必要がある場合は、それを使用して呼び出すことができます./deal ray[6]
。まだシステムでスクリプトをテストしていないが、後で他のユーザーを削除する必要がある場合に役立ちます。スクリプトを変更する必要はありませんが、ユーザー名をパラメータとして呼び出すだけです。
編集する
derobertの提案に従って-p
フラグを使用すると、ディレクトリテストを省略できます。