reduce ssh calls / cleanup


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reduce ssh calls / cleanup
# 1  
Old 09-08-2011
reduce ssh calls / cleanup

all of our *nix systems are using local user accounts still, so i have come up with a management script to handle useradds / deletions / password resets etc. It is functional, but now i am trying to go through and reduce the number of ssh calls and clean up my messy coding. As it sits right now, this little section could potentially have 8 different ssh connections for just this little function..
I have searched all over to see if there is a way for me to gather more of the information that i am needed to run this code more effeciently (ie less ssh calls) but am having a hard time finding a way to set local variables from remote systems without doing a
VARIABLE=`ssh some_command`
for each one i need to gather

This is just one function in the script so there are things that are ommited such as the gathering of the arrays etc, but if anyone could give me some pointers or ideas for this section, i am sure i could adapt them to the other functions of my script

Code:
#!/bin/bash
function delete_user
{
set -x
i=0
for t in ${serverlist_array[@]}; do
SERVER=${serverlist_array[$i]}
# get the servers OS version and type
OSVER=`ssh $USER@$SERVER uname -r`
OSTYPE=`ssh $USER@$SERVER uname -s`
 
# get path to run root commands
if [ $OSVER = "5.10" ]; then
ADMINCMD=/usr/bin/pfexec
elif [ $OSTYPE = "Linux" ]; then
ADMINCMD=sudo
elif [ ! -z `ssh $USER@$SERVER ls /usr/local/bin/sudo` ]; then 
ADMINCMD=/usr/local/bin/sudo
elif [ ! -z `ssh $USER@$SERVER ls /opt/csw/bin/sudo` ]; then
ADMINCMD=/opt/csw/bin/sudo
fi
 
# determine if account is local or not
if ssh $USER@$SERVER grep -w "/export/home/$USERNAME" /etc/passwd
then
LOCALHOME="true"
else
LOCALHOME="false"
fi
 
 
# if the user account doesn't exists, write that out to log file
if [[ -z `ssh $USER@$SERVER cat /etc/passwd | grep -w $USERNAME` ]]; then 
echo "$SERVER Account_Doesnt_Exist" >> /tmp/passwdlist
fi
 
# While the user account exists on this server, create the account
while [[ -n `ssh $USER@$SERVER cat /etc/passwd | grep -w $USERNAME` ]]
do
# Determine is home directory lives local or on nas. Remove if local..
if [ $LOCALHOME = "true" ]; then
echo "Deleting user account AND local home directory on $SERVER" 
ssh -t $USER@$SERVER $ADMINCMD /usr/sbin/userdel -r $USERNAME
else 
echo "Deleting user account on $SERVER"
ssh -t $USER@$SERVER $ADMINCMD /usr/sbin/userdel $USERNAME
echo "PLEASE REMEMBER TO DELETE THE USERS HOME DIRECTORY ON THE HNAS!!"
fi
 
if [ -z `ssh $USER@$SERVER cat /etc/passwd | grep -w $USERNAME` ]; then
echo "$SERVER Account_Deleted" >> /tmp/passwdlist
else
echo "$SERVER Deletion_FAILED!!" >> /tmp/passwdlist
fi
 
done
i=$((i+1))
done
}

# 2  
Old 09-08-2011
You can reduce the number of ssh connections by doing more things with one ssh call. You can run entire scripts, not just single commands:
Code:
$ ssh username@host /bin/sh -s a b 3 <<"EOF"

        # Note that 'read' won't work as expected inside the <<"EOF" block
        # because stdin isn't the terminal, it's the script!

        echo
        echo "arg1 is $1"
        echo "arg2 is $2"
        echo "arg3 is $3"

        [ "$3" -gt 1 ] && echo "$3 > 1"

        exit 42
# This EOF must be at the beginning of the line
EOF
Password:

arg1 is a
arg2 is b
arg3 is 3
3 > 1
$ echo "ssh returned $?"
ssh returned 42
$

