2

I would like to modify my existing SSH connection via a shell script. So if I run an existing script remotely I would like it to open a new port to be tunnelled.

Interactively I can do this via:

ubuntu@6c1a17c3864c:~$ ~C
ssh> -R 9000:localhost:9000

As put much more clearly than I could: https://unix.stackexchange.com/questions/33557/using-an-already-established-ssh-channel.

Ideally I would like to use a shell script to interact via the escaped characters to adjust the existing connection.

I have looked at something like:

#!/bin/bash 
# Attempt 1
echo -n '\r\f \~\~C -L 9000:localhost:9000'
# Attempt 2
echo -e '\r\f\~\C -R 9000:localhost:9000'
printf '\~\C -L 9000:localhost:9000'
netstat -taln

As well as a few other combinations.

I have verified that both echo and printf are shell builtins.

I'm using Bash 4.3.11 x86_64-pc-linux-gnu.

1 Answers1

0

You would need expect script to achieve this. The problem is that these escape chars are NOT evaluated by bash, but locally by your ssh client. Example that should do the job:

#!/usr/bin/expect -f
set timeout 10
 exp_internal 1
spawn telnet $argv
expect "login:"
send "mylogin\n"
expect "Password:"
send "mypass\n"
expect '~$'
# Send some commands
close

Source

Jakuje
  • 10,827