Ssh multiple hops to execute commands with arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ssh multiple hops to execute commands with arguments
# 1  
Old 10-29-2014
Ssh multiple hops to execute commands with arguments

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)
Code:
HostA> ssh -t HostB ssh -t HostC  ssh  HostD  hostname
HostD
Connection to  HostC  closed.
Connection to  HostB  closed.

(Test 2)
Code:
HostA> ssh -t HostB ssh -t HostC  ssh  HostD  hostname;hostname
HostD
Connection to  HostC  closed.
Connection to  HostB  closed.
HostC

(Test 3)
Code:
HostA> ssh -t HostB ssh -t HostC  ssh  HostD  "hostname;hostname"
HostD
Connection to  HostC  closed.
HostB
Connection to  HostB  closed.


(Test 4)
Code:
HostA> ssh -t HostB ssh -t HostC  "ssh HostD   hostname;hostname"
HostD
Connection to  HostC  closed.
Connection to  HostB  closed.
HostB

(Test 5)
Code:
HostA> ssh -t HostB "ssh -t HostC  ssh HostD   hostname;hostname"
HostD
Connection to  HostC  closed.
Connection to  HostB  closed.
HostB

(Test 6)
Code:
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)
Code:
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)
Code:
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)
Code:
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)
Code:
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)
Code:
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)
Code:
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)
Code:
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

Last edited by Corona688; 10-29-2014 at 02:58 PM..
# 2  
Old 10-29-2014
First off -- what exactly is your goal? As far as I can tell, test 1 did exactly what you wanted.

It's not a question of ssh, exactly -- the problem is that you're going through multiple layers of shell. To prevent things from splitting locally, you quote them.

One command deeper, you have to put quotes in quotes -- either single-quotes, or escaped double-quotes -- so the quotes don't "disappear" when they're processed by the local shell.

One command deeper, you have to start escaping them, so they don't disappear here, get processed on the second server, so the third server gets a command its happy with.

Four deep... It starts getting impractical, you end up escaping escaped things, doubling and quadrupling and octupling the number of backslashes to get enough for the last layer to consider it "one" backslash.

I would avoid that completely, and feed text into standard input instead. That way it does not fly through 5 levels of parsing and quote removal.

Code:
ssh server1 ssh server2 ssh server3 ssh server4 ssh server5 exec /bin/sh -s <<EOF
hostname
echo ${LOCALVARIABLE}
echo \${REMOTEVARIABLE}
EOF


Last edited by Corona688; 10-29-2014 at 03:18 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-29-2014
You might want to go to the server where you want the command to run and write a shell script that defines all of the variables that it needs and doesn't rely on a .bashrc or any other login script, ideally with no input, and get that to work. Then exit out of that server and in the next to last server before logging into the final server write a shell script that calls the script you just wrote and passes any variables that it might need and test it, then repeat that process with each previous server.

The benefit will be that you are testing each level as you go, and not trying to figure out why something doesn't work when its 5-10 levels deep. Hopefully I am not helping you to hack someone else's server. Smilie
# 4  
Old 10-29-2014
Have you considered using the ssh ProxyCommand feature to handle this ssh tunneling automatically

There are heaps of documents on the web about doing this sort of thing for example:

Transparent Multi-hop SSH

Note: newer version of ssh (version 2 and later) don't even need the netcat (nc) command and will work with the ssh -w host:port option.
These 2 Users Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script help to execute ssh remote commands

Hi, The below command is not giving me the count , Can somebody help me in re-writing this pls . Proc_Exist=`ssh -q -o "BatchMode=yes" -o "PasswordAuthentication=no" $OAUSER@${Primary_Node} ps -ef | grep -v grep | grep "${ICM_Proc}" |wc -l ` Also the same problem with below... (13 Replies)
Discussion started by: Y.balakrishna
13 Replies

2. Shell Programming and Scripting

Using sed to execute multiple commands

Let's say I have a file called test.out. In this file I want to do the following: 1. Search for DIP-10219 and with this: 2. Remove everything in front of cn= 3. Remove everything after *com 4. Remove duplicate lines 5. Replace ( with \( 6. Replace ) with \) For 1-3 I have figured out this... (11 Replies)
Discussion started by: exm
11 Replies

3. IP Networking

http over ssh tunnel using multiple hops

Hello, I got an application on a linux server that I would like to access using https and a URL. I would like to create a ssh tunnel. But, let's say the app is on box C, but box C can only be accessed through box B, that can be accessed only through box A. I would like to create the ssh tunnel... (0 Replies)
Discussion started by: Pouchie1
0 Replies

4. Shell Programming and Scripting

connect to multiple servers using SSH and execute commands

Requirement: Run a shell script with below inputs file name checksum path the script should go to multiple servers (around 35) and verify the input cksum and if there is a mismatch display a simple message to the user that cksum verification failed. host details, user id /... (1 Reply)
Discussion started by: amicableperson
1 Replies

5. UNIX for Dummies Questions & Answers

Passing arguments to alias with multiple commands

I have a few aliases set up on AIX servers in my .kshrc file. Some of them contain multiple commands that are piped together. A simple example would be something like this: # alias to list directory contents as root and sort by size. alias lss='sudo ls -l | sort -nbk5' When I call... (5 Replies)
Discussion started by: derndingle
5 Replies

6. Solaris

Help with executing multiple remote commands after multiple hops

Hi SSHers, I have embedded this below code in my shell script.. /usr/bin/ssh -t $USER@$SERVER1 /usr/bin/ssh $USER2@S$SERVER2 echo uptime:`/opt/OV/bin/snmpget -r 0 -t 60 $nodeName system.3.0 | cut -d: -f3-5` SSH to both these servers are public-key authenticated, so things run... (13 Replies)
Discussion started by: LinuxUser2008
13 Replies

7. Shell Programming and Scripting

Execute ssh commands through bash script

Hi all! I am trying to write a script that will check if a certain directory is available at several different nodes and then do stuff in it ..... On the beginning of the script I give as a variable the directory and the number of the nodes and then I loop like this: for... (3 Replies)
Discussion started by: idet2
3 Replies

8. Shell Programming and Scripting

ssh - to execute set of commands

Hi Can someone help me to figure out Want to execute few cmds in remote host thru ssh Tried below cmd -------------------------------excerpt------------------- RDIR=/data1/logs ---> variable stores rem. server directory TODAY="`date '+%b %d'`" ssh -i $userid@$host "cd... (2 Replies)
Discussion started by: id100
2 Replies

9. UNIX for Dummies Questions & Answers

cron used to execute multiple commands

have to run multiple commands at a specified time by the user... (3 Replies)
Discussion started by: hemaa
3 Replies

10. Shell Programming and Scripting

Execute multiple commands in a find

I am checking that a file is older than a reference file that I build with a touch command before processing it. If it is not old enough, I want to sleep for an hour and check again. My problem is if it is old enough to process, I want to exit when I am done, but I cannot find a way to exit... (2 Replies)
Discussion started by: prismtx
2 Replies
Login or Register to Ask a Question