Browse code

bash_profile (cygwin) - v2.0 - Behavior improved

Improvements of PATH, DISPLAY setup, start_agent calling and ssh multikey management

Fred authored on11/06/2016 21:48:58
Showing1 changed files
... ...
@@ -1,14 +1,15 @@
1 1
 # Title: .bash_profile
2 2
 # Author: Frédéric CHEVALIER <f15.chevalier@gmail.com>
3
-# Version: 1.3
3
+# Version: 2.0
4 4
 # Created in: 2014-01-07
5
-# Modified in: 2014-09-25
5
+# Modified in: 2016-06-10
6 6
 
7 7
 
8 8
 #==========#
9 9
 # Versions #
10 10
 #==========#
11 11
 
12
+# v2.0 - 2016-06-10: PATH updated / DISPLAY variable setup improved to allow remote display through ssh / start_agent with multikey management / start_agent calling improved / ssh aliases updated
12 13
 # v1.3 - 2014-09-25: start_agent with limited ssh key lifetime added
13 14
 # v1.2 - 2014-08-26: CHERE_INVOKING variable added 
14 15
 # v1.1 - 2014-08-20: multiscreen remove because of .screenrc set up / Java variables added
... ...
@@ -36,6 +37,7 @@ fi
36 37
 alias medusa='ssh medusa'
37 38
 alias medusa2='ssh medusa2'
38 39
 alias mendel='ssh mendel'
40
+alias bulmer='ssh bulmer'
39 41
 
40 42
 # Shortcut for vim
41 43
 alias vi='vim'
... ...
@@ -49,36 +51,65 @@ alias wh="cd /cygdrive/c/Users/$USER/"
49 51
 # User specific environment and startup programs #
50 52
 #================================================#
51 53
 
52
-# Personalized PATH
53
-export PATH="/usr/local/bin:/usr/bin:/cygdrive/c/Program Files/Common Files/Microsoft Shared/Windows Live:/cygdrive/c/Program Files (x86)/Common Files/Microsoft Shared/Windows Live:/cygdrive/c/Windows/system32:/cygdrive/c/Windows:/cygdrive/c/Windows/System32/Wbem:/cygdrive/c/Windows/System32/WindowsPowerShell/v1.0:/cygdrive/c/Program Files (x86)/Intel/Services/IPT:/cygdrive/c/Program Files (x86)/NTRU Cryptosystems/NTRU TCG Software Stack/bin:/cygdrive/c/Program Files/NTRU Cryptosystems/NTRU TCG Software Stack/bin:/cygdrive/c/Program Files/Dell/Dell Data Protection/Access/Advanced/Wave/Gemalto/Access Client/v5:/cygdrive/c/Program Files (x86)/Windows Live/Shared:/cygdrive/c/Program Files (x86)/Common Files/Roxio Shared/DLLShared:/cygdrive/c/Program Files (x86)/Common Files/Roxio Shared/OEM/DLLShared:/cygdrive/c/Program Files (x86)/Common Files/Roxio Shared/OEM/DLLShared:/cygdrive/c/Program Files (x86)/Common Files/Roxio Shared/OEM/12.0/DLLShared:/cygdrive/c/Program Files (x86)/Roxio/OEM/AudioCore:/usr/lib/lapack:$HOME/local/bin"
54
-
55
-# Set up the display variable to get an automatique display in the X server
56
-export DISPLAY=:0.0
54
+#---------------#
55
+# Personal Path #
56
+#---------------#
57 57
 
58 58
 # Java
59
-export JAVA_HOME=/cygdrive/c/Users/fcheval/AppData/Local/PortableApps/CommonFiles/Java
59
+export JAVA_HOME=/cygdrive/c/Users/$USER/AppData/Local/PortableApps/CommonFiles/Java
60 60
 export PATH=$PATH:$JAVA_HOME/bin
61 61
 
62 62
 # Chere variable (read by /etc/profile - important to start new shell session from current directory instead of $HOME)
63 63
 export CHERE_INVOKING=1
64 64
 
65
-# SSH environment setup
65
+
66
+#----------------#
67
+# Window display #
68
+#----------------#
69
+
70
+# Set up the display variable to get an automatique display in the X server
71
+if [[ -z "$SSH_CONNECTION" ]]
72
+then
73
+    export DISPLAY=:0.0
74
+elif [[ -n "$SSH_CONNECTION" && -z "$DISPLAY" ]]
75
+then
76
+    flag="\$DISPLAY has no value"
77
+elif [[ -n "$SSH_CONNECTION" && $DISPLAY =~ ":0.0" ]]
78
+then
79
+    flag="Value of \$DISPLAY is $DISPLAY"
80
+fi
81
+
82
+if [[ -n "$flag" ]]
83
+then
84
+    msg="${flag}. You cannot display remote windows. If -Y was used, set \"X11Forwarding yes\" in the server sshd_config file (see man sshd_config)."
85
+    if [[ $(echo $PS1 | grep '\\e\[') ]]
86
+    then
87
+        echo -e "\n\e[33mWarning:\e[00m $msg\n"
88
+    else
89
+        echo "\nWarning: $msg\n"
90
+    fi
91
+fi
92
+
93
+
94
+#-----------------------#
95
+# SSH environment setup #
96
+#-----------------------#
97
+
98
+# Variables
99
+SSH_KEY_LIST="$HOME/.ssh/key_list"  # File with full path of keys (one per line) to load with start_agent
100
+[[ -f "$SSH_KEY_LIST" ]] && SSH_KEYS=($(cat $SSH_KEY_LIST))
66 101
 lifetime=259200 # seconds => 3 days
102
+
103
+# Loading keys
67 104
 if [[ -f "${SSH_ENV}" ]]
68 105
 then
69
-    source "${SSH_ENV}" > /dev/null
70
-    
71
-    # Check if ssh-agent is active
72
-    #ps ${SSH_AGENT_PID} doesn't work under cywgin
73
-    if [[ ! $(ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$) ]]
74
-    then
75
-        start_agent -t $lifetime
106
+    source "$SSH_ENV"
76 107
     # Check if the ssh key is still stored
77
-    elif [[ $(ssh-add -l &> /dev/null; echo $?) != 0 ]]
108
+    if [[ $(ssh-add -l &> /dev/null; echo $?) != 0 ]]
78 109
     then
79 110
         pkill ssh-agent
80
-        start_agent -t $lifetime
111
+        eval start_agent -t $lifetime ${SSH_KEYS[@]}
81 112
     fi
82 113
 else
83
-    start_agent -t $lifetime
114
+    eval start_agent -t $lifetime ${SSH_KEYS[@]}
84 115
 fi