How to get color output from SSH command

5,931

Yes, it is possible using -tt switches, which will force TTY allocation and it will trick the remote program to write also the colors. Minimal test I can run on my system:

Without colors:

ssh localhost "cowsay hello | lolcat"

With colors:

ssh -tt localhost "cowsay hello | lolcat"
Share:
5,931

Related videos on Youtube

Andy Fleming
Author by

Andy Fleming

Updated on September 18, 2022

Comments

  • Andy Fleming
    Andy Fleming almost 2 years

    I'm trying to run some commands remotely and recieve the output back with color. What am I missing?

    Here's what I've got so far:

    example.sh

    . /etc/profile
    [[ -f ~/.profile ]] && . ~/.profile;
    [[ -f ~/.bashrc ]] && . ~/.bashrc;
    echo "hello remote"
    cd /vagrant/repositories/repo
    pwd
    phpunit --color
    

    I'm piping the script into ssh as follows:

    cat example.sh | ssh vagrant@localhost /bin/bash -s

    • Jakuje
      Jakuje over 8 years
      the linux tools usually recognize that on the other side of pipe is not a terminal. I don't think there is some universal way to force all of them to do so.
    • Andy Fleming
      Andy Fleming over 8 years
      @Jakuje — Is the same true for ssh -T?
    • Jakuje
      Jakuje over 8 years
      -T removes the TTY allocation. Probably you can trick it with ssh -tt, but I haven't try it yet.
  • Andy Fleming
    Andy Fleming over 8 years
    This does output color, but the ssh session does not automatically exit like in the normal "single command" mode.
  • Jakuje
    Jakuje over 8 years
    it does for me. What server/client version of openssh are you using?
  • Andy Fleming
    Andy Fleming over 8 years
    OpenSSH_6.6.1p1 Ubuntu-2ubuntu2, OpenSSL 1.0.1f 6 Jan 2014
  • Jakuje
    Jakuje over 8 years
    I have 6.7p1. It should work the same if you don't have any special shell on remote side. Adding exit at the end of command can do some change, but not sure ...