From 4cf98bc2cc3f7ce90449cd0ab033ff4fc4ff8ae0 Mon Sep 17 00:00:00 2001 From: Luk Date: Sat, 7 Mar 2026 15:11:16 +0800 Subject: [PATCH] u --- myip.sh | 6 +++++- nixhome/.bashrc | 36 ++++++++++++++++++++++++------------ ssh.sh | 20 ++++++++++---------- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/myip.sh b/myip.sh index a5c73fe..66a74fb 100755 --- a/myip.sh +++ b/myip.sh @@ -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 diff --git a/nixhome/.bashrc b/nixhome/.bashrc index df31de7..2e02f3d 100644 --- a/nixhome/.bashrc +++ b/nixhome/.bashrc @@ -79,7 +79,7 @@ umask 022 # rwxr-xr-x ############## following settings copied from amazon's debian #################### # set variable identifying the chroot you work in (used in the prompt below) if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then - debian_chroot=$(cat /etc/debian_chroot) + debian_chroot=$(cat /etc/debian_chroot) fi ## 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) case "$TERM" in - xterm-color|*-256color) color_prompt=yes;; + xterm-color|*-256color) color_prompt=yes;; esac # color starts with \[\033[${Style};${Type}${Color}m\] and ends with \[\033[00m\]. @@ -124,29 +124,41 @@ esac # - `7`: White # others: # \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 PTYPE=4 + AS_ROOT="#" else PSTYLE=07 PTYPE=3 + if [[ -n "$SSH_CONNECTION" ]]; then + IN_SSH="$" + fi fi if [ "$(uname)" = "Darwin" ]; then - MYOSVERSION=Mac`sw_vers -productVersion` + MYOSVERSION=Mac`sw_vers -productVersion` elif [ "$(uname)" = "FreeBSD" ]; then - MYOSVERSION=$(echo "$(uname -r)" | cut -d'-' -f1) + MYOSVERSION=$(echo "$(uname -r)" | cut -d'-' -f1) 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 - MYOSVERSION=Ubt`cat /etc/ubuntu_version 2>/dev/null` -fi -if [[ -n "$SSH_CONNECTION" ]] || [[ "$(uname)" != "Darwin" ]]; then - IN_SSH="ssh$" + MYOSVERSION=Ubt`cat /etc/ubuntu_version 2>/dev/null` fi 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 - 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 unset color_prompt force_color_prompt diff --git a/ssh.sh b/ssh.sh index 7f3f487..8a0288c 100755 --- a/ssh.sh +++ b/ssh.sh @@ -1,6 +1,6 @@ #!/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 if ! command -v jq &> /dev/null; then @@ -38,7 +38,7 @@ done # Check if a command-line argument is provided if [ "$1" != "" ]; then # 如果提供了参数,则直接使用这个前缀 - user_input=$1 + host_to_ssh=$1 else echo "Select a target to connect via SSH, or press 0 to exit:" echo @@ -49,7 +49,7 @@ else done echo - read -p "请输入要连接的主机前缀: " user_input + read -p "请输入要连接的主机前缀: " host_to_ssh fi echo @@ -57,7 +57,7 @@ echo # 查找用户输入对应的索引 selected_index=-1 for i in "${!prefixes[@]}"; do - if [[ "${prefixes[$i]}" == "$user_input" ]]; then + if [[ "${prefixes[$i]}" == "$host_to_ssh" ]]; then selected_index=$i break fi @@ -84,13 +84,13 @@ fi echo "::*** Connecting to ${label}" if [ "$password" != "null" ]; then if command -v sshpass &> /dev/null; then - echo "::*** sshpass -p $password -Y -p $port $username@$host" - sshpass -p "$password" ssh -Y -p "$port" "$username@$host" $2 + echo "::*** sshpass -p $password -Y -p $port $username@$host '$2'" + sshpass -p "$password" ssh -Y -p "$port" "$username@$host" "$2" else - echo "::*** ssh -Y -p $port $username@$host" - ssh -Y -p "$port" "$username@$host" $2 + echo "::*** ssh -Y -p $port $username@$host '$2'" + ssh -Y -p "$port" "$username@$host" "$2" fi else - echo "::*** ssh -Y -p $port $username@$host" - ssh -Y -p "$port" "$username@$host" $2 + echo "::*** ssh -Y -p $port $username@$host '$2'" + ssh -Y -p "$port" "$username@$host" "$2" fi \ No newline at end of file