
Ich habe ein Programm namens, mdr-dev
das ich ausführe und das als Ergebnis Port 1022 öffnet.
Nachdem ich diesen Port geöffnet habe, muss ich das Programm laufen lassen, um den Port offen zu halten. Der Haken besteht darin, dass die Ausführung einige Zeit in Anspruch nimmt. In der Zwischenzeit muss ich also warten, bis der Port geöffnet wird.
Sobald der Port geöffnet ist, möchte ich ein neues Terminal öffnen und ssh
über diesen Port auf ein Remote-Gerät zugreifen.
Die Frage ist: Wie schreibt man hierfür ein Bash- oder anderes Skript?
(Ich habe es bereits mit dem Sleep-Befehl versucht, aber es scheint nicht zu funktionieren.)
Folgendes habe ich bisher versucht:
#!/bin/bash
echo $MDR_ROOT
mdr-dev --root --mount /opt/tile/home /home --tunnel 1022 22
Nach dem mdr-dev
Initialisierungsprozess des obigen Befehls möchte ich automatisch ein neues Terminal öffnen und Folgendes ausführen:
ssh -p 1022 root@localhost
Natürlich habe ich bereits versucht:
--tunnel 1022 22 &
sleep 5m
xterm -hold -e ssh -p 1022 root@localhost
aber das scheint nicht zu funktionieren ...
Wie soll ich vorgehen?
Antwort1
Da ich Ihr Programm nicht habe mdr-dev
, ist es für mich schwierig, es zu testen, also hier Version 0.1. Bitte testen Sie es und geben Sie in den Kommentaren Feedback.
#!/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