Shell script for remote servers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script for remote servers
# 1  
Old 07-19-2016
Shell script for remote servers

Hi ,

I have written a small script :

Code:
set -x
#!/bin/ksh
for i in `cat /tmp/list` ( list contains remove servers )
do
ssh -t $i << EOF
uname -a
cd ~user
echo "Enter the dir >"
read dir
path=`ll -ld /home/user/"$dir"`
if [ "$dir" -eq "$path" ]; then
echo "Dir exists "
read
rm $path
else
echo "no such file"
fi
#echo $?
#rm test
#pwd
EOF
read
logout
done

The idea is to have a interactive script to delete directory under /home/user/ area from all the remote servers via ssh .. I have created list of server in /tmp/list to call the same in for loop. The problem is that the script does not work interactively and completes the entire loop for all servers without taking any options. Please provide your valuable inputs


Thanks
Kpatel.

Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
It makes it far easier to read and preserves spaces for indenting or fixed width data

Last edited by rbatte1; 07-19-2016 at 09:37 AM.. Reason: Added CODE tags
# 2  
Old 07-19-2016
The read command is trying to use stdin which is used by the here document (<< EOF). I would suggest trying something like this:
Code:
...
  ssh "$i" "$(
  cat << EOF
    uname -a
    cd ~user
    echo "Enter the dir >"
    read dir
    ......
EOF
  )"
......

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 07-19-2016
A few issues to tweak first:-
  • The set -x line will negate the 'shebang' line which forces the code to run under ksh. You may get away with it if you are running ksh as your shell already.
  • Some indenting you make it much clearer.
  • Wraping your code with [CODE] & [/CODE] tags will make it far easier to read and preserve the spacing for indenting or fixed width data.
  • You have what is known as a UUOC for your loop. I have a better way shown in green
  • The variable i is not very descriptive. Usually better to have a sensible name for variables so you can read your code as a natural language.
  • You use ll -ld where ll is an alias, often to ls -l but this output would surely never match what is being keyed in.
  • You prompt and read as two commands. There is a ksh way to do this in one shown in blue.

With indenting and the other adjustments, I get your code to be:-
Code:
#!/bin/ksh
set -x

while read servername          # File read at the done statement
do
   ssh -t $servername << EOF
   uname -a
   cd ~user
   read dir?"Enter the dir >"
   path=`ll -ld /home/user/"$dir"`
   if [ "$dir" -eq "$path" ]; then
      echo "Dir exists "
      read
      rm $path
   else
      echo "no such file"
   fi
   #echo $?
   # rm test
   #pwd
EOF

read
logout
done < /tmp/list

I must admit I am confused.

It's an automated process, but you seem to prompt for input. The ssh and the embedded read statements will be gobbling up your input for the file the way you had it, however I'm struggling to work out the purpose overall.

Can you elaborate on the logical you are trying to achieve. I've got:-
  • For every server in a list (file), connect and:-
    • Display the OS information
    • Change to the home directory of user user (if it exists)
    • Prompt for a directory, then try to match that to the current directory in an odd way
    • If it matches, delete the current directory whilst still in it (?) after pressing ENTER with no chance to escape.
    • If it doesn't match, just display a message
    • Disconnect ungracefully by terminating the input (EOF)
    • After pressing ENTER, logout! - oh, everything will exit, perhaps closing your window if it's PuTTY or something else set to close on exit.

Sorry, but I'm a bit lost as to the purpose. Can you lay out a description of what you are trying to achieve?

Perhaps the logout should be before the EOF as a starter.
Should this really be an interactive script within the ssh bit?


If you can lay it out, then I'm sure we can help you.



Kind regards,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 4  
Old 07-19-2016
Thanks for your response.. I have 500 odd servers which have some unused user directories , these directories need to be deleted.
I have one server which can be used to connect to all these servers via ssh wihout any password.
The idea is to put the entire server list is file and run a for loop and connect to all those servers do cd to the /home area .. then interactivily ask for the directory to be deleted. Once the input is provided it should delete the directory and then logout from that server to continue the loop for the remaining servers.

Hope I am able to explain what is required Smilie
# 5  
Old 07-19-2016
@rbatte1: if you replace the for loop with a while read loop, then this takes stdin away from ssh (like ssh was taking stdin away from the read statement from the remote execution script). So in addition to my suggestion in post #2 you need to read the server name from a different file descriptor:

Code:
while read server name  <&3        # File read at the done statement
do
  .....
done 3< /tmp/list

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 6  
Old 07-19-2016
Thanks all for you suggestion.. I will make the necessary change and then try. Thanks a lot again !!
# 7  
Old 07-22-2016
Hi ,

I created the final script as per your suggestion however, the script just runs and comes out of loop without asking any inputs:

