Skip to content

Commit 3b074d2

Browse files
committed
feat: restart ssh tunnel
1 parent 15d5cf8 commit 3b074d2

File tree

7 files changed

+75
-2
lines changed

7 files changed

+75
-2
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+

src/aws/dependencies/reverseShell.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,17 @@ function openSSHTunnel(){
3636
createSshConfig $instanceName $instanceIp "root" "22" "~/.ssh/id_rsa" $sshtunnelPortArray
3737
autossh -f -T -N -q -4 -M $monitorPort $instanceName
3838
}
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+
}

src/aws/load.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ source $manageConfigPath/src/aws/dependencies/manageConfig.sh
1111
source $manageConfigPath/src/aws/dependencies/sshtunnelFunction.sh
1212
source $manageConfigPath/src/aws/dependencies/reverseShell.sh
1313
source $manageConfigPath/src/aws/dependencies/updateOrAppend.sh
14+
source $manageConfigPath/src/aws/dependencies/getProcessNumber.sh
1415

1516
source $manageConfigPath/src/utils/dependencies/config.sh
1617
source $manageConfigPath/src/utils/dependencies/array.sh

src/aws/restart.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,7 @@ function restart(){
3030
echo "the new ip address is $publicIp"
3131
updateIPAddress $balloonName $publicIp
3232

33+
closeSSHTunnel
34+
restartSSHTunnel
35+
echo "open ssh tunnel"
3336
}

allTest.sh renamed to tests/allTest.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
source array.sh
2-
source replace.sh
31

42
test_extractBlocks() {
53
local input_string="$1"

tests/load.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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

tests/testGetProcessNumber.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

0 commit comments

Comments
 (0)