Visit Our UNIX and Linux User Community

Getting SSH Output From Remote to Local Session?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting SSH Output From Remote to Local Session?
# 1  
Old 09-11-2016
Getting SSH Output From Remote to Local Session?

Hi everyone, after about 2 days of scratching my head on this one, I'm finally ready to punt this and ask for some actual help. Here's the situation. We have 1 server, that runs multiple VM's. To gain access to those VM's we ssh from host01 to the other vm hosts. For example when we first log into the machine, we're always on host01. Then from there we can ssh to host02, host03 etc... What I'd like to do is log into host01 as per normal, and have a script that ssh's to host03, runs a few commands on host03, but puts the output of those commands onto the tty of the screen of host01. The script I have looks like this:
#! /bin/bash  ssh -t -t $tar_host & <<EOF  sleep 2 echo "On host03" EOF  echo "worked?" exit 0

What this does is basically logs me onto host03, successfully, but there's no output from the echo "On host03" obviously.
What I would want is to be able to ssh onto host03, run a list of commands in the EOF here document list, but have all of the output from those commands make it's way back to my host01 tty..
Is something like this possible? Assuming that authentication to the ssh session isn't an issue (shared keys). Would I need to open up a separate tty session? I've seen a lot of recommendations about playing around with /dev/tty and /dev/pts/ etc.. but nothing concrete so far..
Thank guys, any help is appreciated!
# 2  
Old 09-11-2016
The single line shell script you have shown us:
#! /bin/bash  ssh -t -t $tar_host & <<EOF  sleep 2 echo "On host03" EOF  echo "worked?" exit 0

makes absolutely no sense.

I will assume that there are some <newline> characters in that script that have been lost in some copy and paste activities. Please show us the actual script (with the line breaks) AND tell us:
  1. what operating system you're using on host01 and the various VMs,
  2. what is the name of your script,
  3. exactly how your script is invoked, and
  4. how the tar_host variable is assigned a value for use in your script.
# 3  
Old 09-11-2016
My apologies! Yea the code tag thing put it all in one line for whatever reason...Here it is again...

#! /bin/bash  

ssh -t -t $tar_host & <<EOF  
sleep 2 
echo "On host03" 
echo "worked?" 
exit 0

The name of the script is just
I can't post the full script due to it having some proprietary elements, but just know that the $tar_host variable is the name of host03. So it's basically saying
ssh -t -t  host03 &  <<EOF

Also from host01 (where this script is run) I can do ssh host03 and it works without a hitch, since it can work without prompting for passwords by using the ssh keys.

The OS being used is RedHat 6 or7 I believe.
Lastly the script is invoked just by the user sourcing it with ./

Any other question let me know! Much appreciated
Moderator's Comments:
Mod Comment No! CODE tags DO NOT remove <newline> characters.
Please use CODE tags when displaying sample input, output, and code segments.

Last edited by Don Cragun; 09-11-2016 at 10:52 PM.. Reason: Add CODE and ICODE tags.
# 4  
Old 09-12-2016
You can choose not to show us your script and we can make lots of wild assumptions. Or, you could show us your complete script with proprietary information redacted. Instead you have shown us two other scripts. All I can do at this point is comment on the script you provided in post #3 in this thread (shown here with trailing spaces replaced by <space>:
#! /bin/bash  

ssh -t -t $tar_host & <<EOF<space><space>
sleep 2<space>
echo "On host03"<space>
echo "worked?"<space>
exit 0

  1. An asynchronous command in a shell script ends with the & that makes the preceding command asynchronous. Therefore, the here-document in this script does not provide input to the ssh command (ssh -t -t). Note that I left out $tar_host because nothing in this script sets it, you have given no indication that it is supplied a value and exported by the invoking shell, and you have stated that it no variables are passed into the script by defining them on the command line when you invoke the script.
  2. Why did I mark the trailing spaces in your script? Although most of them do not matter, trailing spaces following the delimiter in a here-document prevent the shell from recognizing that line as a terminator for that here-document.
  3. So, from #1 and #2 above, we have an ssh command with no host specified, no login specified, and no commands to be executed; and we have an unterminated here-document with no command defined to read the data in that here-document.
