anarchy
anarchy

Reputation: 5174

Automate a shell script using variables from a txt file

I have a list of servers in a addresses.txt file in the following format

xx.xxx.xx.xx
xx.xxx.xxx.xx
xxx.xx.xx.xx

The ip addresses change everyday and I get a new .txt file. The number of servers might change everyday. Im using a Mac so Im using csshx.

I'm trying to csshx into the servers so I need to create a command like this everyday.

csshx -login username --ssh_args "-i ~/.ssh/sshkey" xx.xxx.xx.xx xx.xxx.xxx.xx xxx.xx.xx.xx.

Is there a way to create a shell script so that I can just run the cssh -login username --ssh_args "-i ~/.ssh/sshkey" part and automate the rest of the line using variables from the addresses.txt file? I was thinking of using a for loop but the command needs to be in 1 line.

Upvotes: 0

Views: 118

Answers (1)

Arkku
Arkku

Reputation: 42109

The traditional way would be with backtick or $() command substitution and cat to output the file:

csshx -login username --ssh_args "-i ~/.ssh/sshkey" `cat addresses.txt`

More modern shells allow you to replace cat with <:

csshx -login username --ssh_args "-i ~/.ssh/sshkey" $(< addresses.txt)

Any whitespace in the file is treated as an argument separator, so the server addresses may be separated either by newlines or by spaces/tabs.

Upvotes: 2

Related Questions