Skip to content

Add Optional Tags to Change Behavior for Specific Host #24

@mwpardue

Description

@mwpardue

I'm looking for a way to add optional tags to a host and have sshto act on those tags.

Specifically, in my environment I have certain hosts that are live and hosts used for testing, as most people probably do. One of the fast ways I differentiate this is by making the background color of the production hosts different from non-production hosts, and I use a kitty browser kitten to do this.

What I'm now trying to do is add a tag to the end of a host description in an sshconfig file, say Host prod_serv #Production Server#PROD and then have sshto run a different ssh command when it sees that PROD tag. Where I'm running into trouble is I can't quite figure out how to make sshto see the tag or created variable when go_to_target() is called. Below is what I have so far, and any suggestions or advice you can provide is greatly appreciated. I couldn't find a way to highlight the specific lines I added, so the edited portions in the second code block are preceded with four underscores (____). Please be forewarned I'm no programmer so it would not surprise me to learn I'm approaching this all wrong.

Edit: I suppose I should have included what behavior I'm seeing now. It does not identify the $prod variable in the go_to_target function and so always executes the else statement. In the past I've just put all production servers in the same group and used the $group_name in the if/else statement, but I'd like to get away from having to keep unrelated production servers in the same group.

#------------------------{ SSH to target server }-----------------------------------------------------------------------
go_to_target(){ clear; 
  if [[ $prod == *"PROD"* ]]; then
   kitty +kitten ssh $SSH_OPT $target || pause;
  else
   ssh $SSH_OPT $target || pause;
  fi
}

.
.
.
.

#-------------{ Create the list of hosts. Get hosts and descriptions from ~/.ssh/config* }------------------------------
desclength=20
declare    -A      hostnames
____declare    -A      hostnames_prod
while read -r name hostname desc; do
    case    ${name,,} in
        'group_name') name="{ $desc }"
                      name_length=${#name}
                      name_left=$[(40-name_length)/2]
                      name_right=$[40-(name_left+name_length)]
                      printf -v tmp "%${name_left}s_NAME_%${name_right}s"
                      tmp=${tmp// /-}  name=${tmp//_NAME_/$name}
                      content+=( "$desc" );  desc='_LINE_';;
                '#'*) continue;;
    esac
    ((${#desc}>desclength)) && desclength=${#desc}
    hostnames["$name"]=$hostname #Create host-hostname pairs in hostnames array
    ____hostnames_prod["$name"]=$prod
    fullist+=("$name" "$desc")   #Add Host and Description to the list
done < <(gawk '
BEGIN{IGNORECASE=1}
/Host /{
    strt=1
    host=$2
    desc=gensub(/^.*Host .* #(.*)/, "\\1", "g", $0)
    ____prod=gensub(/(.*)(#PROD$)/, "PROD", "g", desc)
    desc=gensub(/(.*)#.*/,          "\\1", "g", desc)
    # desc=gensub(/(.*)#PROD$/, "\\1", "g", desc)
    next
}
strt && host == "dummy"{
    hostname=$2
    print "group_name", "dummy", desc
    strt=0
}
strt && /HostName /{
    hostname=$2
    print host, hostname, prod, desc
    strt=0
}'  $CONFILES)

descline=$(line - $desclength)
list=( "${fullist[@]}" )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions