LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 07-24-2012, 04:42 AM   #1
ramprak
LQ Newbie
 
Registered: Jul 2012
Posts: 5

Rep: Reputation: Disabled
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.
 
Old 07-24-2012, 05:21 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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?
 
Old 07-24-2012, 05:48 AM   #3
ramprak
LQ Newbie
 
Registered: Jul 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
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..
 
Old 07-24-2012, 06:03 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
1 members found this post helpful.
Old 07-24-2012, 06:41 AM   #5
ramprak
LQ Newbie
 
Registered: Jul 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-24-2012, 03:45 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
 
1 members found this post helpful.
Old 07-25-2012, 08:53 AM   #7
ramprak
LQ Newbie
 
Registered: Jul 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-25-2012, 07:26 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
Old 07-26-2012, 02:27 AM   #9
ramprak
LQ Newbie
 
Registered: Jul 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-26-2012, 08:26 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Code:
ssh ip-addr "./remote.sh"
http://linux.die.net/man/1/ssh
You may need the -t option
 
1 members found this post helpful.
  


Reply



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
Cannot access other linux machines from mine!!!!! rajnair0278 Linux - Networking 1 03-21-2006 01:54 AM
unable to lookup hostnames decaf Linux - Networking 4 03-30-2005 04:30 PM
Apache DNS hostnames lookup; SuSE 9.2 Carlee SUSE / openSUSE 0 01-29-2005 03:04 PM
access linux machines hariiyer Linux - Networking 2 10-27-2004 12:14 PM
remote file access (non-networked machines) mikescar Linux - Newbie 3 02-03-2004 06:01 PM

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

All times are GMT -5. The time now is 12:50 AM.

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
Open Source Consulting | Domain Registration