---------- Post updated at 10:16 AM ---------- Previous update was at 10:11 AM ----------

Also, you have some useless use of cat and useless use of backticks in there.
Code:
if [ -z `ssh $USER@$SERVER cat /etc/passwd | grep -w $USERNAME` ]

You don't need 'cat' there. You don't even need to check the output string, just grep's return value, so you don't need backticks either.

This is how I'd simplify some of your code:

Code:
ssh username@host /bin/sh -s "$username" <<"EOF"
        if ! /usr/sbin/userdel "$1"
        then
                echo "Couldn't delete remote user"
                exit 1
        fi

        if grep -w "$1" /etc/passwd > /dev/null
        then
                echo "Deleting user failed"
                exit 1
        fi

        echo "Deleting user succeeded"
        exit 0
EOF

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 09-08-2011
i appreciate the pointers.. i may just have my scripts built to do things a non-optimal way, but because of the way they are currently designed, your examples dont appear to work for my situation.

My script(s) all run from a single server (doesnt matter which since it is a shared nas mount) and goes out and executes the commands on to the remote systems.. so for your example of
Code:
ssh username@host /bin/sh -s "$username" <<"EOF"
        if ! /usr/sbin/userdel "$1"
        then
                echo "Couldn't delete remote user" >> /tmp/passwdlist
                exit 1
        fi
 
        if grep -w "$1" /etc/passwd > /dev/null
        then
                echo "Deleting user failed" >> /tmp/passwdlist
                exit 1
        fi
 
        echo "Deleting user succeeded" >> /tmp/passwdlist
        exit 0
EOF

i am not able get the echo'd information back to the source server that is running it since it is running the echo on the remote system side. I use that info for the synopsis once it is done looping thru all servers..

For the other tip you gave me to reduce my ssh calls, again the issue is that i can echo the arguments, but i can't use that information since the source server knows nothing of the arguments on the remote system.. So my echo will return the uname output, but the source can't use that to determine what is needed to run the commands as root..

although everything you had helped with is functionally correct, it doesnt work for my script layout.. This is most likely an issue of my script(s) not being laid out the best way. Should i instead be pushing the scripts execution off to the remote servers instead of trying to run it all from a single source?

again, i do truly appreciate the help.. i seem to be able to come up with functional scripts, but i am still learning how to make clean, effecient, functional scripts!
# 4  
Old 09-08-2011
If you want it saved on the local side, save it on the local side:
Code:
ssh username@host /bin/sh -s "$username" >> /tmp/passwdlist <<"EOF"
        if ! /usr/sbin/userdel "$1"
        then
                echo "Couldn't delete remote user"
                exit 1
        fi
 
        if grep -w "$1" /etc/passwd > /dev/null
        then
                echo "Deleting user failed"
                exit 1
        fi
 
        echo "Deleting user succeeded"
        exit 0
EOF

# 5  
Old 09-08-2011
ok.. that makes sense.. however the issue that i now run into with that, is i get all of the other shell output in addition to the "Deleting user succeded" output
Code:
cat /tmp/passwdlist
sh-3.2$ > > > > sh-3.2$ sh3.2$ > > > > sh-3.2$ sh3.2$ Deleting user succeeded

that makes sense, because this is all the info in stdout, but the only thing i need is the echo that says the status

besides coming back around and removing all the extraneous content in the file with something like sed/awk, is there anyway to overcome this?
# 6  
Old 09-08-2011
That's very odd -- a shell run noninteractively really shouldn't be doing that! Try exec /bin/sh instead of just /bin/sh. If that doesn't work try an alternative shell, /bin/ksh or /bin/bash or what have you, instead of basic sh...
# 7  
Old 09-08-2011
Umm that is very odd, you could also try just expanding the variables in the read-from string:

