1

I created a Bash Script that is the following:

File=/library/logs/file.txt
ssh -n username@<ip> "$(< testscript.sh)" > $File 

It will remote into a machine and run a script and place the output in file.txt What I need to do now is instead of manually entering an ip address, I need to have it read from a list of hostnames coming from a .txt file and have it place it in a variable that will substitute the ip address. An example would be: ssh username@Variable in which "Variable" will be changing each time a word is read from a file containing hostnames. Any ideas how to go about this?

1 Answer 1

2

Use xargs with -I

cat hosts.txt | xargs -I %REPL ssh user@%REPL

-I defines a "replacement-string", in this case "%REPL" (but you can set it to what you like). Then that specific string will be will be replaced in the command, with each line from input.

Another way i like, is using printf

echo -e "host1.com\nhost2.com" | xargs -n1 printf "ssh user@%s -p 999\n"

This is especially great, if you wanted to use more lines in the same command.. Imagine that you have a file with username on line 1 and hostname on line 2. You could simply do:

xargs -n2 printf "ssh %s@%s -p 999\n" < user_and_host.txt

Passwords

All this, assumes you are using SSH keys because, by design, the openssh client will not let you "script" passwords.

If you want to script using password authentication, there are tons of hints and methods (someone will yell "expect" any second now). But i did them all at some point or another, and they bring eternal pain and indigestion.

Its not worth it, scripting this yourself. Have a look at my favorite tool for this:

This will let you pretty much do what you need. Give it username, password, one or more commands to run + a bunch of hosts/ip's. When its done, you'll have a nice CSV-file with the result of every command, ordered by its host.

Sign up to request clarification or add additional context in comments.

6 Comments

Or simply xargs -I %REPL ssh user@%REPL < hosts.txt
That worked perfectly thank you, now how would I go about having the password automatically entered for me instead of having to enter a password to ssh in every time? Is there a way for when it prompts "Password:" to have my password automatically entered?
Also is there a way to have it skip a hostname if it isn't connected to the network? It looks like it give me this error "port 22: Operation timed out" and then cancels running all other hostnames
I suspect that there is a problem, if there are multiple hosts in the hosts.txt file. lines from 2nd host onwards will be provided as input to ssh command. I would suggest for host in $(< host.txt); do ssh@$host ... ; done OR create a separate file descriptor for host.txt file & read from that.
@Technic1an Ahh.. The "scripted ssh-password" question. This is a very old one indeed and im sure many duplicates can be found here on stack-exchange. Normally you could use "expect", but to be honest its really not worth the trouble to code yourself. I'll edit my answer a bit, with a recommendation.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.