Script SSH Interactive Console that needs escaped chars

2

1

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.

Luke Exton

Posted 2016-02-13T00:40:51.397

Reputation: 271

you would probably need expect script to achieve this. But why? You can define port forwarding on command-line much easier. – Jakuje – 2016-02-13T10:06:04.557

I was hoping to be able to define a set of bash functions that could modify the existing ssh session. I'm not great at this really low level stuff. But can't see any reason it couldn't be done in bash. Expect might work well though. I'll see what I can do there to get it to play nicely. – Luke Exton – 2016-02-13T10:12:02.837

The problem is that these escape chars are NOT evaluated by bash, but locally by your ssh client. – Jakuje – 2016-02-13T10:14:04.110

So the escape chars I want to send would need to be evaluated by the ssh client on my local machine right? Rather than bash on the remote end. Even sending this via tcl/expect then wouldn't evaluate it on the client side? – Luke Exton – 2016-02-13T10:21:01.590

expect should do that, but I didn't try yet. There are some references.

– Jakuje – 2016-02-13T10:24:50.040

Answers

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

Posted 2016-02-13T00:40:51.397

Reputation: 7 981