If what you are trying to do is to run ssh on host03 feeding it two commands in a here-document, you might want something more like:
#! /bin/bash  
{  ssh -t -t $tar_host <<EOF
   sleep 2
   echo "On host03"
echo "worked?"<space>
exit 0

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 09-12-2016
Hmm Thanks for the feedback Don, trust me it's not falling on deaf ears. I hope to learn much much more. So it was my understanding that the & at the end of a command, basically says to Bash, to execute the command, but run it "in the background" and move on to the next line while it's running (so it doesn't hang their waiting)

And wow, I didn't know that the two spaces following the EOF (ending of the here doc) would cause it problems! The syntax for here docs must be very very specific I see...

The list of commands will be much more than the simple ones I provided here. I was just short on time, and didn't want to list all of the commands I intended to run on host03, because most of them wouldn't make much sense, without explanation. Anyway, the point though of all of this, is to have the output of the commands, display on the tty of host01 though. I'm not in front of my computer to try out the suggested script you provided just yet, but how would I go about this?

Would I need to run the script in one tty session of host01, and watch for the output in another tty session of host01?

I thank you for your immense patience...and to anyone else who has an answer please feel free to chime in..

Previous Thread | Next Thread
Test Your Knowledge in Computers #505
Difficulty: Medium
An executable statement may not invoke (or call or execute) another procedure (also called subroutine, function, method, etc.),
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep remote multiple hosts output to local server

Hello all, i'm trying to create a report by greping a pattern on multiple remote hosts and creta a simple report, actually i did this, is ther any better way to do this. #!/bin/bash for host in `cat RemoteHosts` do ssh $host -C 'hostname 2>&1; grep ERROR /var/log/WebServer.log.2019-09-21... (0 Replies)
Discussion started by: charli1
0 Replies

2. Shell Programming and Scripting

Unable to ssh and list files in local directory from remote.

#!/bin/bash script_work_dir="/home/websys/TEST_dpi_42_file_trnsfer_engine/PORT22/script_work_area" script_config_dir="/home/websys/TEST_dpi_42_file_trnsfer_engine/PORT22/script_config" dpi_sourceServerList=$script_config_dir"/dpi_sourceServerList" dpi_srvr_42="rtm@"... (8 Replies)
Discussion started by: sadique.manzar
8 Replies

3. AIX

Need to remote control client's ssh session

Is there a way that I can remotely control a user's ssh session so I can see what they are doing and walk them through the problem they are having on my AIX based application? (2 Replies)
Discussion started by: De@nneG
2 Replies

4. Shell Programming and Scripting

Using expect to remote SSH and write to a local file

Hi Guys, So what I am trying to do is : Host A should do a SSH to Host B to F. Login to the remote host and gather the output of uptime and write to to a file in HostA. So by the end of the script, HostA should contain a file that contains the uptime output of Host B,C,D,E,F. Right now... (1 Reply)
Discussion started by: Junaid Subhani
1 Replies

5. Shell Programming and Scripting

How to Compare local & remote Files over ssh?

I want to make a script to compare list of files in terms of its size on local & remote server whose names are same & this is required over ssh. How can I accomplish this. Any help would be appreciated. (1 Reply)
Discussion started by: m_raheelahmed
1 Replies

6. Shell Programming and Scripting

ssh sending local variable to remote system

I am running a useradd script, which works locally but I want to take some of that local information and send it to a remote system, ssh keys are set up between the two systems. I am attaching the script, look at the section titled "Sending information to FTP2" Removed attachment, added... (0 Replies)
Discussion started by: slufoot80
0 Replies

7. Linux

Create VNC Session on remote machine on which ssh access is denied

Hi Folks, I want to create VNC session on the Remote RHEL machine on which ssh access is denied. Is there any way so that I can create VNC session without ssh access. Let me know all possible ways! (1 Reply)
Discussion started by: gydave
1 Replies

8. Solaris

How to reconnect to a disconnected remote ssh session

hi all How to reconnect to a disconnected remote ssh session on solaris 10 is there any way (4 Replies)
Discussion started by:
4 Replies

9. Shell Programming and Scripting

rsh help - getting the output of remote script to local server.

Hi, I have a script that runs for an hour. Have to run it on remote server and need the output it produces on the remote server to decide for failure or success. I run it through a Autosys Job which logs the outputs, both 1 & 2. I use the commands 1) rsh <SERVER> 'nohup /tmp/xyz.ksh &' 2)... (5 Replies)
Discussion started by: aster007
5 Replies

10. UNIX for Dummies Questions & Answers

mac 10.4>terminal>linux remote server>ssh login accepted>session closed-why?

mac 10.4>terminal>linux remote server>ssh login accepted>session closed-why? AHHHH!! I have been connecting to the server with the line: ssh The remote server accepts my password; logs me in with ssh; posts a lovely welcome message AND closes the session. Is this a "term... (0 Replies)
Discussion started by: xprankard
0 Replies

Featured Tech Videos