0

I have a program called mdr-dev that I execute and that as a result opens port 1022.

After opening that port, I have to leave the program running to keep that port open. The snag is that it takes some time to execute so in the mean time I should wait for it to open that port!

Once the port is open, I want to open a new terminal and ssh through that port to a remote device.

The question is: How to write a bash or any other script for this?

(I already tried with the sleep command but it does not seem to work.)

Here is what I've tried so far:

#!/bin/bash
echo $MDR_ROOT
mdr-dev --root --mount /opt/tile/home /home --tunnel 1022 22 

After the mdr-dev command above's initialisation process, I want to automatically open a new terminal and run:

ssh -p 1022 root@localhost

Off course, I already tried:

--tunnel 1022 22 &
sleep 5m 
xterm -hold -e ssh -p 1022 root@localhost

but that doesn't seem to work...

How should I proceed?

  • 2
    I don't quite get what you are trying to do. You want to run mdr-dev command and upon its completion open new terminal window with ssh ? Why you cannot just add xterm command at the end of the script ? Also, command for xterm should be xterm -hold -e 'ssh -p 1022 root@localhost' – Sergiy Kolodyazhnyy Oct 6 '15 at 22:53
  • What does mdr-dev actually do? How do you know it's finished? – Fabby Oct 7 '15 at 19:56
  • @Serg mdr-dev after completion goes into interactive mode. I can suspend only after its complete initialization and it takes some time. So I wait till its initialization is done and then open a new terminal and run ssh -p 1022 root@localhost in the new terminal – andi99 Oct 7 '15 at 21:29
  • @Fabby mdr-dev initializes the device connected to the linux machine. It runs in an interactive mode. --root is one the options fed to it. It must be kept running after the initialization so I want to keep it running and open new terminal to do ssh – andi99 Oct 7 '15 at 21:33
  • So that's when you use &: mdr-dev keeps on running and that allows you to ssh in parallel... It's unclear to me what you're trying to accomplish here. Why don't you want &? – Fabby Oct 7 '15 at 21:40
1

As I don't have your mdr-dev program, it's difficult for me to test this, so here goes version 0.1. Please test and feed back in the comments.

#!/bin/bash  
#
# This script opens a port to tunnel through, then waits for the tunnel to be opened
# and then connects to the correct host using ssh  
# Answer to: http://askubuntu.com/questions/682316/how-to-wait-for-a-while-and-then-suspend-current-work-on-open-new-terminal-in-a

# Copyright (c) Fabby 2015

# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. See the GNU General Public License for more details.
# You DID NOT receive a copy of the GNU General Public License along with this program as the license is bigger then this program.
# Therefore, see http://www.gnu.org/licenses/ for more details.

# version 0.1   dd 2015-10-08   First version.

### Init ###
declare -r bDebug=false
declare szPortOpen=""
if $bDebug ; then
  set -x
  declare iDebugTimeOut=30 #seconds
fi

### Main ###
echo "$MDR_ROOT"
# open tunnel:
mdr-dev --root --mount /opt/tile/home /home --tunnel 1022 22 &

if $bDebug ; then
  read -t $iDebugTimeOut -p "Hit [Enter] to continue..."
fi

#Wait for the tunnel to open
while [ -z "$szPortOpen" ]
do
  sleep 1m
  netstat -atn | grep ":1022" | (read szPortOpen; )
  if $bDebug ; then
    echo $szPortOpen
    read -t $iDebugTimeOut -p "Hit [Enter] to continue..."
  fi
done

# Now ssh to the server 
xterm -hold -e ssh -p 1022 root@localhost
  • As you're a reputation 1 user: If this answer helped you, don't forget to click the grey at the left of this text, which means Yes, this answer is valid! ;-) – Fabby Oct 8 '15 at 8:37
  • thank you. System got stuck is what happened. So used some print statements. after mdr-dev --root --mount /opt/tile/home /home --tunnel 1022 22 & echo "end main" inside if $bDebug loop echo "inside loop" inside while loop after sleep 1m echo "sleep ended" and just before ssh` command I wrote doing ssh. Here is the log of these print statements, end main, italic sleep ended sleep ended sleep ended italic ......So I think it never came out of while loop. – andi99 Oct 8 '15 at 19:31
  • Can you post your version to paste.ubuntu.com? Because mine does contain debug statements... Did you turn on debug? – Fabby Oct 8 '15 at 21:06
  • here is a little modification to your code just to understand your code flow. paste.ubuntu.com/12729567 – andi99 Oct 10 '15 at 0:23
  • Reading tomorrow as I'm falling over from exhaustion. Sorry about this. – Fabby Oct 11 '15 at 1:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.