Bash commands to an 'ssh' within an ssh'


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash commands to an 'ssh' within an ssh'
# 1  
Old 01-09-2010
Bash commands to an 'ssh' within an ssh'

I've struggled to find a solution to this problem from searching so I thought I'd write a post to see what can be done.

I'm attempting to connect and run commands on 'server2' but because of security limitations I cannot access it directly. I can however ssh into 'server1' and then into 'server2' to issue the commands manually.

ie.
ssh user@server1
ssh server2
qfree
qstat
etc..

This works perfectly fine however I'm trying to batch hundreds to thousands of jobs using a bash script because doing each one manually would be tedious and would take me until 2015 to complete!

From looking at solutions to previous problems I've managed to get the following to work:

ssh -t user@server1 ssh server2

This takes me into 'server2' but I'm unable to run any commands using the bash script, ie.

ssh -t user@server1 "ssh server2; qfree"

Doesn't work and results in nothing happening. It's only when I 'exit' from 'server2' that I get a response from the terminal saying that the 'requested program couldn't be found'. I can only assume that the system is still trying to run the programs from 'server1' where they don't exist.

My understanding then is that it tries to run each action on 'server1', ie ssh server2 and then once it's finished with that qfree.

Is there a way to make it so that it connects to server1, then server2, and then runs a command from that server? As of yet I've had no luck and would appreciate any assistance.

Many Thanks
Jamie
# 2  
Old 01-09-2010
Hi, have you tried:
Code:
ssh user@server1 ssh server2 qfree

Or with the -t option if required.
# 3  
Old 01-09-2010
Hi,
one thing You can try to facilitate talking to server 2 is to create a tunnel.

Example, run this on server1:

Code:
ssh -f user@server2 -L server1:9999:server2:22 -N

You may choose another port number. now when You connect to
Code:
ssh user@server1 -p 9999

You will actually logon to server2. And You can do

Code:
ssh user@server1 -p 9999 command

to run command on server2. Note that if You run a command like this Your "non-interactive" ssh shell has a limited environment, compared to an interactive login. The command You expect to run may not be in path.

You can do it in two steps, first transfer Your command/script and then run it.

Code:
scp -P 9999 crap.sh server1:/tmp
ssh server1 -p 9999  /tmp/crap.sh

Could this be something?

Best regards,
Lakris
# 4  
Old 01-09-2010
Yeah I've tried that but it results in the same problem above - command not recognised. I use the -t parameter because it removes the pseudo terminal error that appears without it. Logically I would have thought that what I'm trying to achieve is possible but I'm having no luck. If it's impossible to do I'd appreciate to know that.

The only way I can think of making this work is to write some form of script that runs each line in turn on a terminal (so that the program ends up getting run on server2) but I can't think of a way to write this with a bash script.


Jamie

---------- Post updated at 01:15 PM ---------- Previous update was at 01:13 PM ----------

Lakris, just received your reply as I posted my last. The servers have unfortunately just gone down for maintenance so I'll test your solution as soon as I get a chance. Thanks a lot for the post.

---------- Post updated at 05:08 PM ---------- Previous update was at 01:15 PM ----------

I've just tried your solution Lakris and it looks like its a good option. However when I run the command you gave me on server1 I get the following response with any port number I choose:

bind: Cannot assign requested address

I can only assume that IT admin has restricted access on tunnelling. Is there any other options available to me?

Thanks
Jamie
# 5  
Old 01-10-2010
I have to do this on occasion...

Your syntax is almost correct.

try:

ssh -t user@server1 "ssh server2 \"qfree\""

You running a remote ssh command, and then another one.. so the single ";" isn't going to work like you want it to in this case.
# 6  
Old 01-11-2010
Quote:
Originally Posted by mcintosh.jamie
bind: Cannot assign requested address

I can only assume that IT admin has restricted access on tunneling. Is there any other options available to me?

Thanks
Jamie

Ok, it is possible that opening ports is restricted.

But this works for me, without tunneling:

ssh user@server1 "ssh server2 ls"

The reason semi-colon ";" doesn't work is that it is a command to be executed on server1, AFTER the command ssh server2 is finished. You need to group commands differently. And if You still get "command not found" maybe transfer the command first, specify a full path name or something like that.

