Hi
I need to write a script to ssh through several hops (e.g. HostA-HostB-HostC-HostD), where Host A does not have direct assess to HostC ; HostB cannot access HostD directly.
when I ssh 3 hops and run command with arg1, arg2 and redirect the output to a file, e.g.
HostA> ssh -t HostB ssh -t HostC ssh HostD command arg1 arg2 > output.txt
but it doesn't work as expect it does not pass 2nd argument to command and the output file will be written in HostC, instead of in HostD as expected.
I did some tests to see if quotes or double quotes pairs helps, e.g
(Test 1)
HostA> ssh -t HostB ssh -t HostC ssh HostD hostname
HostD
Connection to HostC closed.
Connection to HostB closed.
(Test 2)
HostA> ssh -t HostB ssh -t HostC ssh HostD hostname;hostname
HostD
Connection to HostC closed.
Connection to HostB closed.
HostC
(Test 3)
HostA> ssh -t HostB ssh -t HostC ssh HostD "hostname;hostname"
HostD
Connection to HostC closed.
HostB
Connection to HostB closed.
(Test 4)
HostA> ssh -t HostB ssh -t HostC "ssh HostD hostname;hostname"
HostD
Connection to HostC closed.
Connection to HostB closed.
HostB
(Test 5)
HostA> ssh -t HostB "ssh -t HostC ssh HostD hostname;hostname"
HostD
Connection to HostC closed.
Connection to HostB closed.
HostB
(Test 6)
HostA> ssh -t HostB ssh -t HostC ssh HostD hostname > test.txt
Connection to HostB closed
(in HostA)
HostA> cat test.txt
HostD
Connection to HostD closed.
Connection to HostC closed.
(Test 6)
HostA> ssh -t HostB ssh -t HostC ssh HostD hostname > test.txt
Connection to HostB closed
(in HostA)
HostA> cat test.txt
HostD
Connection to HostC closed.
(Test 7)
HostA> ssh -t HostB ssh -t HostC ssh HostD "hostname > test.txt"
Connection to HostC closed
Connection to HostB closed
(in HostB)
HostB> cat test.txt
HostD
(Test 8)
HostA> ssh -t HostB ssh -t HostC "ssh HostD hostname > test.txt"
Connection to HostC closed
Connection to HostB closed
(in HostB)
HostB> cat test.txt
HostD
(Test 9)
HostA> ssh -t HostB "ssh -t HostC ssh HostD hostname > test.txt"
Connection to HostC closed
Connection to HostB closed
(in HostB)
HostB> cat test.txt
HostD
(Test 10)
HostA> ssh -t HostB "ssh -t HostC ssh HostD 'hostname > test.txt' "
Connection to HostC closed
Connection to HostB closed
(in HostC)
HostC> cat test.txt
HostD
(Test 11)
HostA> ssh -t HostB "ssh -t HostC 'ssh HostD hostname > test.txt' "
Connection to HostC closed
Connection to HostB closed
(in HostC)
HostC> cat test.txt
HostD
(Test 11)
HostA> ssh -t HostB "ssh -t HostC 'ssh HostD "hostname > test.txt" ' "
Connection to HostB closed
(in HostA)
HostA> cat test.txt
bash: -c: line 0: unexpected EOF while looking for matching `''
bash: -c: line 1: syntax error: unexpected end of file
After these tests, I still cannot find the logics of passing arguments correctly in ssh, can anyone help how to get it work correctly?
Thanks!
Rgds,
Dominic