The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Calling expect scripts from other expect scripts seva Shell Programming and Scripting 0 04-03-2008 10:45 AM
FTP Scripts raghav1982 Shell Programming and Scripting 27 12-07-2007 02:38 AM
use of ssh in scripts esham Shell Programming and Scripting 7 11-16-2005 07:27 AM
Help with GDL to SQL scripts ch4r1e5 Shell Programming and Scripting 0 10-31-2005 01:51 PM
using su in scripts uchachra UNIX for Dummies Questions & Answers 3 03-11-2002 09:24 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-30-2007
Registered User
 

Join Date: Jun 2007
Location: Pennsylvania
Posts: 40
Exclamation Help with scripts

Hello all. I'm writing a script and I've run into a wall.

Basically what I want it to do, is mirror a users settings across different servers. I'm a newly employe'd AIX/Linux administrator, and I'm just trying some different thing. I work on 120 different servers, so if we can automate something - we do.

Anyway, the problem is, how do I pull group information on a user? Whenever I try var=`id $user -gn` It will not work.


I'll bold parts of the scripts that won't work. The other parts work.


###################################
# Server mirror script.
# Mirrors a user's information on one server, and adds the exact information on another server.
###################################

# Variables
id=accessid

clear

echo "What user do want to mirror?"
read user1
echo " "
echo " "

echo "What server are we mirroring this user from? (Originating server)"
echo "(E.G awojnare on p595_202, mirror all attributes over to focus, comet and falcon.)"
read server1
echo " "
echo " "

echo "What server/s are we mirroring TO. Remember: Space's are delimited!"
echo "(For the E.G above, this would be focus comet falcon .)"
read list
echo " "
echo " "
echo "Please hold while I slave over this for you."
sleep 2
echo ".."
echo " "

sleep 2
echo "...."
echo " "

#Grab information
for abcdefg in `echo $server1`
do
ssh -q $id@$abcdefg zaquid="sudo cat /etc/passwd | grep $user1 | cut -f3 -d ":""; primgroup="`id -Gn $user1`"

#debugging - test the variables.
echo $abcdefg
echo $zaquid
echo $primgroup
echo $grouplist
echo $server1
done

sleep 2

echo " "

echo "Okay, I got your information from $server1 and I'm going to add your information to server/s $list"
sleep 2
echo ".."

echo " "

Information out to the servers
for servera in $list
do
ssh -q $id@$servera "usermod -d /home/${user1} -g $primgroup -Gn $grouplist -u $uid -p apple1 $user1"
done

sleep 2

echo " "

echo "Okay. ${user1}'s info, on $server1, has been mirrored over to ${list}."
echo "${user1}'s password has been set to apple1."

exit 0









My SECOND problem is: how do I run a loop within a SSH session? I'm writing this other script, that will sync up UID's across the 120 servers. It does not seem to want to work. I tried writing declaring a function, and then running the function within the SSH session (which is already in a for loop.)

How would I write a nested for loop that would do this? How does one run a loop that SSH's out to all the servers in a predefined list, and then run a loop within that?

Here is the code:

function tmplist {
for tmp in $tmpvar
do
chown $UID $tmp
done
}

# Change the ownership
for server3 in $list
do
ssh -q $id@$server3 "`tmplist`"

done


All the other variables are declared through read. Can someone help me? The function isn't working. I need to SSH out to all the servers. Everything else in the script works, but when I try and change the ownership of the old person's UID, it will not work with that function.

Here is the full code:






#############################################################
#This script will sync UID across AIX/Linux servers.
##############################################################

#----------------------- Variables -----------------------#
#id=`whoami`
id=accessid
tmpvar="/tmp/{$USER}uid.tmp"


#----------------------- Functions -----------------------#
function check1 {
sudo grep -x $UID /etc/passwd | cut -d: -f3
}

function check2 {
sudo id -u $USER
}

function tmplist {
for tmp in $tmpvar
do
chown $UID $tmp
done
}

