0

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.

1
  • haha okay I did not think of that. Commented Apr 30, 2020 at 23:04

1 Answer 1

2

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.

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

5 Comments

re. $( ) being preferred over backticks, in writing scripts I definitely prefer and recommend $( ), but for interactive use where the command substitution is typically simple, I must admit I favour the backticks for being quicker to type.
Fails if there are spaces tabs in the file, in this case there are none
@Jetchisel Yes, any whitespace is treated as a separator, but since the question was about hostnames/addresses, one might even consider this a feature.
@Shawn : You can not nest backticks.

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.