LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   If I echo into /dev/pts/N, in a terminal using SSH, will it (https://www.linuxquestions.org/questions/linux-software-2/if-i-echo-into-dev-pts-n-in-a-terminal-using-ssh-will-it-4175615564/)

dedec0 10-12-2017 08:31 PM

If I echo into /dev/pts/N, in a terminal using SSH, will it
 
If I echo into /dev/pts/N, where pts/N is a terminal using SSH, will it send something to the server that SSH is connected to?

Code:

# In terminal 1, which is connected to /dev/pts/5, we
# log into a server with SSH and leave it open:
$ ssh me@server
Password:
Welcome to server!
$


#  Now, in terminal 2, connected to other /dev/pts/N
# in the local machine, I do the command:
$ echo 'Vamos brincar!' > /dev/pts/5

#  I can see "Vamos brincar!" written in terminal 1, but
# I need to know: did it send something to server? Can I
# do that? How?
#

I also do not know how I would test and check that myself. If you know how, please say it. It will also solve this thread.

^.^

Turbocapitalist 10-13-2017 01:07 AM

It's only sending to the PTS itself and not going over the SSH session to the remote server. Try the following. On the remote server:

Code:

cat >> /tmp/foo
Then on the local machine, in a different terminal, send something to the other PTS

Code:

echo "largue monos" > /dev/pts/N
Where N is the right PTS.

Then back in the SSH session type something and then close the file with a ctrl-d

Code:

bar
^D

Then check that file and you'll see only what you typed in the SSH session, not what came from the other PTS.

dedec0 10-13-2017 07:47 AM

I see.

Is there a way I can send something to an open SSH session without it being focused? Maybe a kind of keyboard event.

Turbocapitalist 10-13-2017 08:15 AM

Well, a bit of a kludge might be to run your SSH session inside a named tmux session and then use tmux to send to it.

Code:

tmux new-session -s foobar 'ssh -p 22 server.example.com'
Then in the other window use "send-keys"

Code:

tmux send-keys -t foobar 'ls' C-m
There should be a way to use the SSH client directly, but I am not so familiar with its inner workings. What is your real goal with this?

pan64 10-13-2017 08:19 AM

Yes, would be nice to explain what do you really want to achieve, probably there is a way...

dedec0 10-13-2017 09:03 AM

Quote:

Originally Posted by pan64 (Post 5769468)
Yes, would be nice to explain what do you really want to achieve, probably there is a way...

My problem is not too hard. I have been "not really solving" it with some "tricks".

The situation is that my Internet provider silently kills SSH sessions if they are silent for 5 minutes - which is common for me (and everybody, I guess) to do. I created a Bash script to echo something every second until I press any key:

Code:

#!/bin/bash

if [ -t 0 ]; then
    stty -echo -icanon -icrnl time 0 min 0;
fi

count=0
keypress=''
while [ "$keypress" = "" ]; do
    let count+=1
    echo -ne $count'\r'
    sleep 1
    keypress="`cat -v`"
done

# These echoes clean a few lines and move the
# cursor up and back to make this script more
# invisible after it finishes
echo -en "\r\033[0K"
echo -en "\r\033[1A"
echo -en "\r\033[0K"
echo -en "\r\033[1A"
echo -en "\r\033[0K"

if [ -t 0 ]; then
    stty sane;
fi

exit 0

I call this script from the command line, if I know I will leave an SSH session open for a few minutes (I did this in two sessions to write this post). I also call it from remote Makefiles to give me time to think about messages it may output. I still lose some connections when I think or do anything eventual away from the SSH sessions - which make me lose, for example, my Vim sessions' undo/redo actions for all open files.

With the "echo to the SSH session" idea, I plan to make something to "ping" each SSH session every 4 minutes. Something neutral like a chosen keypress (space, backspace, F3, ...) or (shift/ctrl/alt if they may send something without a second "normal" key being pressed while we hold them) or anything else that is good for what I usually run (id est, something that will not probably break whatever I usually run in those sessions).

dedec0 10-13-2017 09:07 AM

Quote:

Originally Posted by Turbocapitalist (Post 5769467)
Well, a bit of a kludge might be to run your SSH session inside a named tmux session and then use tmux to send to it.

Code:

tmux new-session -s foobar 'ssh -p 22 server.example.com'
Then in the other window use "send-keys"

Code:

tmux send-keys -t foobar 'ls' C-m
There should be a way to use the SSH client directly, but I am not so familiar with its inner workings. What is your real goal with this?

Nice! In the first tmux command, may I use the SSH session like I would do calling SSH directly?

I will (or I am, at the moment you read this sentence) quickly read about tmux...

Turbocapitalist 10-13-2017 09:09 AM

Do the sessions still get killed by the ISP if the client has ServerAliveInterval set or if the server has ClientAliveInterval set? Either of those should send a heartbeat over the connection. You can set ServerAliveInterval specific to particular hosts in ~/.ssh/config. See "man ssh_config" for details.

Alternately, if you run tmux on the remote host, the clock in the default configuration should update every few seconds and thus send a few packets.

Turbocapitalist 10-13-2017 09:11 AM

Quote:

Originally Posted by dedec0 (Post 5769487)
Nice! In the first tmux command, may I use the SSH session like I would do calling SSH directly?

Yes, but you'd get far more advantage out of it by running it on the remote host instead.

Code:

ssh -t server.example.com 'tmux a || tmux'
That way if the connection does drop, it will connect to the old session in progress. But as mentioned above it may end up preventing the ISP from dropping the connection.

dedec0 10-13-2017 09:29 AM

Quote:

In the first tmux command, may I use the SSH session like I would do calling SSH directly?
Yes, I can use tmux for that. tmux is:

Quote:

Originally Posted by Wikipedia(en)
[...] a software application that can be used to multiplex several virtual consoles, allowing a user to access multiple separate terminal sessions inside a single terminal window or remote terminal session.

I will try to compile and locally install tmux in that server (for my user only, I have no admin access there). Seems a nice idea.

On the other hand, "GNU screen" is already installed there. May I send a keypress command to it, like you showed for tmux? Skimming through 'screen' manpage did not ring anything to me.

Turbocapitalist 10-13-2017 09:55 AM

You can also send keystrokes through screen. However, first I'd see if ServerAliveInterval does the job for you instead. You can test it this way:

Code:

ssh -o ServerAliveInterval=15 server.example.com
If that fixes your problem then you can add it to ~/.ssh/config for that particular remote host.

dedec0 10-13-2017 10:15 AM

Thank you very much for all your posts here, Turbocapitalist!

I will reopen one of the sessions I have now, calling ssh with that option, and not using my "stay alive" script. Hopefully it will be enough.
(:

I did not imagine that an SSH option could solve the problem because I use the same machine+OS+ssh I used a few years ago, when I had another provider - the problem never happened, even after hours without using an open SSH session. I assumed it should be something "not so common to make it the bad way, but not many customers would complain about" - and I did decide not to spend time calling support several times to solve that problem. I got around it as I could, as you all already know from my other posts here.

pan64 10-13-2017 10:41 AM

in that case you may try to check logs to find the reason (on both client/server side).

dedec0 10-13-2017 10:47 AM

Quote:

Originally Posted by Turbocapitalist (Post 5769508)
You can also send keystrokes through screen. However, first I'd see if ServerAliveInterval does the job for you instead. You can test it this way:

Code:

ssh -o ServerAliveInterval=15 server.example.com
If that fixes your problem then you can add it to ~/.ssh/config for that particular remote host.

That solved the issue. (: I tested it with a 6+ minutes pause, and the connection still worked.

Something important to note, that I read in ssh_config man page, is that:

Quote:

Originally Posted by man ssh_config
[...] the use of server alive messages is very
different from TCPKeepAlive

The server alive messages are sent through the
encrypted channel and therefore will not be
spoofable.

Now I have created a local ~/.ssh/config file with this:

Code:

# Do not let SSH sessions die for a lack of attention
ServerAliveInterval=31


Turbocapitalist 10-13-2017 10:59 AM

Quote:

Originally Posted by dedec0 (Post 5769523)
Now I have created a local ~/.ssh/config file with this:

Excellent. One thing to note with ~/.ssh/config is that since the client goes with the first match the directives must go from specific to general. It might not be so clear from the man page.

Code:

Host server1 server1.example.com
        User dedec0
        ServerAliveInterval 29

Host server2 server2.example.com
        IdentityFile ~/.ssh/server2.key.rsa

Host *.example.com
        ExitOnForwardFailure yes

Host *
        ServerAliveInterval 31

So server1 would get an interval of 29 seconds, but all others including server2 would get an interval of 31. But server1 and server2 can be reached either by their short names or full names.


All times are GMT -5. The time now is 01:03 AM.