#----------------------------------------------------------#

clear
echo "\n \n \n"
echo "This script will sync UID's across multiple servers."
echo "It will also find all files owned by the user and set the ownership to the new UID."

echo
USER=
while [ -z "$USER" ] ;
do
echo "What user has an incorrect UID on the servers? \c"
read USER
if [ -z "$USER" ] ; then
echo "You must enter a valid User id...!"
USER=
fi
done

echo
UID=
while [ -z "$UID" ] ;
do
echo "What is the correct UID for $USER ? \c"
read UID
if [ -z "$UID" ] ; then
echo "You must enter a UID...!"
UID=
fi
done

echo
LIST=
while [ -z "$list" ];
do
echo
echo "** Which servers do you want to sync UID's up on?"
echo "** List the servers, space delimited (e.g., charger cobra cuda) \n \t \c"
read list
if [ -z "$list" ]; then
echo "You have to enter at least one server!"
list=
fi
done

sleep 2
echo " "
echo " "

echo "Searching to see if the UID is being used on any user on $list."
for server0 in $list
do
ssh -q $id@$server0 "variable1=`check1`"
if [ "variable1 -ne $UID" ]; then
echo "No UID found"
else
echo "UID already in use!!"; exit
fi
done

sleep 2
echo " "
echo " "

echo "Searching to see if the UID is already being used by $USER"
for server00 in $list
do
ssh -q $id@$server00 "variable2=`check2`"
if [ "$UID -ne $variable2" ]; then
echo "UID's are not the same. You may proceed"
else "$USER already has an ID of $UID"; exit
fi
done
echo " "
echo " "
sleep 2
echo " "
echo "Searching for files owned by $USER, on the following servers: $list."
echo "Hold.."
echo "Any file found will be dumped into the temp file: /tmp/uid.tmp"
echo " "

for server in $list
do
ssh -q $id@$server "sudo find / -user $USER | grep -v proc | grep -v dev > /tmp/{$USER}uid.tmp"
done

sleep 2

# Change the UID
for server2 in $list
do
ssh -q $id@$server2 "sudo usermod -u $UID $USER"
echo "If this fails, the UID you're trying to change to is probably in use already"
done

sleep 2

# Change the ownership
for server3 in $list
do
ssh -q $id@$server3 "`tmplist`"

done



sleep 2

# Delete the temp files.
#for server4 in $list
#do
# ssh -q $id@$server4 "sudo rm /tmp/{$USER}uid.tmp"
#done
d
echo "All finished!"

exit 0
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-01-2007
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,211
Quote:
Originally Posted by syndex View Post
#Grab information
for abcdefg in `echo $server1`
do
ssh -q $id@$abcdefg zaquid="sudo cat /etc/passwd | grep $user1 | cut -f3 -d ":""; primgroup="`id -Gn $user1`"

#debugging - test the variables.
echo $abcdefg
echo $zaquid
echo $primgroup
echo $grouplist
echo $server1
done
Try something like that (not tested):
Code:
server1=$(echo $server1)
zaquid=$(ssh -q $id@$server1 "id -u $user1")
primgroup=$(ssh -q $id@$server1 "id -gn $user1")
grouplist=$(ssh -q $id@$server1 "id -Gn $user1")
Quote:
Originally Posted by syndex View Post
function tmplist {
for tmp in $tmpvar
do
chown $UID $tmp
done
}

# Change the ownership
for server3 in $list
do
ssh -q $id@$server3 "`tmplist`"

done
Try (not tested) :
Code:
chown_command=""
for tmp in $tempvar
do
   chown_command="${chown_command}chown $UID $tmp;"
done

for server3 in list
do
   ssh -q $id@$server3 "${chown_command}"
done
Reply With Quote
  #3 (permalink)  
Old 07-01-2007
Registered User
 

Join Date: Jun 2007
Location: Pennsylvania
Posts: 40
Thank you sir!


I'll try it out when I get to work on Monday.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:20 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0