39 Star 222 Fork 51

chuanjiao10 / kasini3000

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
k_copyfrom_ip_port_w2l_user_expect_pwd.ps1 6.37 KB
一键复制 编辑 原始数据 按行查看 历史
chuanjiao10 提交于 2023-05-05 14:45 . 新增:kl-rs2,kl2,类似于ansible
# 建议保存编码为:bom头 + utf8
# 脚本用途是:
# 1 能自动输入密码。解决从win到linux的ps session,不支持通过参数值,指定密码登录的问题。
# 2 能自动输入密码。解决从win到linux的ps session,不支持通过参数值,指定sudo密码,需要手动输入sudo密码的问题。
# 3 脚本限制,会破坏pssession原有的传参。传值。
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代码块}
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$LiteralPath, #位于被控机
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$Destination, #位于主控机,即本机。
[Switch]$Recurse,
#缩水封装
[String[]]$Exclude, #源目录,需要使用【-path】和【*】才灵。如: -path 'd:\xxx\*'
[Alias("timeout")][byte]$超时秒 = 20,
[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 (Test-Path -LiteralPath Destination)
{
}
else
{
Write-Error "错误:找不到目标目录。退出码4"
exit 4
}
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-v5.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 -WindowStyle "Hidden" $spawn -ArgumentList " -NoProfile -noexit -Command $pwsh标题代码 " -PassThru
}
}
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
Write-Verbose '连接成功。现在开始复制文件:'
if ($sudo -eq $true)
{
进入窗口
[System.Windows.Forms.SendKeys]::SendWait("Invoke-Command -Session `$a -scriptblock {{} write-output $密码 | sudo -S stat -c '{%}a' $LiteralPath > /tmp/quanxian.txt {}} {enter}") | Out-Null
[System.Windows.Forms.SendKeys]::SendWait("Invoke-Command -Session `$a -scriptblock {{} write-output $密码 | sudo -S chmod 777 $LiteralPath {}} {enter}") | Out-Null
if ($Recurse -eq $True)
{
[System.Windows.Forms.SendKeys]::SendWait("Copy-Item -fromSession `$a -LiteralPath $LiteralPath -Destination $Destination -Force -Recurse -Exclude $Exclude {enter}") | Out-Null
}
else
{
[System.Windows.Forms.SendKeys]::SendWait("Copy-Item -fromSession `$a -LiteralPath $LiteralPath -Destination $Destination -Force -Exclude $Exclude {enter}") | Out-Null
}
[System.Windows.Forms.SendKeys]::SendWait("Invoke-Command -Session `$a -scriptblock {{} `$quanxian=get-content -LiteralPath /tmp/quanxian.txt ; write-output $密码 | sudo -S chmod `$quanxian $LiteralPath {}} {enter}") | Out-Null
}
else
{
进入窗口
if ($Recurse -eq $True)
{
[System.Windows.Forms.SendKeys]::SendWait("Copy-Item -fromSession `$a -LiteralPath $LiteralPath -Destination $Destination -Force -Recurse -Exclude $Exclude {enter}") | Out-Null
}
else
{
[System.Windows.Forms.SendKeys]::SendWait("Copy-Item -fromSession `$a -LiteralPath $LiteralPath -Destination $Destination -Force -Exclude $Exclude {enter}") | Out-Null
}
}
Write-Verbose '复制文件完成,即将断开连接。'
if ($ShowWindow -eq $True)
{
Start-Sleep -Seconds 20
}
进入窗口
[System.Windows.Forms.SendKeys]::SendWait("exit{enter}") | Out-Null
sleep20
if ($script:__powershell)
{
Stop-Process -Id $script:__powershell.id
}
Get-Process pwsh | Where-Object commandline -Match $script:__pwsh标题 | Stop-Process
exit 0
PowerShell
1
https://gitee.com/chuanjiao10/kasini3000.git
git@gitee.com:chuanjiao10/kasini3000.git
chuanjiao10
kasini3000
kasini3000
master

搜索帮助