1 Star 0 Fork 52

yimiaoxiehou / kasini3000

forked from chuanjiao10 / kasini3000 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
k_run_ip_port_w2l_user_expect_pwd.ps1 10.06 KB
一键复制 编辑 原始数据 按行查看 历史
# 建议保存编码为:bom头 + utf8
# 脚本用途是:
# 1 能自动输入密码。解决从win到linux的ps session,不支持通过参数值,指定密码登录的问题。
# 2 能自动输入密码。解决从win到linux的ps session,不支持通过参数值,指定sudo密码,需要手动输入sudo密码的问题。
# 3 支持pssession原有的传参。传值。
# 4 单个被控机,不能并发运行。不支持powershell v5.x
# 用法:
# $a = '/root/a.txt'
# [scriptblock]$b = { touch $args[0] }
# c:\ProgramData\kasini3000\k_run_ip_port_w2l_user_expect_pwd.ps1 -sudo `
# -目的ip地址 '192.168.1.2' -用户名 'xxx' -密码 'xxx' `
# -脚本文件名 'd:\c.ps1' 或 -powershell代码块 $b
# -全部传入参数 $a
# 反馈问题:卡死你3000官方技术支持群。qq群:700816263
param
(
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[Alias("ipaddress")][String]$目的ip地址,
[Alias("port")][uint16]$端口 = 22,
[ValidateNotNullOrEmpty()]
[Alias("user")][String]$用户名 = 'root',
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[Alias("pwd")][String]$密码,
[switch]$sudo ,
#设置成$true 后,登录后,会发送
# "$密码" | sudo -S pwsh -f x.ps1
#或 "$密码" | sudo -S pwsh -c {powershell代码块}
[Alias("file")][String]$ps1脚本文件名 = '',
[parameter(Position = 0)][Alias("scriptblock")][scriptblock]$powershell代码块 = { },
[Alias("allparameter")]$全部传入参数,
[Alias("timeout")][byte]$超时秒 = 10,
[String]$spawn = 'C:\Program Files\PowerShell\7-preview\pwsh.exe',
# 目标swawn,不知支持powershell v5.1
# 支持powershell v6.x
# 支持powershell v7.x
[switch]$ShowWindow #显示窗口,用于debug。
)
function sleep5
{
Start-Sleep -Milliseconds 500
}
function sleep20
{
Start-Sleep -Seconds 1
}
function 获取句柄
{
foreach ($i in 1..8)
{
$script:__hwnd = $FindWindow::windsget($script:__pwsh标题)
if ( ($script:__hwnd -eq 0) -or ($script:__hwnd -eq $null) -or ($script:__hwnd -eq '') )
{
sleep5
Write-Error '获取句柄错误'
}
else
{
return
}
}
Write-Error '错误:获取spawn句柄失败。退出码11'
exit 11
}
function 进入窗口
{
$超时 = (Get-Date).AddSeconds($超时秒)
do
{
sleep5
if ($FindWindow::SetForegroundWindow($script:__hwnd) -eq $true)
{
return
}
Write-Error '进入窗口错误'
}
while ((Get-Date) -lt $超时 )
Write-Error '错误:进入spawn进程超时。退出码12'
exit 12
}
if ($IsLinux -eq $True)
{
Write-Error '错误:这个脚本不支持linux,linux下应使用expect。退出码1'
exit 1
}
if ($PSVersionTable.psversion.major -lt 6)
{
Write-Error '错误:这个脚本需要powershell v7,及以上。退出码3'
exit 3
}
if ( ($ps1脚本文件名 -eq $null) -or ($ps1脚本文件名 -eq '') )
{
if ( ($powershell代码块.Ast.Extent.Text -eq '{ }') -or ($powershell代码块.Ast.Extent.Text -eq '{}') )
{
Write-Error "错误:没有输入脚本文件名,同时也没有输入代码块"
exit 15
}
}
else
{
if ( ($powershell代码块.Ast.Extent.Text -eq '{ }') -or ($powershell代码块.Ast.Extent.Text -eq '{}') )
{
}
else
{
Write-Error "错误:有输入脚本文件名,同时有输入代码块"
exit 16
}
if (Test-Path -LiteralPath $ps1脚本文件名)
{
}
else
{
Write-Error "找不到脚本文件"
exit 17
}
}
if (Test-Path -LiteralPath $spawn)
{
}
else
{
[String]$spawn = 'C:\Program Files\PowerShell\7\pwsh.exe'
if (Test-Path -LiteralPath $spawn)
{
}
else
{
Write-Error '错误:找不到pwsh7。错误码2'
exit 2
}
}
$端口通了 = & 'c:\ProgramData\kasini3000\tcp--ping-v2.ps1' -MyComputerName $目的ip地址 -port $端口 -Quiet
if ($端口通了 -eq $true)
{
}
else
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:被控机端口不通。错误码24"
}
else
{
Write-Error "error:node port cloesd.exit 24"
}
exit 24
}
#main
$FindWindow = Add-Type -MemberDefinition @"
[DllImport("user32.dll")]public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll")]public static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]public static extern int SendMessage(IntPtr hWnd,int Msg,int wParam,int lParam);
public static IntPtr windsget(string name) {
return FindWindow(null,name);
}
"@ -PassThru -Name MyFindWindow
[System.Reflection.Assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
$script:__pwsh标题 = [GUID]::NewGuid()
$pwsh标题代码 = "[System.Console]::Title = '$script:__pwsh标题' "
if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
{
if ($ShowWindow -eq $True)
{
${script:__powershell} = Start-Process $spawn -ArgumentList " -NoProfile -noexit -Command $pwsh标题代码 " -PassThru
}
else
{
${script:__powershell} = Start-Process $spawn -ArgumentList " -WindowStyle Hidden -NoProfile -noexit -Command $pwsh标题代码 " -PassThru
}
Write-Verbose ${script:__powershell}.id
}
sleep20
获取句柄
进入窗口
[System.Windows.Forms.SendKeys]::SendWait(" `$a = New-PSSession -HostName $目的ip地址 -Port $端口 -UserName $用户名 {enter}") | Out-Null
进入窗口
[System.Windows.Forms.SendKeys]::SendWait("$密码{enter}") | Out-Null
#main2
if ($sudo -eq $true)
{
进入窗口
if (($ps1脚本文件名 -eq '') -or ($ps1脚本文件名 -eq $null))
{
$Private:temp902 = "${env:temp}\k_run_expect_sudo_scriptblock_onmaster.ps1"
if (Test-Path -LiteralPath $Private:temp902)
{
Remove-Item -LiteralPath $Private:temp902 -Force
}
[string]$Private:temp903 = @"
param
(
`${全部传入参数} = '$全部传入参数'
)
[String]`$密码 = '$密码'
`$null = `$密码 | sudo -S whoami
if (`$LastExitCode -eq 0)
{
}
else
{
if (Test-Path -LiteralPath '/etc/kasini3000/get_Language.ps1')
{
& '/etc/kasini3000/get_Language.ps1'
}
if (`$env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:sudo权限获取失败。账户不在sudo,admin,adm,wheel组。返回码1001"
}
else
{
Write-Error "error:Failed to cache sudo password,exit 1001"
}
exit 1001
}
`$写入被控机代码 = @'
`${全部传入参数} = Import-Clixml -LiteralPath '/tmp/k_run_expect_sudo_scriptblock_param.xml'
[scriptblock]`$代码 =
{
$powershell代码块
}
& `$代码 `${全部传入参数}
'@
Set-Content -Encoding utf8BOM -LiteralPath '/tmp/k_run_expect_sudo_scriptblock_onnode.ps1' -Value `$写入被控机代码
`${全部传入参数} | Export-Clixml -LiteralPath '/tmp/k_run_expect_sudo_scriptblock_param.xml'
sudo pwsh -file '/tmp/k_run_expect_sudo_scriptblock_onnode.ps1'
"@
Set-Content -Encoding unicode -LiteralPath $Private:temp902 -Value $Private:temp903
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait("Invoke-Command -Session `$a -filepath $Private:temp902 {enter}") | Out-Null
}
else
{
$Private:temp905 = "${env:temp}\k_run_expect_sudo_file_onmaster.ps1"
if (Test-Path -LiteralPath $Private:temp905)
{
Remove-Item -LiteralPath $Private:temp905 -Force
}
$Private:temp911 = $(Get-Content -LiteralPath $ps1脚本文件名) -join "`n"
[string]$Private:temp906 = @"
param
(
`${全部传入参数} = '$全部传入参数'
)
[String]`$密码 = '$密码'
`$null = `$密码 | sudo -S whoami
if (`$LastExitCode -eq 0)
{
}
else
{
if (Test-Path -LiteralPath '/etc/kasini3000/get_Language.ps1')
{
& '/etc/kasini3000/get_Language.ps1'
}
if (`$env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:sudo权限获取失败。账户不在sudo,admin,adm,wheel组。返回码1001"
}
else
{
Write-Error "error:Failed to cache sudo password,exit 1001"
}
exit 1001
}
`$写入被控机代码 = @'
`${全部传入参数} = Import-Clixml -LiteralPath '/tmp/k_run_expect_sudo_file_param.xml'
& '/tmp/k_run_expect_sudo_file_onnode2.ps1' `${全部传入参数}
'@
Set-Content -Encoding utf8BOM -LiteralPath '/tmp/k_run_expect_sudo_file_onnode.ps1' -Value `$写入被控机代码
`$Private:temp912 = @'
$Private:temp911
'@
Set-Content -Encoding utf8BOM -LiteralPath '/tmp/k_run_expect_sudo_file_onnode2.ps1' -Value `$Private:temp912
`${全部传入参数} | Export-Clixml -LiteralPath '/tmp/k_run_expect_sudo_file_param.xml'
sudo pwsh -file '/tmp/k_run_expect_sudo_file_onnode.ps1'
"@
Set-Content -Encoding unicode -LiteralPath $Private:temp905 -Value $Private:temp906
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait("Invoke-Command -Session `$a -filepath $Private:temp905 {enter}") | Out-Null
}
}
else
{
进入窗口
if (($ps1脚本文件名 -eq '') -or ($ps1脚本文件名 -eq $null))
{
$Private:temp901 = "${env:temp}\k_run_expect_scriptblock_onmaster.ps1"
if (Test-Path -LiteralPath $Private:temp901)
{
Remove-Item -LiteralPath $Private:temp901 -Force
}
Set-Content -Encoding unicode -LiteralPath $Private:temp901 -Value $powershell代码块
Start-Sleep -Seconds 2
if ($全部传入参数)
{
[System.Windows.Forms.SendKeys]::SendWait("Invoke-Command -Session `$a -filepath $Private:temp901 -ArgumentList $全部传入参数 {enter}") | Out-Null
}
else
{
[System.Windows.Forms.SendKeys]::SendWait("Invoke-Command -Session `$a -filepath $Private:temp901 {enter}") | Out-Null
}
}
else
{
if ($全部传入参数)
{
[System.Windows.Forms.SendKeys]::SendWait("Invoke-Command -Session `$a -filepath $ps1脚本文件名 -ArgumentList $全部传入参数 {enter}") | Out-Null
}
else
{
[System.Windows.Forms.SendKeys]::SendWait("Invoke-Command -Session `$a -filepath $ps1脚本文件名 {enter}") | Out-Null
}
}
}
if ($ShowWindow -eq $True)
{
Start-Sleep -Seconds 20
}
sleep5
进入窗口
[System.Windows.Forms.SendKeys]::SendWait(";exit{enter}") | Out-Null
sleep20
if ( (Get-Process -ErrorAction SilentlyContinue -Id ${script:__powershell}.id ) -eq $null)
{
}
else
{
Stop-Process -Id ${script:__powershell}.id
}
Get-Process pwsh | Where-Object commandline -Match $script:__pwsh标题 | Stop-Process
exit 0
PowerShell
1
https://gitee.com/yimiaoCC/kasini3000.git
git@gitee.com:yimiaoCC/kasini3000.git
yimiaoCC
kasini3000
kasini3000
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891