LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-01-2011, 12:44 PM   #1
dnoy
Member
 
Registered: Nov 2007
Posts: 69

Rep: Reputation: 15
script help


All,

can someone please help me with something very basic. I have one script that goes through some IPs:


###############################3
#/usr/bin/sh
# This is a script to copy files from one host to a group of h osts


# $3 = third paramter (file that contains list of hosts)


HOSTFILE="list.txt"

#Im trying to do a loop that will go through every ip in the list.txt file
for host in $HOSTFILE
do
#need to figure out how to pass the variable to the expect script
/scripts/expect.sh
done
################################################

#this is a seperate script called expect.sh
#!/usr/bin/expect
#need to get variable here
spawn ssh test@$IP
expect "Password:"
send "test\r"
expect "#"
send "mkdir /test\r"
expect "#"


##################################################

thank you in advance
 
Old 04-01-2011, 12:50 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,552

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
So what is happening for you so far?

I see you have a variable called host (which may not be a great idea as there is an executable by the same name on a number of machines) which you never use anywhere?

Also I notice the variable $IP in expect script, where is this being set?

Answer some of these and it may help you when reading through the literature you are reading to help you.
 
Old 04-01-2011, 01:14 PM   #3
dnoy
Member
 
Registered: Nov 2007
Posts: 69

Original Poster
Rep: Reputation: 15
Ok so lists.txt has:
192.168.0.1
192.168.0.2
192.168.0.3

What im trying to do is bring this into the original script in one by one so I can pass them to the expect script. I saw a while loop that sais while read servers. Im not sure what variable I put after expect.sh.
It doesn’t seem that “/scripts/expect.sh $HOSTFILE” would work. One that is in do I modify my spawn ssh in expect to look like “spawn ssh test@$1”
?
 
Old 04-02-2011, 03:40 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,552

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
So you saw a while loop but changed to a for loop?? The can both read from a file but do it quite differently and have different affects.

This will demonstrate both using your list.txt:
Code:
#!/bin/bash

FILE=list.txt

while read -r LINE
do
    echo $LINE
done<$FILE

for LINE in $(< $FILE)
do
    echo $LINE
done
Now this will simply print the 3 IPs twice through. The catch with the for loop would be if you changed the file like so:
Code:
192.168 0.1
192.168 0.2
192.168 0.3
Due to the space, the for loop will now impose word splitting. So running the same script above you will get:
Code:
192.168 0.1
192.168 0.2
192.168 0.3
192.168
0.1
192.168
0.2
192.168
0.3
Here you see the while loop has kept the lines as they were in the file but the for loop has processed the line based on each space and each new line.

So assuming you know that you file has no white space anywhere to cause the issues above, your shell script can look like:
Code:
#/usr/bin/sh
# This is a script to copy files from one host to a group of h osts


# $3 = third paramter (file that contains list of hosts)


HOSTFILE="list.txt"

#Im trying to do a loop that will go through every ip in the list.txt file
for host in $(< $HOSTFILE)
do
#need to figure out how to pass the variable to the expect script
    /scripts/expect.sh $host
done
Are you sure that sh is under /usr/bin and not just /bin? Also, is there a reason for using sh as opposed to bash?

Quote:
One that is in do I modify my spawn ssh in expect to look like “spawn ssh test@$1”
I do not know a lot about expect scripts, but assuming it processes parameters the same as sh / bash, then yes that should work.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 04:05 AM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 09:40 AM
ssh - using variables in call to start remote script from local script babag Linux - Networking 2 06-03-2008 05:50 PM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 12:33 PM
Shell Script: want to insert values in database when update script runs ring Programming 2 10-25-2007 11:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration