How do I make the apple terminal window auto change colour scheme when I ssh to a specific server

前端 未结 10 1796
甜味超标
甜味超标 2020-12-12 12:58

When I ssh into a remote production server I would like the colour scheme of my terminal window to change to something brigh and scary, preferably red, to warn me that I am

相关标签:
10条回答
  • 2020-12-12 13:46

    Here's a combined solution based on a couple of existing answers that handles the exit. Also includes a little extra if you don't want to deal with 16 bit color values.

    This should be put in your ~/.bash_profile

    # Convert 8 bit r,g,b,a (0-255) to 16 bit r,g,b,a (0-65535)
    # to set terminal background.
    # r, g, b, a values default to 255
    set_bg () {
        r=${1:-255}
        g=${2:-255}
        b=${3:-255}
        a=${4:-255}
    
        r=$(($r * 256 + $r))
        g=$(($g * 256 + $g))
        b=$(($b * 256 + $b))
        a=$(($a * 256 + $a))
    
        osascript -e "tell application \"Terminal\" to set background color of window 1 to {$r, $g, $b, $a}"
    }
    
    # Set terminal background based on hex rgba values
    # r,g,b,a default to FF
    set_bg_from_hex() {
        r=${1:-FF}
        g=${2:-FF}
        b=${3:-FF}
        a=${4:-FF}
    
        set_bg $((16#$r)) $((16#$g)) $((16#$b)) $((16#$s))
    }
    
    # Wrapping ssh command with extra functionality
    ssh() {
        # If prod server of interest, change bg color
        if ...some check for server list
        then
            set_bg_from_hex 6A 05 0C
        end
    
        # Call original ssh command
        if command ssh "$@"
        then
            # on exit change back to your default
            set_bg_from_hex 24 34 52
        fi
    }
    
    • set_bg - takes 4 (8 bit) color values
    • set_bg_from_hex - takes 4 hex values. most of my color references I use are in hex, so this just makes it easier for me. It could be taken a step further to actually parse #RRGGBB instead of RR GG BB, but it works well for me.
    • ssh - wrapping the default ssh command with whatever custom logic you want. The if statement is used to handle the exit to reset the background color.
    0 讨论(0)
  • 2020-12-12 13:47

    A lesser-known feature of Terminal is that you can set the name of a settings profile to a command name and it will select that profile when you create a new terminal via either Shell > New Command… or Shell > New Remote Connection….

    For example, duplicate your default profile, name it “ssh” and set its background color to red. Then use New Command… to run ssh host.example.com.

    It also matches on arguments, so you can have it choose different settings for different remote hosts, for example.

    0 讨论(0)
  • 2020-12-12 13:48

    You can set the $PS1 variable in your .bashrc.

    red='\e[0;31m'
    PS1="$\[${red}\]"
    

    EDIT: To do this open the Terminal. Then say

    #touch .bashrc
    

    You can then open .bashrc in textEdit or in TextWrangler and add the previous commands.

    0 讨论(0)
  • 2020-12-12 13:51

    Another solution is to set the colors strait in the ssh config file:

    inside ~/.ssh/config

    Host Server1
       HostName x.x.x.x
       User ubuntu
       IdentityFile ~/Desktop/keys/1.pem
       PermitLocalCommand yes
       LocalCommand osascript -e "tell application \"Terminal\" to set background color of window 1 to {27655, 0, 0, -16373}"
    
    Host Server2
       HostName x.x.x.x
       User ubuntu
       IdentityFile ~/Desktop/keys/2.pem
       PermitLocalCommand yes
       LocalCommand osascript -e "tell application \"Terminal\" to set background color of window 1 to {37655, 0, 0, -16373}"
    
    0 讨论(0)
提交回复
热议问题