Detect whether the current terminal is through mosh or not

Multi tool use
Multi tool use

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I am trying to figure out a way to detect whether the current terminal/connection (and under a tmux session as well) is through mosh or not.



From this thread, I found which pseudo-terminal session I am currently on:



$ tty
/dev/pts/69


So, I need to some information of the process that spawned this pseudo-terminal, or owns this tty as a children. With the information, perhaps I might be able to determine whether it is from sshd or mosh. But how can I do that?



Another challenge: If the current shell is under tmux, the retrieved tty might not match the sshd/mosh-server information since tmux also allocates another pseudo-terminal. Regardless of how the tmux session was created, I'll need to distinguish my current connection is from SSH or mosh. How will it be possible?



Some trials:



(1) For SSH, it was possible to find the sshd process that matches the tty:



$ ps x | grep sshd | grep 'pts/27'
5270 ? S 0:00 sshd: wookayin@pts/27


So I can know the current connection is through SSH. However, through mosh, I could not find any relevant information.



(2) Using environment variables like SSH_CLIENT or SSH_TTY might not work because both ssh/mosh set these variables, and it is even wrong inside a tmux session.










share|improve this question





















  • Using ps -p $PPID, I could detect the parent is mosh-server, tmux: server or sshd. However, tmux+mosh is still a complicated situation.
    – Jongwook Choi
    Oct 1 '17 at 23:26















up vote
0
down vote

favorite












I am trying to figure out a way to detect whether the current terminal/connection (and under a tmux session as well) is through mosh or not.



From this thread, I found which pseudo-terminal session I am currently on:



$ tty
/dev/pts/69


So, I need to some information of the process that spawned this pseudo-terminal, or owns this tty as a children. With the information, perhaps I might be able to determine whether it is from sshd or mosh. But how can I do that?



Another challenge: If the current shell is under tmux, the retrieved tty might not match the sshd/mosh-server information since tmux also allocates another pseudo-terminal. Regardless of how the tmux session was created, I'll need to distinguish my current connection is from SSH or mosh. How will it be possible?



Some trials:



(1) For SSH, it was possible to find the sshd process that matches the tty:



$ ps x | grep sshd | grep 'pts/27'
5270 ? S 0:00 sshd: wookayin@pts/27


So I can know the current connection is through SSH. However, through mosh, I could not find any relevant information.



(2) Using environment variables like SSH_CLIENT or SSH_TTY might not work because both ssh/mosh set these variables, and it is even wrong inside a tmux session.










share|improve this question





















  • Using ps -p $PPID, I could detect the parent is mosh-server, tmux: server or sshd. However, tmux+mosh is still a complicated situation.
    – Jongwook Choi
    Oct 1 '17 at 23:26













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to figure out a way to detect whether the current terminal/connection (and under a tmux session as well) is through mosh or not.



From this thread, I found which pseudo-terminal session I am currently on:



$ tty
/dev/pts/69


So, I need to some information of the process that spawned this pseudo-terminal, or owns this tty as a children. With the information, perhaps I might be able to determine whether it is from sshd or mosh. But how can I do that?



Another challenge: If the current shell is under tmux, the retrieved tty might not match the sshd/mosh-server information since tmux also allocates another pseudo-terminal. Regardless of how the tmux session was created, I'll need to distinguish my current connection is from SSH or mosh. How will it be possible?



Some trials:



(1) For SSH, it was possible to find the sshd process that matches the tty:



$ ps x | grep sshd | grep 'pts/27'
5270 ? S 0:00 sshd: wookayin@pts/27


So I can know the current connection is through SSH. However, through mosh, I could not find any relevant information.



(2) Using environment variables like SSH_CLIENT or SSH_TTY might not work because both ssh/mosh set these variables, and it is even wrong inside a tmux session.










share|improve this question













I am trying to figure out a way to detect whether the current terminal/connection (and under a tmux session as well) is through mosh or not.



From this thread, I found which pseudo-terminal session I am currently on:



$ tty
/dev/pts/69


So, I need to some information of the process that spawned this pseudo-terminal, or owns this tty as a children. With the information, perhaps I might be able to determine whether it is from sshd or mosh. But how can I do that?



Another challenge: If the current shell is under tmux, the retrieved tty might not match the sshd/mosh-server information since tmux also allocates another pseudo-terminal. Regardless of how the tmux session was created, I'll need to distinguish my current connection is from SSH or mosh. How will it be possible?



Some trials:



(1) For SSH, it was possible to find the sshd process that matches the tty:



$ ps x | grep sshd | grep 'pts/27'
5270 ? S 0:00 sshd: wookayin@pts/27


So I can know the current connection is through SSH. However, through mosh, I could not find any relevant information.



(2) Using environment variables like SSH_CLIENT or SSH_TTY might not work because both ssh/mosh set these variables, and it is even wrong inside a tmux session.







ssh tty mosh






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Oct 1 '17 at 17:26









Jongwook Choi

