File tree Expand file tree Collapse file tree 7 files changed +75
-2
lines changed Expand file tree Collapse file tree 7 files changed +75
-2
lines changed Original file line number Diff line number Diff line change
1
+ #! /bin/bash
2
+
3
+ getProcessNumber () {
4
+ local targetString=" $1 "
5
+ local processInfo=" $2 "
6
+ local processNumber=$(
7
+ echo " $processInfo " \
8
+ | \
9
+ awk -v target=" $targetString " ' $0 ~ /\/usr\/lib\/autossh\/autossh/ && $0 ~ target {print $2}'
10
+ )
11
+
12
+ if [ -n " $processNumber " ]; then
13
+ echo " $processNumber "
14
+ else
15
+ echo " "
16
+ fi
17
+ }
18
+
19
+ getProcessInfo () {
20
+ local processInfo=$( ps aux | grep ssh)
21
+ echo " $processInfo "
22
+ }
23
+
Original file line number Diff line number Diff line change @@ -36,3 +36,17 @@ function openSSHTunnel(){
36
36
createSshConfig $instanceName $instanceIp " root" " 22" " ~/.ssh/id_rsa" $sshtunnelPortArray
37
37
autossh -f -T -N -q -4 -M $monitorPort $instanceName
38
38
}
39
+
40
+ function closeSSHTunnel(){
41
+ local instanceName=$1
42
+ local processNumber=$( getProcessNumber " $instanceName " " $( getProcessInfo) " )
43
+ if [ -n " $processNumber " ]; then
44
+ kill -9 $processNumber
45
+ fi
46
+ }
47
+
48
+ function restartSSHTunnel(){
49
+ local instanceName=$1
50
+ local instanceIp=$2
51
+ updateSshConfigInterface $instanceName HostName $instanceIp
52
+ }
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ source $manageConfigPath/src/aws/dependencies/manageConfig.sh
11
11
source $manageConfigPath /src/aws/dependencies/sshtunnelFunction.sh
12
12
source $manageConfigPath /src/aws/dependencies/reverseShell.sh
13
13
source $manageConfigPath /src/aws/dependencies/updateOrAppend.sh
14
+ source $manageConfigPath /src/aws/dependencies/getProcessNumber.sh
14
15
15
16
source $manageConfigPath /src/utils/dependencies/config.sh
16
17
source $manageConfigPath /src/utils/dependencies/array.sh
Original file line number Diff line number Diff line change @@ -30,4 +30,7 @@ function restart(){
30
30
echo " the new ip address is $publicIp "
31
31
updateIPAddress $balloonName $publicIp
32
32
33
+ closeSSHTunnel
34
+ restartSSHTunnel
35
+ echo " open ssh tunnel"
33
36
}
Original file line number Diff line number Diff line change 1
- source array.sh
2
- source replace.sh
3
1
4
2
test_extractBlocks () {
5
3
local input_string=" $1 "
Original file line number Diff line number Diff line change
1
+
2
+ manageConfigPath=$( pwd)
3
+
4
+ source $manageConfigPath /src/utils/load.sh
5
+ source $manageConfigPath /src/aws/load.sh
6
+
7
+ source $manageConfigPath /tests/testGetProcessNumber.sh
Original file line number Diff line number Diff line change
1
+ testGetProcessNumber () {
2
+ local luftballon=" luftballon"
3
+
4
+ # Test case 1: Find process number
5
+ local processInfo1=" root 1923245 0.0 0.0 2216 84 ? Ss 03:18 0:00 /usr/lib/autossh/autossh -T -N -q -4 -M 2200 luftballon"
6
+ local result1=$( getProcessNumber " $luftballon " " $processInfo1 " )
7
+ echo " Test case 1 - Process number: $result1 "
8
+
9
+ # Test case 2: No /usr/lib/autossh/autossh
10
+ local processInfo2=" root 1922602 0.0 0.1 19736 10404 ? Ss 03:16 0:00 sshd: root@pts/0"
11
+ local result2=$( getProcessNumber " $luftballon " " $processInfo2 " )
12
+ echo " Test case 2 - Process number: $result2 "
13
+
14
+ # Test case 3: No luftballon
15
+ local processInfo3=" root 1923245 0.0 0.0 2216 84 ? Ss 03:18 0:00 /usr/lib/autossh/autossh -T -N -q -4 -M 2200 not_found"
16
+ local result3=$( getProcessNumber " $luftballon " " $processInfo3 " )
17
+ echo " Test case 3 - Process number: $result3 "
18
+
19
+ # Test case 4: Multiple /usr/lib/autossh/autossh
20
+ local processInfo4=" root 1923245 0.0 0.0 2216 84 ? Ss 03:18 0:00 /usr/lib/autossh/autossh -T -N -q -4 -M 2200 luftballon
21
+ root 1923246 0.3 0.1 14476 8072 ? S 03:18 0:00 /usr/lib/autossh/autossh -T -N -q -4 -M 2200 luftballon"
22
+ local result4=$( getProcessNumber " $luftballon " " $processInfo4 " )
23
+ echo " Test case 4 - Process number: $result4 "
24
+
25
+ local result5=$( getProcessNumber " $luftballon " " $( getProcessInfo) " )
26
+ echo " Test case 5 - Process number: $result5 "
27
+ }
You can’t perform that action at this time.
0 commit comments