This commit is contained in:
Luk
2026-03-07 15:11:16 +08:00
parent d75c6b7d02
commit 4cf98bc2cc
3 changed files with 39 additions and 23 deletions

View File

@@ -1,4 +1,8 @@
echo Local IP: $(echo `hostname -I` | awk '{print $1;}') if [ "$(uname)" = "Darwin" ]; then
echo Local IP: $(ifconfig en0 | grep "inet " | awk '{print $2}')
else
echo Local IP: $(echo `hostname -I` | awk '{print $1;}')
fi
echo Public IP: `curl -s ifconfig.me` echo Public IP: `curl -s ifconfig.me`
echo echo

View File

@@ -79,7 +79,7 @@ umask 022 # rwxr-xr-x
############## following settings copied from amazon's debian #################### ############## following settings copied from amazon's debian ####################
# set variable identifying the chroot you work in (used in the prompt below) # set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot) debian_chroot=$(cat /etc/debian_chroot)
fi fi
## uncomment for a colored prompt, if the terminal has the capability; turned ## uncomment for a colored prompt, if the terminal has the capability; turned
@@ -99,7 +99,7 @@ fi
# set a fancy prompt (non-color, unless we know we "want" color) # set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in case "$TERM" in
xterm-color|*-256color) color_prompt=yes;; xterm-color|*-256color) color_prompt=yes;;
esac esac
# color starts with \[\033[${Style};${Type}${Color}m\] and ends with \[\033[00m\]. # color starts with \[\033[${Style};${Type}${Color}m\] and ends with \[\033[00m\].
@@ -124,29 +124,41 @@ esac
# - `7`: White # - `7`: White
# others: # others:
# \w shows absolute path, \W shows current folder. # \w shows absolute path, \W shows current folder.
if [ "$(whoami)" = "root" ]; then
if [ "$(uname)" = "Darwin" ]; then
MYIPLAN=$(ifconfig en0 | grep "inet " | awk '{print $2}')
else
MYIPLAN=$(echo `hostname -I` | awk '{print $1;}')
fi
MYIPPUB=$(curl -s ifconfig.me)
if [ "$(uname)" = "Darwin" ]; then
PSTYLE=04
PTYPE=3
elif [ "$(whoami)" = "root" ]; then
PSTYLE=07 PSTYLE=07
PTYPE=4 PTYPE=4
AS_ROOT="#"
else else
PSTYLE=07 PSTYLE=07
PTYPE=3 PTYPE=3
if [[ -n "$SSH_CONNECTION" ]]; then
IN_SSH="$"
fi
fi fi
if [ "$(uname)" = "Darwin" ]; then if [ "$(uname)" = "Darwin" ]; then
MYOSVERSION=Mac`sw_vers -productVersion` MYOSVERSION=Mac`sw_vers -productVersion`
elif [ "$(uname)" = "FreeBSD" ]; then elif [ "$(uname)" = "FreeBSD" ]; then
MYOSVERSION=$(echo "$(uname -r)" | cut -d'-' -f1) MYOSVERSION=$(echo "$(uname -r)" | cut -d'-' -f1)
elif [ -f /etc/debian_version ]; then elif [ -f /etc/debian_version ]; then
MYOSVERSION=Deb`cat /etc/debian_version 2>/dev/null` MYOSVERSION=Deb`cat /etc/debian_version 2>/dev/null`
elif [ -f /etc/ubuntu_version ]; then elif [ -f /etc/ubuntu_version ]; then
MYOSVERSION=Ubt`cat /etc/ubuntu_version 2>/dev/null` MYOSVERSION=Ubt`cat /etc/ubuntu_version 2>/dev/null`
fi
if [[ -n "$SSH_CONNECTION" ]] || [[ "$(uname)" != "Darwin" ]]; then
IN_SSH="ssh$"
fi fi
if [ "$color_prompt" = yes ]; then if [ "$color_prompt" = yes ]; then
PS1='<\[\033[$PSTYLE;${PTYPE}2m\]\t\[\033[00m\]#\[\033[$PSTYLE;${PTYPE}5m\]\u\[\033[00m\]@\[\033[$PSTYLE;${PTYPE}1m\]\h\[\033[00m\]=\[\033[$PSTYLE;${PTYPE}4m\]$(uname -m),$(uname),$MYOSVERSION\[\033[00m\]:\[\033[$PSTYLE;${PTYPE}2m\]\w\[\033[00m\]>\[\033[$PSTYLE;${PTYPE}7m\]$IN_SSH\[\033[00m\] ' PS1='<\[\033[$PSTYLE;${PTYPE}2m\]\t\[\033[00m\]#\[\033[$PSTYLE;${PTYPE}5m\]\u\[\033[00m\]@\[\033[$PSTYLE;${PTYPE}1m\]\h\[\033[00m\]=\[\033[$PSTYLE;${PTYPE}1m\]$MYIPPUB\[\033[00m\]~\[\033[$PSTYLE;${PTYPE}4m\]$(uname -m),$(uname),$MYOSVERSION\[\033[00m\]:\[\033[$PSTYLE;${PTYPE}2m\]\w\[\033[00m\]>\[\033[$PSTYLE;${PTYPE}7m\]$IN_SSH$AS_ROOT\[\033[00m\] '
else else
PS1='<\t#\u@\h=$(uname -m),$(uname),$MYOSVERSION:\w>$IN_SSH ' # \w shows absolute path, \W shows current folder. PS1='<\t#\u@\h=$MYIPPUB~$(uname -m),$(uname),$MYOSVERSION:\w>$IN_SSH$AS_ROOT ' # \w shows absolute path, \W shows current folder.
fi fi
unset color_prompt force_color_prompt unset color_prompt force_color_prompt

