Sponsored Content
Top Forums Shell Programming and Scripting Shell script for remote servers Post 302977514 by rbatte1 on Tuesday 19th of July 2016 09:10:31 AM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 12:14 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy