0

I'm use ssh -T foo@bar to login a remote host.But echo $? command response 1 after login success.
Besides, echo $? return 0 if I'm use ssh -t foo@bar.

Code 1 means Catchall for general errors.

  1. What's the means in that stage?
  2. How to fix it?
    Thanks

UPDATE #1
Some clarify:
1. Q: Are you running echo $? on the server after logging in, or on the client after logging out?
A: echo $? runs after login server.It is executed on remote server side.
2. Q: How are you able to log in if you disable TTY allocation with -T?
A: It just works and I have test on many machines.
3. Q: Do you not want a shell session maybe?
A: No. I'm just not want use pseudo-terminal.It is also a ssh session. I need the "clean" message used at a program.It's not a interactive environment.

Thanks again!

UPDATE #2
some environment file.
1. ~/.profile file

if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi
fi

tty -s && mesg n  
  1. ~/.bashrc file :

    # ~/.bashrc: executed by bash(1) for non-login shells.
    # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
    # for examples
    
    # If not running interactively, don't do anything
    [ -z "$PS1" ] && return
    
    # don't put duplicate lines in the history. See bash(1) for more options
    # ... or force ignoredups and ignorespace
    HISTCONTROL=ignoredups:ignorespace
    
    # append to the history file, don't overwrite it
    shopt -s histappend
    
    # for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
    HISTSIZE=1000
    HISTFILESIZE=2000
    
     # check the window size after each command and, if necessary,
     # update the values of LINES and COLUMNS.
    shopt -s checkwinsize
    
     # make less more friendly for non-text input files, see lesspipe(1)
    [ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
    
      # set variable identifying the chroot you work in (used in the prompt below)
    if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
        debian_chroot=$(cat /etc/debian_chroot)
    fi
    
     # set a fancy prompt (non-color, unless we know we "want" color)
    case "$TERM" in
        xterm-color) color_prompt=yes;;
    esac
    
     # uncomment for a colored prompt, if the terminal has the capability; turned
     # off by default to not distract the user: the focus in a terminal window
     # should be on the output of commands, not on the prompt
     #force_color_prompt=yes
    
    if [ -n "$force_color_prompt" ]; then
        if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
        # We have color support; assume it's compliant with Ecma-48
        # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
        # a case would tend to support setf rather than setaf.)
        color_prompt=yes
        else
        color_prompt=
        fi
    fi
    
    if [ "$color_prompt" = yes ]; then
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    else
        PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
    fi
    unset color_prompt force_color_prompt
    
     # If this is an xterm set the title to user@host:dir
    case "$TERM" in
    xterm*|rxvt*)
        PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
        ;;
    *)
        ;;
    esac
    
     # enable color support of ls and also add handy aliases
    if [ -x /usr/bin/dircolors ]; then
        test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
        alias ls='ls --color=auto'
        #alias dir='dir --color=auto'
        #alias vdir='vdir --color=auto'
    
        alias grep='grep --color=auto'
        alias fgrep='fgrep --color=auto'
        alias egrep='egrep --color=auto'
    fi
    
     # some more ls aliases
    alias ll='ls -alF'
    alias la='ls -A'
    alias l='ls -CF'
    
     # Alias definitions.
     # You may want to put all your additions into a separate file like
     # ~/.bash_aliases, instead of adding them here directly.
     # See /usr/share/doc/bash-doc/examples in the bash-doc package.
    
    if [ -f ~/.bash_aliases ]; then
        . ~/.bash_aliases
    fi
    
     # enable programmable completion features (you don't need to enable
     # this, if it's already enabled in /etc/bash.bashrc and /etc/profile
     # sources /etc/bash.bashrc).
     #if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
     #    . /etc/bash_completion
     #fi
    
  • Are you running echo $? on the server after logging in, or on the client after logging out? Also, how are you able to log in if you disable TTY allocation with -T? Do you not want a shell session maybe? – Kusalananda Aug 21 '17 at 8:48
  • hi, I'm just updated on the question.@Kusalananda – LoranceChen Aug 21 '17 at 9:01
  • It is likely something in your shell startup files. If you are using bash, see if anything in .bash_profile or .bashrc requires a TTY to work correctly (and which fails when there isn't one, giving rise to the non-zero exit status in $?). – Kusalananda Aug 21 '17 at 9:05
  • Hi, does the config file some error here? I'm don't know how to check it. Sorry for edit broken.... @Kusalananda – LoranceChen Aug 21 '17 at 9:27
1

Since you use tty -s in your .profile file, and since the login shell does not have a TTY when logging in with ssh -T, this command will fail and set $? to 1.

This is expected and the full command

tty -s && mesg n

relies on this so that mesg n (which "disallows messages from other users" through talk and write, something that I believe is rarely used nowadays) does not execute if there is no TTY.

To "fix" this, simply comment out or remove that line from .profile. It will have little impact on anything.

  • Hi, it was fixed when I remove the command! Besides, I'm use man tty, the -s is explained with print nothing, only return an exit status.Can you explain what's the means and why it broken? – LoranceChen Aug 21 '17 at 9:53
  • Or change it to if tty -s; then mesg n; fi or ! tty -s || mesg n. Or add a : make sure the exit status is 0 at the end of the .profile – Stéphane Chazelas Aug 21 '17 at 9:54
  • What's the exit status?I'm just login the host why show me a exit status? – LoranceChen Aug 21 '17 at 10:01
  • @LoranceChen The "exit status" is the integer code that a program returns when it exits. An exit status of zero usually means that the program "succeeded" while a non-zero exit status usually means that "something went wrong", or that a test failed. grep (if you are familiar with that command), for example, returns a non-zero exit status if the pattern that it looked for in a file was not found. – Kusalananda Aug 21 '17 at 10:03
  • thanks, It's clear.But I'm also not understand what does tty -s && mesg n do. Expecially, "disallows messages from other users" means. – LoranceChen Aug 21 '17 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.