20
ssh.sh
View File

@@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
echo "Usage: $(basename $0) [selected_index] [to_run_cmd]" echo "Usage: $(basename $0) [host_to_ssh] [cmd_to_run]"
# Ensure jq is installed # Ensure jq is installed
if ! command -v jq &> /dev/null; then if ! command -v jq &> /dev/null; then
@@ -38,7 +38,7 @@ done
# Check if a command-line argument is provided # Check if a command-line argument is provided
if [ "$1" != "" ]; then if [ "$1" != "" ]; then
# 如果提供了参数,则直接使用这个前缀 # 如果提供了参数,则直接使用这个前缀
user_input=$1 host_to_ssh=$1
else else
echo "Select a target to connect via SSH, or press 0 to exit:" echo "Select a target to connect via SSH, or press 0 to exit:"
echo echo
@@ -49,7 +49,7 @@ else
done done
echo echo
read -p "请输入要连接的主机前缀: " user_input read -p "请输入要连接的主机前缀: " host_to_ssh
fi fi
echo echo
@@ -57,7 +57,7 @@ echo
# 查找用户输入对应的索引 # 查找用户输入对应的索引
selected_index=-1 selected_index=-1
for i in "${!prefixes[@]}"; do for i in "${!prefixes[@]}"; do
if [[ "${prefixes[$i]}" == "$user_input" ]]; then if [[ "${prefixes[$i]}" == "$host_to_ssh" ]]; then
selected_index=$i selected_index=$i
break break
fi fi
@@ -84,13 +84,13 @@ fi
echo "::*** Connecting to ${label}" echo "::*** Connecting to ${label}"
if [ "$password" != "null" ]; then if [ "$password" != "null" ]; then
if command -v sshpass &> /dev/null; then if command -v sshpass &> /dev/null; then
echo "::*** sshpass -p $password -Y -p $port $username@$host" echo "::*** sshpass -p $password -Y -p $port $username@$host '$2'"
sshpass -p "$password" ssh -Y -p "$port" "$username@$host" $2 sshpass -p "$password" ssh -Y -p "$port" "$username@$host" "$2"
else else
echo "::*** ssh -Y -p $port $username@$host" echo "::*** ssh -Y -p $port $username@$host '$2'"
ssh -Y -p "$port" "$username@$host" $2 ssh -Y -p "$port" "$username@$host" "$2"
fi fi
else else
echo "::*** ssh -Y -p $port $username@$host" echo "::*** ssh -Y -p $port $username@$host '$2'"
ssh -Y -p "$port" "$username@$host" $2 ssh -Y -p "$port" "$username@$host" "$2"
fi fi