linux - Bash Script read hostnames from .txt file and place in variable for ssh -


i created bash script following:

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

it remote machine , run script , place output in file.txt need instead of manually entering ip address, need have read list of hostnames coming .txt file , have place in variable substitute ip address. example be: ssh username@variable in "variable" changing each time word read file containing hostnames. ideas how go this?

use xargs -i

cat hosts.txt | xargs -i %repl ssh user@%repl 

-i defines "replacement-string", in case "%repl" (but can set like). specific string will replaced in command, each line input.

another way like, using printf

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

this great, if wanted use more lines in same command.. imagine have file username on line 1 , hostname on line 2. do:

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

passwords

all this, assumes using ssh keys because, by design, openssh client not let "script" passwords.

if want script using password authentication, there tons of hints , methods (someone yell "expect" second now). did them @ point or another, , bring eternal pain , indigestion.

its not worth it, scripting yourself. have @ favorite tool this:

this let pretty need. give username, password, 1 or more commands run + bunch of hosts/ip's. when done, you'll have nice csv-file result of every command, ordered host.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -