Dummy script assistance


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dummy script assistance
# 1  
Old 06-14-2011
Dummy script assistance

Hi,
I am new in ksh scripting and if anyone can help that would be great.

I'm writing a script which will SSH to several machines and then would append a certain file from a NAS to the /etc/sudoers file
the problem i am having is after the script connects to a certain server the commands are not done on the remote machine but on the machine i run the script from.

the script is:
Code:
#!/bin/ksh
SERVERS=`cat servers.log`
for i in $SERVERS
do
  ssh -n -T $i
  echo "i'm on $i"
  cp /etc/sudoers /etc/sudoers.bkp
  cat /system_kits/tmp/gal/lsof >> /etc/sudoers.gal
  echo "==========Giving lsof sudo permission on  $i ===================="
done


what am I doing wrong? how make so that the commands will run on the remote machine?


Regards,
Gal Uzan

Last edited by Franklin52; 06-14-2011 at 03:49 PM.. Reason: Please indent your code and use code tags
# 2  
Old 06-14-2011
ssh doesn't magically take the lines below it because it's not part of your shell. If you ran 'cat' you wouldn't expect it to eat the lines below it, you have to redirect whatever you want into it. You have to treat ssh like any other program.

Also, you have a useless use of cat and useless use of backticks in there. It's a bad habit since it's dangerous -- there's a limit to the size of a shell variable which can be surprisingly small on some shells and systems. Use 'while read' instead, it's also more efficient.

Code:
while read LINE
do
        ssh -T username@"$LINE" <<"EOF"
                echo "I'm on $HOSTNAME"
                cp /etc/sudoers /etc/sudoers.bkp
                cat /system_kits/tmp/gal/lsof >> /etc/sudoers.gal
                echo "==========Giving lsof sudo permission on  $HOSTNAME ===================="
EOF
done < servers.log

The last EOF needs to be on the beginning of the line. HOSTNAME is a special variable which the host ought to set for you.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 06-14-2011
excuse me if my questions are novice

when using
Code:
while read line

the loop will read each line seperatley and insert the line into the $LINE variable? And this will read the list from servers.log specified at the end of the loop?

Also to confirm I understood from google -
Code:
<<"EOF"

means that the loop will run on the specific line untill it reads EOF as input...correct?
# 4  
Old 06-14-2011
Quote:
Originally Posted by galuzan
excuse me if my questions are novice

when using
Code:
while read line

the loop will read each line seperatley and insert the line into the $LINE variable?
Yep.

Quote:
And this will read the list from servers.log specified at the end of the loop?
Yep.
Quote:
Also to confirm I understood from google -
Code:
<<"EOF"

means that the loop will run on the specific line untill it reads EOF as input...correct?
Nope. That's a here-document. Try typing this in your terminal:

Code:
$ cat <<"EOF"
This
is
some
text
that
I
am
typing
EOF

This
is
some
text
that
I
am
typing
$

It feeds the text inside <<"EOF" ... EOF into the standard input of the program. That's one way to feed the lines into ssh. Another would be echo or printf, and so forth.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 06-14-2011
So 'EOF' can be anything from 'bla' to 'gal' ?
# 6  
Old 06-14-2011
Yes, EOF can be anything.

Also note that the quotes are important.

Code:
VAR=5

$ cat <<OMG
VAR=${VAR}
OMG

VAR=5

$ cat <<"WTF"
VAR=${VAR}
WTF

VAR=${VAR}

$

Putting the first one in quotes causes it not to substitute variables inside.
# 7  
Old 06-15-2011
So the quotes keep the variables to be the same as I defined on my terminal before I ran the script?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need UNIX script to check checksum and dummy file creation.

Hi Folks, I need a UNIX script which will copy files(Table wise) from source directory to destination directory (Under table directory) and also creates 2 additional files after getting copied to destination directory with extension .pdy and .ldy , . pdy file will be zero byte file should get... (4 Replies)
Discussion started by: Nicks1412
4 Replies

2. Shell Programming and Scripting

Need assistance in ksh script

requirement : I need to read a text file and find out which particular line has highest charcters on it using the shell script. I tried & was able to find out only for one line. I could not able to find out for the entire the line. sed -n '10 p' ctstest.sh | wc -w Please guide me... (5 Replies)
Discussion started by: ramkumar15
5 Replies

3. Red Hat

Dummy Question, put a script on startup fails

Hi all, im pretty new to unit/redhat 6.2 I am trying to load a script upon startup of my redhat 6.2 (instance in ec2 AWS if someone cares) I want everytime when instance starts (after stop/reboot) a script i build to launch. when i run the script ./scriptname the script runs and everything is... (4 Replies)
Discussion started by: hookedatwalla
4 Replies

4. Shell Programming and Scripting

Need assistance with a simple script

I have a simple script. Do you know what I got this error? ./total_memory.ksh: line 5: ' Thanks #! /bin/bash setmem=30177660 totalMemory= grep MemTotal /proc/meminfo | awk '{print $2}' if ; then echo "Total memory $totalMemory is less than :$setmem" exit 1 ... (3 Replies)
Discussion started by: Beginer0705
3 Replies

5. Shell Programming and Scripting

script assistance with shift J

Hey all, I need some assistance. I'm writing a script to eject tapes from a tape library, but the library is not a queued system and can only eject 15 tapes at a time. I added paste -d : -s so that it goes through full_tapes and puts each media_id on one line separated by the :. Now I'm... (2 Replies)
Discussion started by: em23
2 Replies

6. Shell Programming and Scripting

Shell Script Assistance

I am looking for a shell script or command to automate a process of opening many files in a directory and changing a string of text. Example: I have a apache web server that uses virtual hosting. There are approximately 2300 vhost entries or files. So in the directory... (2 Replies)
Discussion started by: jaysunn
2 Replies

7. Shell Programming and Scripting

shell script assistance please

When I run this command (showstatus <username> <dbname>) in the prompt, the following will be displayed in the screen: 1. Show processes 2. Start process 3. Stop process 4. Go back to prompt Once i choose/type Option "1" (which is Show processes), it will display the list of processes... (5 Replies)
Discussion started by: xinoo
5 Replies

8. Shell Programming and Scripting

need assistance ----SH schell script

Hello All, I need to develop a script(SH]) to generate a comparison file between two files old and new file.The script takes in parameter the old file path and the new file path. And the script generates a file containing the comparison between the two files with this details: - Keys... (2 Replies)
Discussion started by: shahidbakshi
2 Replies

9. Shell Programming and Scripting

Need a little assistance with a shell script

I need to modify a script to send an attatched file. I have researched and read the faq's but have not found a solution for my script. Here is a copy of the code I am using: #!/bin/sh mysqldump --opt --skip-add-locks --user=****** --password=******* databasename | gzip >... (3 Replies)
Discussion started by: rickou812
3 Replies

10. Shell Programming and Scripting

KSH Script Assistance

Hey everyone, I'm newer than new when it comes to this ksh and scripting stuff, and unix in general. I have been thrown into a task at work that A: They expect me to come up to speed on, B: Show I can do this. (Program for the workgroup) Here's the script, part of it, from the overall... (3 Replies)
Discussion started by: Brusimm
3 Replies
Login or Register to Ask a Question