SSH in SSH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SSH in SSH
# 1  
Old 02-14-2014
SSH in SSH

Hello,


I'm trying to make a ssh in another ssh like below:

Code:
ssh user1@ip1 'ssh user2@ip2 'cd /path; ls -lrt | grep CcnMeasureJob | tail -1 | cut -d " " -f 10 | xargs -I{} scp {} user3@ip3:/path3 ''

The problem is that everything dies after the second ssh ("cd /path is not regognized").
I mention that doing like this is the only way I can copy the desired file in the desired location, but it seems there is sort of syntax error.


Please help me about this.
Thank you

Last edited by Scrutinizer; 02-15-2014 at 03:48 AM.. Reason: code tags also for data
# 2  
Old 02-14-2014
Try adding the -t switch to the first ssh command. Also, make sure the second server is accessible from the first one.

Code:
ssh -t user@server1 ssh user@server2 'commands-to-run'

# 3  
Old 02-15-2014
I tried with ssh -t command, the output is like below:

Code:
ssh -t user@server1 ssh user@server2 'cd /path_server2/; pwd'

Code:
/home_server1
Connection to server1 closed.

It still doesn't want to change the execute commands in server2, although this is accesible from server1
# 4  
Old 02-15-2014
To do they type of tunneling I typically use nc (or netcat) on server1 and a ProxyAlias in your .ssh/config like this:

Code:
host server2
ProxyCommand ssh server1 nc %h %p

They you can go direct to server2 eg:

Code:
$ ssh user@server2 'cd /path_server2/; pwd'

This ProxyCommand config be further simplified and the need for nc to be installed on server1 avoided, if you have OpenSSH version 5.3 or later you can use:

Code:
ProxyCommand ssh server1 -W %h:%p

# 5  
Old 02-15-2014
I managed to do it finaly like this:

Code:
ssh user1@host1 'ssh user2@host2 ' ./launch_script.sh ''

,where in launch_script.sh I put the desired code.

Like this it wanted to run correctly.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find active SSH servers w/ ssh keys on LAN

Hi, I am trying to complete my bash script in order to find which SSH servers on LAN are still active with the ssh keys, but i am frozen at this step: #!/bin/bash # LAN SSH KEYS DISCOVERY SCRIPT </etc/passwd \ grep /bin/bash | cut -d: -f6 | sudo xargs -i -- sh -c ' && cat... (11 Replies)
Discussion started by: syrius
11 Replies

2. UNIX for Beginners Questions & Answers

Ssh script to validate ssh connection to multiple serves with status

Hi, I want to validate ssh connection one after one for multiple servers..... password less keys already setup but now i want to validate if ssh is working fine or not... I have .sh script like below and i have servers.txt contains all the list of servers #/bin/bash for host in $(cat... (3 Replies)
Discussion started by: sreeram4
3 Replies

3. Shell Programming and Scripting

Check if file exists via ssh in ssh (nested)

I'm using redhat and have an odd issue with a nested ssh call. ssh -i ~/.ssh/transfer-key -q transfer@fserver1 ] && ssh -i ~/.ssh/transfer-key transfer@fserver1 "ssh -i ~/.ssh/sftp-key sftpin@10.0.0.1 ]" && ssh -i ~/.ssh/transfer-key transfer@fserver1 "scp -i ~/.ssh/sftp-key /home/S/outbox/*... (2 Replies)
Discussion started by: say170
2 Replies

4. UNIX for Dummies Questions & Answers

SSH tunnel working for ssh but not for sshfs

I'm trying to setup a link between my home pc (work-machine) and a server at work (tar-machine) that is behind a gateway (hop-machine) and not directly accessible. my actions: work-machine$ ssh -L 1234:tar-machine:22 hop-machine work-machine$ ssh -p 1234 user@127.0.0.1 - shh access on... (1 Reply)
Discussion started by: Vathau
1 Replies

5. Shell Programming and Scripting

Ssh = ssh expect and keep everything not change include parameter postion

I have write a script which contains ssh -p 12345 dcplatform@10.125.42.50 ssh 127.0.0.1 -p 5555 "$CMD" ssh root@$GUEST_IP "$CMD" before I use public key, it works well, now I want to change to "expect", BUT I don't want to change above code and "parameter position" I can post a... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

6. UNIX for Dummies Questions & Answers

SSH SSH encountered 1 errors during the transfer

This issue was resolved due to using the correct user transferring the file over to the desktop. (1 Reply)
Discussion started by: Computergal2104
1 Replies

7. Shell Programming and Scripting

Using ssh to add register key on ssh server

Hi, I want to use ssh to add a register key on remote ssh server. Since there are space characters in my register key string, it always failed. If there is no space characters in the string, it worked fine. The following is what I have tried. It seems that "ssh" command doesn't care about double... (9 Replies)
Discussion started by: leaftree
9 Replies

8. Shell Programming and Scripting

could not send commands SSH session with Net::SSH::Expect

I am using Net::SSH::Expect to connect to the device(iLO) with SSH. After the $ssh->login() I'm able to view the prompt, but not able to send any coommands. With the putty I can connect to the device and execute the commands without any issues. Here is the sample script my $ssh =... (0 Replies)
Discussion started by: hansini
0 Replies

9. Cybersecurity

What's the difference between an SSH Client and an SSH Server?

Eh... yeah. What the title says. :D (1 Reply)
Discussion started by: PSC
1 Replies
Login or Register to Ask a Question