Code:
ssh adminuser@host /bin/sh >> /tmp/passwdlist <<EOF
        if ! /usr/sbin/userdel "$deluser"
        then
                echo "Couldn't delete remote user"
                exit 1
        fi
 
        if grep -w "^$deluser" /etc/passwd > /dev/null
        then
                echo "Deleting user failed"
                exit 1
        fi
 
        echo "Deleting user succeeded"
        exit 0
EOF


Last edited by Chubler_XL; 09-08-2011 at 08:43 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX script for cleanup

Hello, I need some help from unix guru's here..I am looking for some advanced level script to cleanup the directories and files from specific directories under a file system.. The folders are created under /opt/modules And under modules, there are multiple subfolders with the application... (6 Replies)
Discussion started by: mb525
6 Replies

2. Red Hat

How to Cleanup Multipathing

I have a server running redhat 5.5 and it has one SAN device presented to it as LUN9. How can I clean up the remaining entries. I cannot afford to interupt the service. Please assist. # multipath -l mpath0 (36000097000019260298953666633436) dm-11 EMC,SYMMETRIX \_ round-robin 0 \_ 2:0:0:9 ... (2 Replies)
Discussion started by: Tirmazi
2 Replies

3. UNIX for Advanced & Expert Users

Table Cleanup Script

I needed some help with a script to fetch and delete all records prior to 3 days from now connecting to sybase from sunos. I wrote the following script but not working..can someone please guide me with my code. Thanks #!/bin/ksh ##GET PREVIOUS DAY DATE dt=`date | awk... (3 Replies)
Discussion started by: moe458
3 Replies

4. Shell Programming and Scripting

Cleanup between parenthesis

Hi, I am trying to clean up data between parenthesis () in a file. See example below.... Input File : (New York) Chicago (London) New York (Chicago) London New York Chicago (London) (New York) (Chicago) (London) New York (Chicago) ... (3 Replies)
Discussion started by: msalam65
3 Replies

5. Solaris

/home cleanup

Hi All, I have this script for linux on cleaning up orphaned folder. But I need to use this on solaris 8/9/10 for user in $(ls | grep -v lost+found) ; do id $user >/dev/null 2>&1 if ] then ls -ld $user grep $user /etc/passwd fi done Can someone please convert this script? ... (1 Reply)
Discussion started by: itik
1 Replies

6. Shell Programming and Scripting

Cleanup script

Hi! I would like to write a script which remove some files, all beginning with the same prefix : prefix.1 doc/prefix.2 ../prefix.3 etc. So, I would create a file and chmod it executable. But I dont know how to pass a variable to a script. I would like to write something like ... (2 Replies)
Discussion started by: tipi
2 Replies

7. Shell Programming and Scripting

Help with cleanup

I am trying to add a unique string to a variable to prevent some name space collisions. DATAFILE=/u001/app/unica/affinium644/campaign/partitions/limited/tmp/ebf9aaah.t~# DATETIME=`date +%Y%m%d_%H%M%S` echo $DATAFILE > tmpnme.txt sed 's_/_ _g' tmpnme.txt > tmpnme2.txt DATA=$(cat tmpnme2.txt)... (2 Replies)
Discussion started by: whdr02
2 Replies

8. IP Networking

Identification of data calls & voice calls

Is there any facility to filter/identify the data calls and voice calls coming throug modem? OR Can we get the data or voice calls information through a script(preferably C Kermit)? (0 Replies)
Discussion started by: pcsaji
0 Replies

9. AIX

Login ID cleanup

Hello I have many old IDs on my AIX and would like to know the simplest way of knowing the last time an ID was used. I am familiar with the "last" command. Thanks for any info :) (1 Reply)
Discussion started by: MILLERJ62
1 Replies

10. UNIX for Dummies Questions & Answers

sendmail cleanup

What is the correct procedures to clean up /var/spool/mqueue? Any help appreciated. This directory gets really clogged up at times. :( :( (1 Reply)
Discussion started by: thomi39
1 Replies
Login or Register to Ask a Question