2

I have a script that shows all the instances on AWS that I'm running:

$/home/scripts/show_instances.sh
i-f66d2de0  running 10.148.17.40
i-fxxdbbe0  running 10.148.20.60
(...)

What I'd like to do is to take the first IP, pipe to ssh and connect to the server automatically. I tried this:

$/home/scripts/show_instances.sh | tail -1 | awk '{print $3}' | xargs ssh

But is not working: it connects to the server but after the first command that I type (i.e. ls), the terminal just seems to be waiting for something and nothing happens (I'm on mac connecting to Unix), so I have to exit the instance with ctrl+c. Some suggestions?

5
  • Not sure that xargs is buying you anything in this case. If your headline is really your Q, then ssh $(..../show_instances.sh | awk 'NR==1{print $3}') ..other ssh stuff ... (using cmd-substitution $(...)) should solve your problem. (don't need tail). Otherwise it seems that your Q may be about "how do I use xargs). Good luck. Commented Oct 3, 2016 at 13:37
  • Generally you can use backticks (or $( ) ) for obtaining this (I can't show you how backticks work because they are used for formatting purposes on StackOverflow website). Commented Oct 3, 2016 at 13:42
  • 1
    Wow! Genius! It's working now! I did like this: ssh /home/scripts/show_instances.sh | tail -1 | awk '{print $3}' Thank you! Commented Oct 3, 2016 at 13:56
  • Explaining better, both ways work, like you said. ssh (backticks)/home/scripts/show_instances.sh | tail -1 | awk '{print $3}'(backticks) or using $(..) ssh "$(/home/scripts/show_instances.sh | tail -1 | awk '{print $3}')" Thank you! Commented Oct 3, 2016 at 14:24
  • 1
    Join the early 90's and use $(... cmds sub ) ;-) .. Backticks are your backup, if you get stuck using an ancient system that doesn't support $(..). Also, read about the awk internal variable NR, you don't need tail for this case. Good luck. Commented Oct 3, 2016 at 15:19

1 Answer 1

4

Use a command substitution, and run one go of awk in that to get the desired IP from first line, and then do ssh:

ssh "$(/home/scripts/show_instances.sh | awk 'NR==1{print $NF; exit}')"

If you want to stick to the piping, and xargs one, then allocating pseudo TTY (-t) should let you in and run interactively:

/home/scripts/show_instances.sh | awk 'NR==1{print $NF; exit}' | xargs ssh -tt

For last line, do:

ssh "$(/home/scripts/show_instances.sh | awk 'END{print $NF}')"
/home/scripts/show_instances.sh | awk 'END{print $NF}' | xargs ssh -tt
Sign up to request clarification or add additional context in comments.

4 Comments

Worked! I did like this: ssh "$(/home/scripts/show_instances.sh | tail -1 | awk '{print $3}')" Thank you!
@ArthurAccioly Check my edits. You don't need tail, you can do this with awk alone.
Wow! Even better! Like this right? ssh "$(/home/scripts/show_instances.sh | awk 'END{print $NF}')" I'm not very good on awk. Why NF gets exactly the IP? btw, the second option with ssh -tt didn't work and I had the same issue of terminal waiting for something after any command, like "ls". But, anyway, I'm very happy with the command.
@ArthurAccioly NF gets us the number of fields, and $NF gets the last field's value expectedly.

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.