50143




50143











  • Using ps -p $PPID, I could detect the parent is mosh-server, tmux: server or sshd. However, tmux+mosh is still a complicated situation.
    – Jongwook Choi
    Oct 1 '17 at 23:26

















  • Using ps -p $PPID, I could detect the parent is mosh-server, tmux: server or sshd. However, tmux+mosh is still a complicated situation.
    – Jongwook Choi
    Oct 1 '17 at 23:26
















Using ps -p $PPID, I could detect the parent is mosh-server, tmux: server or sshd. However, tmux+mosh is still a complicated situation.
– Jongwook Choi
Oct 1 '17 at 23:26





Using ps -p $PPID, I could detect the parent is mosh-server, tmux: server or sshd. However, tmux+mosh is still a complicated situation.
– Jongwook Choi
Oct 1 '17 at 23:26











1 Answer
1






active

oldest

votes

















up vote
0
down vote













I have come up with a decent solution to this, and wrapped it as a simple script: is_mosh.



The idea is quite simple: (i) to find the tmux client that is attached to the current tmux session, and then (ii) to search its ancestor processes to find if there is a mosh process.



It might not be complete, depending on environments, but I could succesfully detect and apply 24-bit color feature only if it's not running under mosh (as mosh doesn't support 24-bit colors). Here is a solution.






share|improve this answer




















  • To avoid being flagged as a link-only answer, could you import the relevant bits of code into this Answer?
    – Jeff Schaller
    Oct 18 '17 at 11:09










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395491%2fdetect-whether-the-current-terminal-is-through-mosh-or-not%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













I have come up with a decent solution to this, and wrapped it as a simple script: is_mosh.



The idea is quite simple: (i) to find the tmux client that is attached to the current tmux session, and then (ii) to search its ancestor processes to find if there is a mosh process.



It might not be complete, depending on environments, but I could succesfully detect and apply 24-bit color feature only if it's not running under mosh (as mosh doesn't support 24-bit colors). Here is a solution.






share|improve this answer




















  • To avoid being flagged as a link-only answer, could you import the relevant bits of code into this Answer?
    – Jeff Schaller
    Oct 18 '17 at 11:09














up vote
0
down vote













I have come up with a decent solution to this, and wrapped it as a simple script: is_mosh.



The idea is quite simple: (i) to find the tmux client that is attached to the current tmux session, and then (ii) to search its ancestor processes to find if there is a mosh process.



It might not be complete, depending on environments, but I could succesfully detect and apply 24-bit color feature only if it's not running under mosh (as mosh doesn't support 24-bit colors). Here is a solution.






share|improve this answer




















  • To avoid being flagged as a link-only answer, could you import the relevant bits of code into this Answer?
    – Jeff Schaller
    Oct 18 '17 at 11:09












up vote
0
down vote










up vote
0
down vote









I have come up with a decent solution to this, and wrapped it as a simple script: is_mosh.



The idea is quite simple: (i) to find the tmux client that is attached to the current tmux session, and then (ii) to search its ancestor processes to find if there is a mosh process.



It might not be complete, depending on environments, but I could succesfully detect and apply 24-bit color feature only if it's not running under mosh (as mosh doesn't support 24-bit colors). Here is a solution.






share|improve this answer












I have come up with a decent solution to this, and wrapped it as a simple script: is_mosh.



The idea is quite simple: (i) to find the tmux client that is attached to the current tmux session, and then (ii) to search its ancestor processes to find if there is a mosh process.



It might not be complete, depending on environments, but I could succesfully detect and apply 24-bit color feature only if it's not running under mosh (as mosh doesn't support 24-bit colors). Here is a solution.







share|improve this answer












share|improve this answer



share|improve this answer










answered Oct 8 '17 at 19:11









Jongwook Choi

50143




50143











  • To avoid being flagged as a link-only answer, could you import the relevant bits of code into this Answer?
    – Jeff Schaller
    Oct 18 '17 at 11:09
















  • To avoid being flagged as a link-only answer, could you import the relevant bits of code into this Answer?
    – Jeff Schaller
    Oct 18 '17 at 11:09















To avoid being flagged as a link-only answer, could you import the relevant bits of code into this Answer?
– Jeff Schaller
Oct 18 '17 at 11:09




To avoid being flagged as a link-only answer, could you import the relevant bits of code into this Answer?
– Jeff Schaller
Oct 18 '17 at 11:09

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395491%2fdetect-whether-the-current-terminal-is-through-mosh-or-not%23new-answer', 'question_page');

);

Post as a guest













































































2c 0LBFV j2,HfdbaSXM2 OJcp4Lb,t5Q 3bnprT8vtXKBLLT,oJ
p9AtG9hoMZ,rsJhsL53,3PeJJYNWCSmdu2LLiEI

Popular posts from this blog

How to check contact read email or not when send email to Individual?

How many registers does an x86_64 CPU actually have?

Displaying single band from multi-band raster using QGIS