Skip to content

Commit

Permalink
update RemoteForward argument
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroTochigi committed Apr 25, 2024
1 parent 90e9528 commit 0d63785
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
24 changes: 24 additions & 0 deletions allTest.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
source array.sh
source replace.sh

test_extractBlocks() {
local input_string="$1"
Expand All @@ -13,3 +14,26 @@ test_extractBlocks() {

test_extractBlocks "12345 localhost:22 54321 localhost:80 ..."
test_extractBlocks "67890 localhost:443 another_text 98765 localhost:8080"

test_replaceFirstOccurrence() {
local test_all="$1"
local test_first="$2"
local test_second="$3"
local expected_result="$4"

local result=$(replaceFirstOccurrence "$test_all" "$test_first" "$test_second")
if [[ "$result" == "$expected_result" ]]; then
echo "PASS: Got expected result '$result'"
else
echo "FAIL: Expected '$expected_result', but got '$result'"
fi
}

# Run test cases
echo "Running test cases..."
test_replaceFirstOccurrence "8888:80,9999:443" "8888:80" "8887:81" "8887:81,9999:443"
test_replaceFirstOccurrence "8888:80,9999:443" "9999:443" "9998:442" "8888:80,9998:442"
test_replaceFirstOccurrence "8888:80,9999:443" "7777:77" "7776:76" "8888:80,9999:443"
test_replaceFirstOccurrence "8888:80,8888:80" "8888:80" "8887:81" "8887:81,8888:80"
test_replaceFirstOccurrence "" "8888:80" "8887:81" ""
echo "Tests completed."
29 changes: 21 additions & 8 deletions callUpdate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

source update.sh
source retrieve.sh
source array.sh
source replace.sh

host="remoteserver1"
host="myserver"

ssh_config=$(extract_ssh_config_to_variables $host)

function update(){
host=$1
newKey=$2
newValue=$3
local host=$1
local newKey=$2
local newValue=$3
local currentValue
local ssh_config=$(extractSshConfigToVariables $host)

if [ $? -ne 0 ]; then
echo "Failed to execute extract_ssh_config_to_variables or the command does not exist."
echo "Failed to execute extractSshConfigToVariables or the command does not exist."
exit 1
fi

Expand Down Expand Up @@ -48,15 +51,25 @@ function update(){
;;
"TCPKeepAlive")
TCPKeepAlive=$newValue
;;
"RemoteForward")
currentValue=$newValue # Assign newValue to currentValue
if [[ -n "$4" ]]; then
newValue=$4 # Update newValue if a fourth argument is provided
fi
allArgument=$(extractBlocks "$RemoteForward")
RemoteForward=$(replaceFirstOccurrence "$allArgument" "$currentValue" "$newValue")

;;
*)
echo "Invalid key."
exit 1
;;
esac

update_ssh_config "$host" "$HostName" "$User" "$Port" "$IdentityFile" "2222:88"

update_ssh_config "$host" "$HostName" "$User" "$Port" "$IdentityFile" "$RemoteForward"

}

update $host "HostName" "newremoteserver.com"
update $host "RemoteForward" "8888:80" "8887:81"
11 changes: 11 additions & 0 deletions replace.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
replaceFirstOccurrence() {
local all="$1"
local first="$2"
local second="$3"

if [[ "$all" == *"$first"* ]]; then
all="${all/$first/$second}"
fi

echo "$all"
}
6 changes: 1 addition & 5 deletions retrieve.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
source read.sh
extract_ssh_config_to_variables() {
extractSshConfigToVariables() {
# Capture the output of get_ssh_config_values
CONFIG_OUTPUT=$(get_ssh_config_values "$1")

Expand All @@ -16,10 +16,6 @@ extract_ssh_config_to_variables() {
PORT=$(echo "$CONFIG_OUTPUT" | grep "Port " | awk '{print $NF}')
IDENTITYFILE=$(echo "$CONFIG_OUTPUT" | grep "IdentityFile " | awk '{print $NF}')
REMOTEFORWARD=$(echo "$CONFIG_OUTPUT" | grep "RemoteForward " | awk '{$1=""; print $0}' | xargs )
LOCALPORT=$(echo "$REMOTEFORWARD" | cut -d' ' -f1)
REMOTEHOSTANDPORT=$(echo "$REMOTEFORWARD" | cut -d' ' -f2)
REMOTEHOST=$(echo "$REMOTEHOSTANDPORT" | cut -d':' -f1)
REMOTEPORT=$(echo "$REMOTEHOSTANDPORT" | cut -d':' -f2)
SERVERALIVEINTERVAL=$(echo "$CONFIG_OUTPUT" | grep "ServerAliveInterval " | awk '{print $NF}')
SERVERALIVECOUNTMAX=$(echo "$CONFIG_OUTPUT" | grep "ServerAliveCountMax " | awk '{print $NF}')
EXITONFORWARDFAILURE=$(echo "$CONFIG_OUTPUT" | grep "ExitOnForwardFailure " | awk '{print $NF}')
Expand Down

0 comments on commit 0d63785

Please sign in to comment.