Code:
:/home/kampatel=> ./working.sh
Pseudo-terminal will not be allocated because stdin is not a terminal.
Hpux server-1 B.11.31 U ia64 2459719568 unlimited-user license
Enter the dir >
no such file
Pseudo-terminal will not be allocated because stdin is not a terminal.
HP-UX  Server-2 B.11.31 U ia64 2191140766 unlimited-user license
Enter the dir >
no such file

Script:
------
Code:
#!/bin/ksh
for i in `cat /tmp/list`
do
  ssh -n -t "$i" "$(
  cat << EOF
    uname -a
echo "Enter the dir >"
    read path
if [ -d "$path" ]
then
echo "removing $path as per the input .."
rm $path
else
echo "no such file"
fi
exit
EOF
  )"
done

If written in while loop it also does the same thing completes the loop without asking any input or deleting the files.

Last edited by Scrutinizer; 07-22-2016 at 07:50 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Running set of commands in remote servers in shell script

Hi Wishing to all. I am very new joined in an organization as a unix system administrator. I need a help in preparing a script for a report. i have a file contains all of the linux/ubuntu servers line by line around 140 servers. vi servers.txt nh01 nh02 nh03 bh01 bh04 - - :wq (3 Replies)
Discussion started by: kumaraswamy
3 Replies

2. Shell Programming and Scripting

Remote login and running a script on multiple servers

Hi all, I am baffled on this. Solaris Irix system.:confused: I have 4 servers all connected to one another, :b: I need to write a script line that would login on to server 1-3 ($HOST) start a script in the back ground and log off while the back ground script runs over a length of time.:eek: ... (10 Replies)
Discussion started by: weddy
10 Replies

3. Shell Programming and Scripting

Bash script connect to remote servers and become root

Hi, I need a script that will connect to a list of servers and first sudo to root and then run a couple of commands. For security reasons, we can't setup ssh keys as root. Manually I have to login to a server as user and then sudo to root. It's not possible to use root@servername , because of... (8 Replies)
Discussion started by: misterx12345
8 Replies

4. Shell Programming and Scripting

Help with script to reach remote servers

new3=`cat /tmp/list3` for pol in "$new3" $(su - dbadmin -c "ssh $new3 '/usr//llist'"); do export policy=`echo $pol`; su - dbadmin -c "ssh $x '/usr/policycmd $policy -L |grep -i active; echo $policy'">>/tmp/listxyz;done I am having trouble with this testscript as the file list3 has two names... (1 Reply)
Discussion started by: newbie2010
1 Replies

5. Shell Programming and Scripting

Shell script using expect to login to couple of remote servers and read "crontab -l"

I need a shell script using expect to login to couple of remote servers and read "crontab -l -u <username>" & "cat /etc/rc.local" & "df -h" and able to create output into a file saved locally with hostname.crontab & hostname.rc.local & disk.status. I can supply a file as list of hostname or IP... (4 Replies)
Discussion started by: jaipsharma
4 Replies

6. Solaris

Remote execution of a local script on multiple servers

So I have a scriptlet called solaris_command: for i in \ server1 server2 server3 do echo $i ssh $i $1 echo "" done I then use that as a command in multiple scripts to allow for data gathering for all virtual hosts in the environment thusly: solaris_command "cat... (3 Replies)
Discussion started by: os2mac
3 Replies

7. Shell Programming and Scripting

C shell scripting, check if link exists on remote servers

Hi, I'm new to C Shell programming. I'm trying to check if a sym link exists on remote server if not send email. I'm not having much luck. Can anyone help? Here is what I have written but it doesn't work. It tells me that my variable was not defined. Here is part of the script, the second... (0 Replies)
Discussion started by: CDi
0 Replies

8. Shell Programming and Scripting

Prevent wrong user from using shell script for multiple remote servers

Hi, I am running a shell script from a central server to multiple remote servers using the following code: application_check() { # Linux/UNIX box with ssh key based login SERVERS=`cat /tmp/server-details` # SSH User name USR="user" # create new file > /tmp/abc.log # connect... (2 Replies)
Discussion started by: mystition
2 Replies

9. UNIX for Dummies Questions & Answers

Running the same remote script on multiple servers

Experts, Im trying to remote into a server, run a script that resides on that server and capture the information displayed & store in a local file. I struggled with this yesterday & finally that script is working now. Now, here is a scope creep and the script that I wrote for 1 remote... (2 Replies)
Discussion started by: OMLEELA
2 Replies

10. Shell Programming and Scripting

unix shell script which inserts some records into a file located in remote servers...

All, I need to write an unix shell script which inserts some records into a file located in remote servers. * Get the input from the user and insert according the first row. It should be in ascending order. 123451,XA,ABA 123452,XB,ABB 123453,XC,ABC 123455,XE,ABE 123456,XF,ABF 123458,XG,ABG... (2 Replies)
Discussion started by: techychap
2 Replies
Login or Register to Ask a Question