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)
(Test 2)
(Test 3)
(Test 4)
(Test 5)
(Test 6)
(Test 6)
(Test 7)
(Test 8)
(Test 9)
(Test 10)
(Test 11)
(Test 11)
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..
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.
Last edited by Corona688; 10-29-2014 at 03:18 PM..
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)