Best regards,
Lakris
# 7  
Old 01-13-2010
Thankyou trey85stang, I tried your solution but received the same problem as before. I've been trying all sorts of syntax arrangements with no success.

Lakris I've tried running the programs using the full path which has a lot more success. It looks as if it's actually running something on the second server now.

However quite a few of the programs I need to run require access to other programs. ie running qfree requires qhost so I can't successfully run qfree using its full path. This is written in its own programming so I can't change this.

ie.
ssh hamilton /usr/local/Cluster-Apps/sge/bin/lx26-amd64/qfree

Responds with:
sh: qhost: command not found
-------------------------------------------------------
QUEUE PE FREE USED TOTAL
-------------------------------------------------------
-------------------------------------------------------

So what I'm guessing now is that it's successfully running qfree but failing to find qhost.

Is there any way I could pre load these programs into some form of cash (like the /bin store) when I connect into the second server so I don't need to run any program using the full path?

Thanks for your continued support
Jamie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remote ssh commands

ssh XXXXX@XXXXX'. $HOME/.profile 2>/dev/null;cd $COMMON_TOP/admin/scripts/$CONTEXT_NAME; adcmctl.sh start "apps${AppsPass}" || cd $ADMIN_SCRIPTS_HOME; adcmctl.sh start "apps${AppsPass}"' As per our business requirement we should be able to execute the above two commands seprated with or ( || )... (4 Replies)
Discussion started by: Y.balakrishna
4 Replies

2. Shell Programming and Scripting

SSH + multiple commands

Sorry for the basic question here, but I can't seem to find an answer anywhere. I want to alias a command that will ssh and then open up bbedit all in 1 command. alias bbb 'ssh NAME@SERVER bbedit' returns: bash: bbedit: command not found It returns this same thing for anything except... (1 Reply)
Discussion started by: busdriver
1 Replies

3. Programming

Using Commands over SSH using Sudo

Is there a way to transfer my sudo password via ssh so that I can copy files remotely and pass them locally, so: cat sudo-passwd-file|ssh -t user@10.7.0.180 'sudo find / -depth|cpio -oacv|gzip' > /path/to/dir/file.cpio.gz I am in the process of a creating a script. Everytime I try and just... (16 Replies)
Discussion started by: metallica1973
16 Replies

4. Shell Programming and Scripting

ssh commands in Perl

I'm new to perl. I have a input file with pipe delimited lines which have server_name, and file names as fields. The file names have wild card (*) in the input feed file. The actual file names will be suffixed with timestamps. Ex: Actual files in remote servers server_1/abc_20110801.txt... (3 Replies)
Discussion started by: bperl
3 Replies

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

6. Shell Programming and Scripting

executing commands over ssh

Hi I am trying to send a command over ssh with a parameter but the shell fails to expand the command properly any ideas what am i doing wrong with this. This is ssh on AIX for i in 71 72 73 74 75 do for server in server1 server2 do somestr="Some String" echo "$server... (3 Replies)
Discussion started by: xiamin
3 Replies

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

8. Shell Programming and Scripting

remote ssh commands help

I have a script that I'm trying to use that will check for files on a remote machine and if there is files copy the files, modify the perm on the copied files, and then delete the files off of the remote server. Right now I need to get the correct syntax so the the remote commands still interpret... (4 Replies)
Discussion started by: jcalisi
4 Replies

9. Shell Programming and Scripting

ssh and commands

can someone point me to where it explains how to set the right commands wd ssh? cat something | while read h; do awk 'BEGIN {FS="\n"; RS=""; ORS="\n\n"} {if ($0~/'$h'/) print hdrvar,"\n",$0 }' /something/somedata ; done above works in when ssh'ing into linux machine invoke like this ssh... (10 Replies)
Discussion started by: convenientstore
10 Replies

10. UNIX for Dummies Questions & Answers

SSH Compress Commands

I am brand new to unix and am hoping someone can start me in the right direction. I hope I have the right forum but if not can a moderator move this thread to the right area. I am moving servers and need to back up a folder and its sub folders into a .tar.gz file then want to upload that file... (2 Replies)
Discussion started by: kushti
2 Replies
Login or Register to Ask a Question