LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Linux script to access machines using hostnames / IP specified in a lookup file (https://www.linuxquestions.org/questions/linux-newbie-8/linux-script-to-access-machines-using-hostnames-ip-specified-in-a-lookup-file-4175418367/)

ramprak 07-24-2012 04:42 AM

Linux script to access machines using hostnames / IP specified in a lookup file
 
I need a shell script that can access remote machines using the hostname/IP specified in the lookup file and run commands on it. I'm new to Linux scripting. Any help is appreciated. Thanks in advance.

pixellany 07-24-2012 05:21 AM

Homework?

Regardless, please give some more details---e.g. do you need a secure connection? What protocols are set up on the remote machines?--eg ftp, ssh, etc.

And, does each machine require a login and password?

ramprak 07-24-2012 05:48 AM

It is for a monitoring tool. Yes it should be a secure connection.
ssh was set up in that machine. Credentials are required. Like I said it the script should access machine from the lookup file.
Expecting your reply..

pixellany 07-24-2012 06:03 AM

getting the information from the lookup file is the easy part. Simply set up a loop which reads the file, process the entries (if needed), and athen pass the data to the other commands.

The hard part---at least for me---is passing commands to ssh which it will use after it has connected to the remote machine. I would start with a Google search using something like "ssh scripting" or "pass command in ssh script".

Also, build up the program one step at a time---eg start by just having it login to one machine.

For help on learning BASH, go here: http://tldp.org
Start with the BASH Guide for Beginners, and then the Advanced BASH Scripting Guide.

ramprak 07-24-2012 06:41 AM

I don't have a lot of time. But, thanks anyway. One more thing, will this script get the desired result, if I have commands to be executed on the remote machine in a separate script..

for ip_addr in $(cat hostname.txt); do
ssh ${ip_addr} "bash -s" < script_to_be_invoked
done

Thanks.

pixellany 07-24-2012 03:45 PM

Quote:

will this script get the desired result,
Have you tried it?
Quote:

I don't have a lot of time
You can go thru BASH Guide for Beginners in 1/2 day---including trying some examples.

I think your loop may work but I think it's more common to do:
while read var; do
<do stuff>
done <file

This reads one line at a time into "var".....reading from filename

ramprak 07-25-2012 08:53 AM

The for loop works,but I couldn't figure it out with while loop.
If you could provide a script with while loop using same parameters, it will be useful in future.
Thanks for your replies.

pixellany 07-25-2012 07:26 PM

Here's a typical use of the while loop: We're going to read from a file named "inputfile"---each line will go into a variable named "lin"---, and then echo each line back to standard output.

Code:

while read lin; do
    echo $lin
done  < inputfile

The loop keeps reading one line at a time until it encounters end of file--at this point, the "read" command returns "FALSE" and the loop is not executed.

ramprak 07-26-2012 02:27 AM

I can execute the while loop, but how can I pass the commands to ssh like in the for loop because it has to run a script stored in the remote machine. So it has to take input from two files. Anyways thanks for your help.

chrism01 07-26-2012 08:26 PM

Code:

ssh ip-addr "./remote.sh"
http://linux.die.net/man/1/ssh
You may need the -t option


All times are GMT -5. The time now is 09:04 AM.