LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-12-2017, 08:31 PM   #1
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Rep: Reputation: 51
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.

^.^
 
Old 10-13-2017, 01:07 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729
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.
 
1 members found this post helpful.
Old 10-13-2017, 07:47 AM   #3
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
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.
 
Old 10-13-2017, 08:15 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729
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?

Last edited by Turbocapitalist; 10-13-2017 at 08:47 AM. Reason: markup
 
Old 10-13-2017, 08:19 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,945

Rep: Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325
Yes, would be nice to explain what do you really want to achieve, probably there is a way...
 
Old 10-13-2017, 09:03 AM   #6
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Post

Quote:
Originally Posted by pan64 View Post
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).
 
Old 10-13-2017, 09:07 AM   #7
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by Turbocapitalist View Post
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...
 
Old 10-13-2017, 09:09 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729
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.
 
1 members found this post helpful.
Old 10-13-2017, 09:11 AM   #9
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729
Quote:
Originally Posted by dedec0 View Post
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.
 
1 members found this post helpful.
Old 10-13-2017, 09:29 AM   #10
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
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.

Last edited by dedec0; 10-13-2017 at 10:02 AM.
 
Old 10-13-2017, 09:55 AM   #11
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729
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.
 
1 members found this post helpful.
Old 10-13-2017, 10:15 AM   #12
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
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.
 
Old 10-13-2017, 10:41 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,945

Rep: Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325
in that case you may try to check logs to find the reason (on both client/server side).
 
Old 10-13-2017, 10:47 AM   #14
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Talking

Quote:
Originally Posted by Turbocapitalist View Post
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
 
Old 10-13-2017, 10:59 AM   #15
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729Reputation: 3729
Quote:
Originally Posted by dedec0 View Post
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.

Last edited by Turbocapitalist; 10-13-2017 at 11:04 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem using screen: Cannot open your terminal '/dev/pts/0' - please check. stefaandk Linux - General 18 06-17-2014 07:52 PM
[SOLVED] Terminal Emulation & /dev/pts and /dev/shm Issues on Kernel 3.4.3 unassailable Gentoo 8 10-27-2012 11:03 PM
pseudo terminals: /dev/ptmx <-> /dev/pts/ vs. /dev/ptyp <-> /dev/ttyp rtspitz Linux - Software 2 12-02-2011 02:07 PM
Connect via SSH to remote /dev/pts/X antonko Linux - Newbie 2 01-23-2011 09:40 AM
Cannot open your terminal '/dev/pts/5' - please check Laodiceans Slackware 3 12-27-2009 07:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration