Help with scripts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with scripts
# 1  
Old 06-30-2007
Error 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
# 2  
Old 07-01-2007
Quote:
Originally Posted by syndex
#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
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

# 3  
Old 07-01-2007
Thank you sir!


I'll try it out when I get to work on Monday.SmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling multiple scripts from another scripts

Dear all, I am working on script which call other shell scripts in a loop but problem is from second script am not able to come out. Here is the snippet:- #!/bin/bash HSFILE=/root/Test/Components.txt LOGFile=/opt/domain/AdminDomain/application/logs... (3 Replies)
Discussion started by: sharsour
3 Replies

2. Shell Programming and Scripting

Calling scripts from with scripts

Hi all, I'm wondering if you could give me some advice. I am new to scripting and am getting rather frustrated that i can get my script to call another script if certain criteria is met, via command line, but I cannot get the same script to work thru the cron jobs. My first script monitors... (8 Replies)
Discussion started by: echoes
8 Replies

3. Shell Programming and Scripting

KSH - How to call different scripts from master scripts based on a column in an Oracle table

Dear Members, I have a table REQUESTS in Oracle which has an attribute REQUEST_ACTION. The entries in REQUEST_ACTION are like, ME, MD, ND, NE etc. I would like to create a script which will will call other scripts based on the request action. Can we directly read from the REQUEST_ACTION... (2 Replies)
Discussion started by: Yoodit
2 Replies

4. Shell Programming and Scripting

Changing the Bash Scripts to Bourne Scripts:URGENT

Hi, I have to write a program to compute the checksums of files ./script.sh I wrote the program using bash and it took me forever since I am a beginner but it works very well. I'm getting so close to the deadline and I realised today that actually I have to use normal Bourne shell... (3 Replies)
Discussion started by: pgarg1989
3 Replies

5. Shell Programming and Scripting

Running scripts within scripts from cron

Hi all, I have set up a cron job which calls another shell script shell script which in turn calls a Java process. The cron tab looks so. 0,30 7-18 * * 1-5 /u01/home/weblogic/brp/bin/checkstatus.sh >> /u01/home/weblogic/logs/checkstatus.log The checkstatus.sh scripts looks like this. ... (4 Replies)
Discussion started by: sirbrian
4 Replies

6. Shell Programming and Scripting

Help with Script using rsh and scripts within scripts

Hi, I've written a script that runs on a Database server. It has to shutdown the Application server, do an Oracle Dump and then restart the Application server. Its been a long time since I wrote any shells scripts. Can you tell me if the scripts that I execute within my script will be executed... (3 Replies)
Discussion started by: brockwile1
3 Replies

7. UNIX for Dummies Questions & Answers

Profile scripts versus rc scripts....

what is the difference between login and profile scripts versus the rc scripts? (1 Reply)
Discussion started by: rookie22
1 Replies

8. Shell Programming and Scripting

Calling expect scripts from other expect scripts

Hi, First, let me explain the issue I am trying to solve. We have a lot of expect scripts with the duplicated send/expect commands. So, I'd like to be able to extract the duplicated code into the common scripts that can be used by other scripts. Below is my test where I am trying to call... (0 Replies)
Discussion started by: seva
0 Replies

9. UNIX for Dummies Questions & Answers

Help with scripts

script that ask for "enter a file name" and removes that file and asks for confirmation before deletion if executed the output might look as enter the filename you intent to deleted remover file? Y file deleterd I knwo the comand I would use find . -name *.* -ok rm {}\; I guess... can... (1 Reply)
Discussion started by: LiTo
1 Replies
Login or Register to Ask a Question