Sponsored Content
Top Forums Shell Programming and Scripting Ssh multiple hops to execute commands with arguments Post 302922929 by chiensh on Wednesday 29th of October 2014 08:04:52 AM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 09:00 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy