Linux script to access machines using hostnames / IP specified in a lookup file
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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..
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.
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
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.