Skip to content

Commit

Permalink
feat: windows openssh 修复 (closed TencentBlueKing#2353)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyyalt committed Jul 30, 2024
1 parent 7cd281c commit 33232ac
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions apps/backend/components/collections/agent_new/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ def convert_shell_to_powershell(cls, shell_cmd):
# Convert mkdir -p xxx to if not exist xxx mkdir xxx
shell_cmd = re.sub(
r"mkdir -p\s+(\S+)",
r"powershell -c 'if (-Not (Test-Path -Path \1)) { New-Item -ItemType Directory -Path \1 }'",
r"if (-Not (Test-Path -Path \1)) { New-Item -ItemType Directory -Path \1 }",
shell_cmd,
)

Expand All @@ -730,15 +730,15 @@ def convert_shell_to_powershell(cls, shell_cmd):
# Convert curl to Invoke-WebRequest
# shell_cmd = re.sub(
# r"curl\s+(http[s]?:\/\/[^\s]+)\s+-o\s+(\/?[^\s]+)\s+--connect-timeout\s+(\d+)\s+-sSfg",
# r"powershell -c 'Invoke-WebRequest -Uri \1 -OutFile \2 -TimeoutSec \3 -UseBasicParsing'",
# r"Invoke-WebRequest -Uri \1 -OutFile \2 -TimeoutSec \3 -UseBasicParsing",
# shell_cmd,
# )
shell_cmd = re.sub(r"(curl\s+\S+\s+-o\s+\S+\s+--connect-timeout\s+\d+\s+-sSfg)", r'cmd /c "\1"', shell_cmd)

# Convert nohup xxx &> ... & to xxx (ignore nohup, output redirection and background execution)
shell_cmd = re.sub(
r"nohup\s+([^&>]+)(\s*&>\s*.*?&)?",
r"powershell -c 'Invoke-Command -Session (New-PSSession) -ScriptBlock { \1 } -AsJob'",
r"Invoke-Command -Session (New-PSSession) -ScriptBlock { \1 } -AsJob",
shell_cmd,
)

Expand Down
2 changes: 1 addition & 1 deletion apps/backend/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,4 @@ def needs_batch_request(self) -> bool:
DEFAULT_ALIVE_TIME = 30
DEFAULT_CLEAN_RECORD_LIMIT = 5000

POWERSHELL_SERVICE_CHECK_SSHD = "powershell -c Get-Service -Name sshd"
POWERSHELL_SERVICE_CHECK_SSHD = "Get-Service -Name sshd"
8 changes: 4 additions & 4 deletions script_tools/setup_pagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def logging(

JOB_PRIVATE_KEY_RE = re.compile(r"^(-{5}BEGIN .*? PRIVATE KEY-{5})(.*?)(-{5}END .*? PRIVATE KEY-{5}.?)$")

POWERSHELL_SERVICE_CHECK_SSHD = "powershell -c Get-Service -Name sshd"
POWERSHELL_SERVICE_CHECK_SSHD = "Get-Service -Name sshd"


def is_ip(ip: str, _version: Optional[int] = None) -> bool:
Expand Down Expand Up @@ -318,7 +318,7 @@ def convert_shell_to_powershell(shell_cmd):
# Convert mkdir -p xxx to if not exist xxx mkdir xxx
shell_cmd = re.sub(
r"mkdir -p\s+(\S+)",
r"powershell -c 'if (-Not (Test-Path -Path \1)) { New-Item -ItemType Directory -Path \1 }'",
r"if (-Not (Test-Path -Path \1)) { New-Item -ItemType Directory -Path \1 }",
shell_cmd,
)

Expand All @@ -328,15 +328,15 @@ def convert_shell_to_powershell(shell_cmd):
# Convert curl to Invoke-WebRequest
# shell_cmd = re.sub(
# r"curl\s+(http[s]?:\/\/[^\s]+)\s+-o\s+(\/?[^\s]+)\s+--connect-timeout\s+(\d+)\s+-sSfg",
# r"powershell -c 'Invoke-WebRequest -Uri \1 -OutFile \2 -TimeoutSec \3 -UseBasicParsing'",
# r"Invoke-WebRequest -Uri \1 -OutFile \2 -TimeoutSec \3 -UseBasicParsing",
# shell_cmd,
# )
shell_cmd = re.sub(r"(curl\s+\S+\s+-o\s+\S+\s+--connect-timeout\s+\d+\s+-sSfg)", r'cmd /c "\1"', shell_cmd)

# Convert nohup xxx &> ... & to xxx (ignore nohup, output redirection and background execution)
shell_cmd = re.sub(
r"nohup\s+([^&>]+)(\s*&>\s*.*?&)?",
r"powershell -c 'Invoke-Command -Session (New-PSSession) -ScriptBlock { \1 } -AsJob'",
r"Invoke-Command -Session (New-PSSession) -ScriptBlock { \1 } -AsJob",
shell_cmd,
)

Expand Down

0 comments on commit 33232ac

Please sign in to comment.