Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  - kill ssh tunnel process
  - connect ssh before starting the ssh tunnel
  • Loading branch information
hiroTochigi committed Jun 29, 2024
1 parent 7bae499 commit 872c422
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/aws/dependencies/getProcessNumber.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ getProcessNumber() {
fi
}

getProcessNumberSsh() {
local targetString="$1"
local processInfo="$2"

local processNumber=$(echo "$processInfo" | awk -v target="$targetString" '$0 ~ /\/usr\/bin\/ssh/ && $0 ~ target {print $2}')

if [ -n "$processNumber" ]; then
echo "$processNumber"
else
echo ""
fi
}

getProcessInfo() {
local processInfo=$(ps aux | grep ssh)
echo "$processInfo"
Expand Down
22 changes: 19 additions & 3 deletions src/aws/dependencies/reverseShell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,34 @@ function openSSHTunnel(){
autossh -f -T -N -q -4 -M $monitorPort $instanceName
}

killProcess() {
local processNumber="$1"

if [ -n "$processNumber" ]; then
kill -9 "$processNumber"
echo "Killed process $processNumber"
else
echo "No process to kill"
fi
}

function closeSSHTunnel(){
local instanceName=$1
local processNumber=$(getProcessNumber "$instanceName" "$(getProcessInfo)")
if [ -n "$processNumber" ]; then
kill -9 $processNumber
fi
killProcess $processNumber
processNumber=$(getProcessNumber "$instanceName" "$(getProcessNumberSsh)")
killProcess $processNumber
}

function restartSSHTunnel(){
local instanceName=$1
local instanceIp=$2
local monitorPort=2200

ssh -i /root/.ssh/$sshkey root@$instanceIp 'echo hello world'
sleep 2

updateSshConfigInterface $instanceName HostName $instanceIp
sleep 2
autossh -f -T -N -q -4 -M $monitorPort $instanceName
}
5 changes: 4 additions & 1 deletion src/aws/restart.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ function restart(){
updateIPAddress $balloonName $publicIp

closeSSHTunnel
echo "remove old ssh tunnel settings"
sleep 5

restartSSHTunnel $balloonName $publicIp
echo "open ssh tunnel"
echo "open new ssh tunnel"
}
28 changes: 27 additions & 1 deletion tests/testGetProcessNumber.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,36 @@ testGetProcessNumber() {

# Test case 4: Multiple /usr/lib/autossh/autossh
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
root 1923246 0.3 0.1 14476 8072 ? S 03:18 0:00 /usr/lib/autossh/autossh -T -N -q -4 -M 2200 luftballon"
root 1923246 0.3 0.1 14476 8072 ? S 03:18 0:00 /usr/lib/autossh/autossh -T -N -q -4 -M 2200 myballon"
local result4=$(getProcessNumber "$luftballon" "$processInfo4")
echo "Test case 4 - Process number: $result4"

local result5=$(getProcessNumber "$luftballon" "$(getProcessInfo)")
echo "Test case 5 - Process number: $result5"
}

testGetProcessNumberSsh() {
local luftballon="luftballon"

local processInfo1="root 1923245 0.0 0.0 2216 84 ? Ss 03:18 0:00 /usr/bin/ssh -L 2200:127.0.0.1:2200 -R 2200:127.0.0.1:2201 -T -N -q -4 luftballon"
local result1=$(getProcessNumberSsh "$luftballon" "$processInfo1")
echo "Test case 1 - Process number: $result1"

local processInfo2="root 1922602 0.0 0.1 19736 10404 ? Ss 03:16 0:00 sshd: root@pts/0"
local result2=$(getProcessNumberSsh "$luftballon" "$processInfo2")
echo "Test case 2 - Process number: $result2"

local processInfo3="root 1923245 0.0 0.0 2216 84 ? Ss 03:18 0:00 /usr/bin/ssh -L 2200:127.0.0.1:2200 -R 2200:127.0.0.1:2201 -T -N -q -4 not_found"
local result3=$(getProcessNumberSsh "$luftballon" "$processInfo3")
echo "Test case 3 - Process number: $result3"

local processInfo4="root 1923245 0.0 0.0 2216 84 ? Ss 03:18 0:00 /usr/bin/ssh -L 2200:127.0.0.1:2200 -R 2200:127.0.0.1:2201 -T -N -q -4 luftballon
root 1923246 0.3 0.1 14476 8072 ? S 03:18 0:00 /usr/bin/ssh -L 2200:127.0.0.1:2200 -R 2200:127.0.0.1:2201 -T -N -q -4 myballon"
local result4=$(getProcessNumberSsh "$luftballon" "$processInfo4")
echo "Test case 4 - Process number: $result4"

local processInfo5="root 1923245 0.0 0.0 2216 84 ? Ss 03:18 0:00 /usr/bin/ssh -L 2200:127.0.0.1:2200 -R 2200:127.0.0.1:2201 -T -N -q -4 not_found
root 1923246 0.3 0.1 14476 8072 ? S 03:18 0:00 /usr/bin/ssh -L 2200:127.0.0.1:2200 -R 2200:127.0.0.1:2201 -T -N -q -4 another_string"
local result5=$(getProcessNumberSsh "$luftballon" "$processInfo5")
echo "Test case 5 - Process number: $result5"
}

0 comments on commit 872c422

Please sign in to comment.