ssh_session.sh
1 #!/usr/bin/env bash 2 3 # setting the locale, some users have issues with different locales, this forces the correct one 4 export LC_ALL=en_US.UTF-8 5 6 current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 7 source $current_dir/utils.sh 8 9 show_ssh_session_port=$1 10 11 parse_ssh_port() { 12 # Get port from connection 13 local port=$(echo $1|grep -Eo '\-p\s*([0-9]+)'|sed 's/-p\s*//') 14 15 if [ -z $port ]; then 16 local port=22 17 fi 18 19 echo $port 20 } 21 22 parse_ssh_config() { 23 for ssh_config in `awk ' 24 $1 == "Host" { 25 gsub("\\\\.", "\\\\.", $2); 26 gsub("\\\\*", ".*", $2); 27 host = $2; 28 next; 29 } 30 $1 == "User" { 31 $1 = ""; 32 sub( /^[[:space:]]*/, "" ); 33 printf "%s|%s\n", host, $0; 34 }' $1`; do 35 local host_regex=${ssh_config%|*} 36 local host_user=${ssh_config#*|} 37 if [ "$2" == "$host_regex" ]; then 38 ssh_user_found=$host_user 39 break 40 fi 41 done 42 43 echo $ssh_user_found 44 } 45 46 get_ssh_user() { 47 # Search SSH User in user local file if available 48 if [ -f ~/.ssh/config ]; then 49 ssh_user=$(parse_ssh_config ~/.ssh/config $1) 50 fi 51 52 # If SSH User not found, search in global config file 53 if [ -z $ssh_user ]; then 54 ssh_user=$(parse_ssh_config /etc/ssh/ssh_config $1) 55 fi 56 57 #If SSH User not found in any config file, return current user 58 if [ -z $ssh_user ]; then 59 ssh_user=$(whoami) 60 fi 61 62 echo $ssh_user 63 } 64 65 get_remote_info() { 66 local command=$1 67 68 # First get the current pane command pid to get the full command with arguments 69 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 //') 70 local port=$(parse_ssh_port "$cmd") 71 72 local cmd=$(echo $cmd|sed 's/\-p\s*'"$port"'//g') 73 local user=$(echo $cmd | awk '{print $NF}'|cut -f1 -d@) 74 local host=$(echo $cmd | awk '{print $NF}'|cut -f2 -d@) 75 76 if [ $user == $host ]; then 77 local user=$(get_ssh_user $host) 78 fi 79 80 case "$1" in 81 "whoami") 82 echo $user 83 ;; 84 "hostname") 85 echo $host 86 ;; 87 "port") 88 echo $port 89 ;; 90 *) 91 echo "$user@$host:$port" 92 ;; 93 esac 94 } 95 96 get_info() { 97 # If command is ssh get info from remote 98 if $(ssh_connected); then 99 echo $(get_remote_info $1) 100 else 101 echo $($1) 102 fi 103 } 104 105 ssh_connected() { 106 # Get current pane command 107 local cmd=$(tmux display-message -p "#{pane_current_command}") 108 109 [ $cmd = "ssh" ] || [ $cmd = "sshpass" ] 110 } 111 112 main() { 113 hostname=$(get_info hostname) 114 user=$(get_info whoami) 115 116 # Only show port info if ssh session connected (no localhost) and option enabled 117 if $(get_tmux_option "@dracula-show-ssh-only-when-connected" false) && ! $(ssh_connected); then 118 echo "" 119 elif $(ssh_connected) && [ "$show_ssh_session_port" == "true" ] ; then 120 port=$(get_info port) 121 echo $user@$hostname:$port 122 else 123 echo $user@$hostname 124 fi 125 } 126 127 main