-
Notifications
You must be signed in to change notification settings - Fork 25
add support to show info about gcloud ssh connections #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jacostag
wants to merge
8
commits into
soyuka:master
Choose a base branch
from
jacostag:gcloud_mosh
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0228fcf
add mor options to pane_ssh_connected.sh
jacostag 07c0235
add gcloud to shared.sh
jacostag bdac2cb
add examples to README.md
jacostag afa1a29
Update port.sh for gcloud
jacostag 33cc256
Create ip.sh
jacostag 5330a00
adding mosh functionality shared.sh
jacostag e407a5a
Update README.md
jacostag f6edf39
Merge branch 'master' into gcloud_mosh
jacostag File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env bash | ||
|
||
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
|
||
source $CURRENT_DIR/shared.sh | ||
|
||
main() { | ||
get_info "ip" | ||
} | ||
|
||
main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,21 @@ | ||
#!/usr/bin/env bash | ||
|
||
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
|
||
source $CURRENT_DIR/shared.sh | ||
|
||
main() { | ||
ssh_connected && echo 1 || echo 0 | ||
|
||
if ssh_connected; then | ||
echo "SSH" | ||
elif gcloud_connected; then | ||
echo "GC" | ||
elif mosh_connected; then | ||
echo "MOSH" | ||
else | ||
echo "" | ||
fi | ||
|
||
} | ||
|
||
main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
#!/usr/bin/env bash | ||
|
||
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
|
||
source $CURRENT_DIR/shared.sh | ||
|
||
main() { | ||
get_info port | ||
|
||
if ssh_connected; then | ||
get_info "port" | ||
elif gcloud_connected; then | ||
get_info "port" | ||
elif mosh_connected; then | ||
get_info "port" | ||
fi | ||
|
||
} | ||
|
||
main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,27 @@ | ||
#!/usr/bin/env bash | ||
|
||
|
||
get_tmux_option() { | ||
local option=$1 | ||
local default_value=$2 | ||
local option_value=$(tmux show-option -gqv "$option") | ||
if [ -z "$option_value" ]; then | ||
echo "$default_value" | ||
else | ||
echo "$option_value" | ||
fi | ||
} | ||
|
||
set_tmux_option() { | ||
local option=$1 | ||
local value=$2 | ||
tmux set-option -gq "$option" "$value" | ||
} | ||
|
||
parse_ssh_port() { | ||
# If there is a port get it | ||
local port=$(echo $1 | grep -Eo '\-p\s*([0-9]+)' | sed 's/-p\s*//') | ||
|
||
get_info() { | ||
local host port user | ||
local cmd=$(__current_pane_command) | ||
|
@@ -46,6 +68,7 @@ containered() { | |
__containered_cmd "$cmd" | ||
} | ||
|
||
|
||
__ssh_cmd() { | ||
[[ $1 =~ (^|^[[:blank:]]*|/?)ssh[[:blank:]] ]] || [[ $1 =~ (^|^[[:blank:]]*|/?)sshpass[[:blank:]] ]] | ||
} | ||
|
@@ -54,6 +77,138 @@ __containered_cmd() { | |
[[ $1 =~ (^|^[[:blank:]]*|/?)docker[[:blank:]] ]] || [[ $1 =~ (^|^[[:blank:]]*|/?)podman[[:blank:]] ]] | ||
} | ||
|
||
|
||
get_ssh_user() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Plugin doesn't parse ssh/config file anymore |
||
local ssh_user=$(whoami) | ||
|
||
for ssh_config in $(awk ' | ||
$1 == "Host" { | ||
gsub("\\.", "\\.", $2); | ||
gsub("\\*", ".*", $2); | ||
host = $2; | ||
next; | ||
} | ||
$1 == "User" { | ||
$1 = ""; | ||
sub( /^[[:space:]]*/, "" ); | ||
printf "%s|%s\n", host, $0; | ||
}' .ssh/config); do | ||
local host_regex=${ssh_config%|*} | ||
local host_user=${ssh_config#*|} | ||
if [[ "$1" =~ $host_regex ]]; then | ||
ssh_user=$host_user | ||
break | ||
fi | ||
done | ||
|
||
echo $ssh_user | ||
} | ||
|
||
get_remote_ssh() { | ||
local command=$1 | ||
|
||
# First get the current pane command pid to get the full command with arguments | ||
local cmd=$({ | ||
pgrep -flaP $(tmux display-message -p "#{pane_pid}") | ||
ps -o command -p $(tmux display-message -p "#{pane_pid}") | ||
} | xargs -I{} echo {} | grep ssh | sed -E 's/^[0-9]*[[:blank:]]*ssh //') | ||
|
||
local port=$(parse_ssh_port "$cmd") | ||
|
||
local cmd=$(echo $cmd | sed 's/\-p\s*'"$port"'//g') | ||
local user=$(echo $cmd | awk '{print $NF}' | cut -f9 -d@) | ||
local host=$(echo $cmd | awk '{print $NF}' | cut -f1 -d@) | ||
|
||
if [ $user == $host ]; then | ||
local user=$(get_ssh_user $host) | ||
fi | ||
|
||
case "$1" in | ||
"whoami") | ||
echo $user | ||
;; | ||
"hostname") | ||
echo $host | ||
;; | ||
"port") | ||
echo $port | ||
;; | ||
*) | ||
echo "$user@$host:$port" | ||
;; | ||
esac | ||
} | ||
|
||
get_remote_gcloud() { | ||
local pane_pid=$(tmux display-message -p "#{pane_pid}") | ||
local parent=$(pgrep -P $pane_pid) | ||
local grandparent=$(pgrep -P $parent) # user | ||
local greatgrandparent=$(pgrep -P $grandparent) #port and host | ||
|
||
local user=$(ps -o command -p $grandparent | awk '{print $33}' | cut -f1 -d@) | ||
local host=$(ps -o command -p $greatgrandparent | awk '{print $7}') | ||
local port=$(ps -o command -p $greatgrandparent | awk '{print $8}') | ||
|
||
case "$1" in | ||
"whoami") | ||
echo $user | ||
;; | ||
"hostname") | ||
echo $host | ||
;; | ||
"port") | ||
echo $port | ||
;; | ||
*) | ||
echo "$user@$host:$port" | ||
;; | ||
esac | ||
} | ||
|
||
get_remote_mosh() { | ||
local pane_pid=$(tmux display-message -p "#{pane_pid}") | ||
local parent=$(pgrep -P $pane_pid) | ||
|
||
local user=$(ps -o command -p $parent | awk '{print $3}' | cut -f1 -d@) | ||
local host=$(ps -o command -p $parent | awk '{print $3}' | cut -f2 -d@) | ||
local port=$(ps -o command -p $parent | awk '{print $6}') | ||
local ip=$(ps -o command -p $parent | awk '{print $5}') | ||
|
||
case "$1" in | ||
"whoami") | ||
echo $user | ||
;; | ||
"hostname") | ||
echo $host | ||
;; | ||
"port") | ||
echo $port | ||
;; | ||
"ip") | ||
echo $ip | ||
;; | ||
*) | ||
echo "$user@$host:$port" | ||
;; | ||
esac | ||
} | ||
|
||
get_info() { | ||
if ssh_connected; then | ||
echo $(get_remote_ssh $1) | ||
elif mosh_connected; then | ||
echo $(get_remote_mosh $1) | ||
elif gcloud_connected; then | ||
echo $(get_remote_gcloud $1) | ||
else | ||
echo $($1) | ||
fi | ||
} | ||
|
||
ssh_connected() { | ||
local cmd=$(tmux display-message -p "#{pane_current_command}") | ||
[ $cmd = "ssh" ] || [ $cmd = "sshpass" ] | ||
======= | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Invalid conflicts resolving |
||
__current_pane_command() { | ||
local ppid=$(tmux display-message -p "#{pane_pid}") | ||
local pid command cmd | ||
|
@@ -129,3 +284,13 @@ __get_hostname() { | |
__get_hostname_short() { | ||
command -v hostname > /dev/null && hostname --short || echo "${HOSTNAME%%.*}" | ||
} | ||
|
||
gcloud_connected() { | ||
local cmd=$(tmux display-message -p "#{pane_current_command}") | ||
[ $cmd = "python" ] | ||
} | ||
|
||
mosh_connected() { | ||
local cmd=$(tmux display-message -p "#{pane_current_command}") | ||
[ $cmd = "mosh-client" ] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicated, see scripts/utils/tmux.sh