Bash script to read line by line and execute commands
Hello,
I'm trying to figure out how to piece together a script that will do the following: - Read a .txt file line by line. Each line will be the name of a server. - SSH into each of these servers (keys are setup so no need for username/password). Just "ssh <servername>" will work. - Execute a command on each server. - Return the results in a coherent manner and place them in a text file. If possible, splitting the results up into separate text files based on $servername would be preferable. I'm not much of a scripter, and I don't know enough to put this together properly. Any guidance would be highly appreciated. Thanks for your time. |
I am assuming the command is 'ls -rtl'. Substitute my assumption for your command.
Try this: Code:
while read servername; do |
I personally would use
Code:
#!/bin/bash Code:
./script ls -laF < servers.txt The first test makes sure the server name does not contain hashes (#), slashes (/), or semicolons (;). Hash and semicolon are often used to indicate a comment line. Slash would make it impossible to create a file based on the server name. None of the three should occur in a server name or address anyway. The second test makes sure the server name is not empty. Empty lines are common at the end of files, so handling them gracefully is a good idea. The ampersand (&) backgrounds each ssh command, and the final wait waits until all ssh commands have completed. Since the ssh commands are done in parallel, I redirect the input to the ssh commands from /dev/null -- no user input is possible. It would not make sense to allow it anyway, because you could not tell which ssh command you were supplying input to. |
Quote:
That worked perfectly, though. Again, thanks a lot for your help! |
please click "mark solved".
|
All times are GMT -5. The time now is 12:34 PM. |