Skip to content

Instantly share code, notes, and snippets.

@liaohuqiu
Created February 28, 2017 03:57
Show Gist options
  • Save liaohuqiu/ba6277fe41b0018b003480394adabe27 to your computer and use it in GitHub Desktop.
Save liaohuqiu/ba6277fe41b0018b003480394adabe27 to your computer and use it in GitHub Desktop.
sourceServer=172.16.1.121
function syncusers() {
scp $sourceServer:/etc/passwd /tmp/passwd.$sourceServer
scp $sourceServer:/etc/group /tmp/group.$sourceServer
scp $sourceServer:/etc/shadow /tmp/shadow.$sourceServer
# First, make a list of non-system users that need to be moved.
local UGIDLIMIT=500
awk -v LIMIT=$UGIDLIMIT -F: '($3 >= LIMIT) && ($3 != 65534)' /tmp/passwd.$sourceServer > /tmp/passwd.mig
awk -v LIMIT=$UGIDLIMIT -F: '($3 >= LIMIT) && ($3 != 65534)' /tmp/group.$sourceServer >/tmp/group.mig
awk -v LIMIT=$UGIDLIMIT -F: '($3 >= LIMIT) && ($3 != 65534) { print $1 }' /tmp/passwd.$sourceServer \
| tee - |egrep -f - /tmp/shadow.$sourceServer > /tmp/shadow.mig
# Now copy non-duplicate entries in to the new server files...
while IFS=: read user pass uid gid full home shell
do
line="$user:$pass:$uid:$gid:$full:$home:$shell"
exists=$(grep $user /etc/passwd)
if [ -z "$exists" ]
then
echo "Copying entry for user $user to new system"
echo $line >> /etc/passwd
fi
done </tmp/passwd.mig
while IFS=: read group pass gid userlist
do
line="$group:$pass:$gid:$userlist"
exists=$(grep $group /etc/group)
if [ -z "$exists" ]
then
echo "Copying entry for group $group to new system"
echo $line >> /etc/group
fi
done </tmp/group.mig
while IFS=: read user pass lastchanged minimum maximum warn
do
line="$user:$pass:$lastchanged:$minimum:$maximum:$warn"
exists=$(grep $user /etc/passwd)
if [ -z "$exists" ]
then
echo "Copying entry for user $user to new system"
echo $line >> /etc/shadow
fi
done </tmp/shadow.mig
}
syncusers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment