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"
Related videos on Youtube
Author by
Andy Fleming
Updated on September 18, 2022Comments
-
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 over 8 yearsthe 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 over 8 years@Jakuje — Is the same true for
ssh -T
? -
Jakuje over 8 years
-T
removes the TTY allocation. Probably you can trick it withssh -tt
, but I haven't try it yet.
-
-
Andy Fleming over 8 yearsThis does output color, but the ssh session does not automatically exit like in the normal "single command" mode.
-
Jakuje over 8 yearsit does for me. What server/client version of openssh are you using?
-
Andy Fleming over 8 years
OpenSSH_6.6.1p1 Ubuntu-2ubuntu2, OpenSSL 1.0.1f 6 Jan 2014
-
Jakuje over 8 yearsI 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 ...