1 Star 0 Fork 51

谯夫 / project_14055964

forked from chuanjiao10 / kasini3000 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
k_copyfrom_ip.ps1 15.69 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
#建议保存编码为:bom头 + utf8
param
(
[Alias("ipaddress")][String]$目的ip地址 = $global:当前被控机_ip.ip,
[Alias("port")][uint16]$端口 = $global:当前被控机_ip.端口,
[String]$LiteralPath,
[String]$Path,
[ValidateNotNullOrEmpty()][String]$Destination,
[Switch]$Recurse
#缩水封装
)
if (($LiteralPath -eq '') -and ($Path -eq ''))
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:源文件,源目录为空。退出码6"
}
else
{
Write-Error "error:Source file, or source directory is empty .exit 6"
}
exit 6
}
if (($LiteralPath -ne '') -and ($Path -ne ''))
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:-LiteralPath,-Path,只能输入其一。退出码7"
}
else
{
Write-Error "error:-LiteralPath,-Path,Only one of them can be entered .exit 7"
}
exit 7
}
if (Test-Path -LiteralPath $Destination)
{
}
else
{
Write-Error "错误:找不到目标目录。退出码4"
exit 4
}
if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
{
& 'c:\ProgramData\kasini3000\0k_source.ps1'
}
if ($IsLinux -eq $True)
{
& '/etc/kasini3000/0k_source.ps1'
}
Write-Verbose '从被控机到主控机,复制文件开始'
if ($global:当前被控机_ip -eq $null)
{
Write-Error "错误:当前被控机 ${global:当前被控机_ip} 为空"
exit 1
}
if ($global:当前被控机_ip.length -gt 1)
{
Write-Error "错误:当前被控机 ${global:当前被控机_ip} 数量太多"
exit 2
}
$win = 'win7','win8','win10','win2008r2','win2012r2','win2016','win2019'
$linux = & "${PSScriptRoot}/linux_distribution_str.ps1"
if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
{
if ( $global:当前被控机_ip.被控机os类型 -in $win)
{
if ( ($端口 -eq '') -or ($端口 -eq $null) )
{
$端口 = 5985
}
try
{
Test-WSMan -ComputerName $目的ip地址 -Port $端口 -ErrorAction stop
}
catch
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:目的ip地址【${目的ip地址}】端口不通。错误码22"
}
else
{
Write-Error "error:node port closed :【${目的ip地址}】.exit 22"
}
exit 22
}
finally
{
}
& 'zd只读nodelist文件.ps1'
$当前被控机 = $global:所有被控机 | Where-Object { $_.ip -eq $目的ip地址 }
if ($当前被控机.ip -ne $目的ip地址)
{
Write-Error "错误:当前被控机获取失败 ${当前被控机}"
exit 23
}
$PSRemoting服务器用户名 = $当前被控机.用户名
$用户名2 = "${目的ip地址}\${PSRemoting服务器用户名}"
$密码明文 = $当前被控机.当前密码
$密码密文 = ConvertTo-SecureString $密码明文 -AsPlainText -Force
$用户名和密码捆绑后的授权信息 = New-Object System.Management.Automation.PSCredential ($用户名2,$密码密文)
Write-Verbose '使用密码,连接w2w5985开始'
$private:连接3 = New-PSSession -ComputerName $目的ip地址 -Port $端口 -Credential $用户名和密码捆绑后的授权信息
if ($private:连接3 -eq $null)
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:使用密码,在${目的ip地址}上连接w2w5985失败。退出码21"
}
else
{
Write-Error "error:Connection failed: ${被控机ip地址} .exit 21"
}
exit 21
}
Write-Verbose '连接成功。现在开始测试LiteralPath,或Path,目录存在否:'
if ($LiteralPath -eq '')
{
$Path存在否 = Invoke-Command -Session $private:连接3 -ScriptBlock { Test-Path $args[0] } -ArgumentList $path
}
else
{
$Path存在否 = Invoke-Command -Session $private:连接3 -ScriptBlock { Test-Path $args[0] } -ArgumentList $LiteralPath
}
if ($Path存在否 -eq $true)
{
}
else
{
Remove-PSSession -Session $private:连接3
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:找不到源目录,或源文件。错误码5"
}
else
{
Write-Error "error:The source directory or file could not be found.exit 5"
}
exit 5
}
Write-Verbose '连接成功。现在开始复制文件:'
if ($LiteralPath -eq '')
{
if ($Recurse -eq $True)
{
Copy-Item -FromSession $private:连接3 -Path $Path -Destination $Destination -Force -Recurse
}
else
{
Copy-Item -FromSession $private:连接3 -Path $Path -Destination $Destination -Force
}
}
else
{
if ($Recurse -eq $True)
{
Copy-Item -FromSession $private:连接3 -LiteralPath $LiteralPath -Destination $Destination -Force -Recurse
}
else
{
Copy-Item -FromSession $private:连接3 -LiteralPath $LiteralPath -Destination $Destination -Force
}
}
Write-Verbose '复制文件完成,即将断开连接。'
Remove-PSSession -Session $private:连接3
}
if ( $global:当前被控机_ip.被控机os类型 -in $linux)
{
if ( $PSVersionTable.psversion.major -ge 6)
{
}
else
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:在win中连接linux,依赖powershell 6,或以上版本。错误码12"
}
else
{
Write-Error "error:need powershell version 6 or higher.exit 12"
}
exit 12
}
if ( ($端口 -eq '') -or ($端口 -eq $null) )
{
$端口 = 22
}
Write-Verbose '使用ssh秘钥1,连接w2l开始'
[string]$private:temp011 = ssh.exe -l root -i "$env:USERPROFILE\.ssh\id_rsa" ${目的ip地址} -p $端口 -o PasswordAuthentication=no 'date' *>&1
Write-Verbose $private:temp011
if ( $private:temp011.ToLower().Contains('Permission denied'.ToLower()) )
{
$秘钥1连接成功 = $false
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Verbose "错误:使用ssh秘钥1,在${目的ip地址}上连接w2l失败"
}
else
{
Write-Verbose "error:ssh-key-file1 login failed:${目的ip地址}"
}
}
else
{
$秘钥1连接成功 = $true
}
if ( $private:temp011.ToLower().Contains('timed out'.ToLower()) )
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:使用ssh秘钥1,在${目的ip地址}上w2l连接超时。退出码13"
}
else
{
Write-Error "error:ssh-key-file1 login timeout: ${被控机ip地址} .exit 13"
}
exit 13
}
if ($秘钥1连接成功 -eq $true)
{
$private:连接1 = New-PSSession -HostName ${目的ip地址} -Port $端口 -UserName root -KeyFilePath "$env:USERPROFILE\.ssh\id_rsa"
Write-Verbose '连接成功。现在开始测试LiteralPath,或Path,目录存在否:'
if ($LiteralPath -eq '')
{
$Path存在否 = Invoke-Command -Session $private:连接1 -ScriptBlock { Test-Path $args[0] } -ArgumentList $path
}
else
{
$Path存在否 = Invoke-Command -Session $private:连接1 -ScriptBlock { Test-Path $args[0] } -ArgumentList $LiteralPath
}
if ($Path存在否 -eq $true)
{
}
else
{
Remove-PSSession -Session $private:连接1
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:找不到源目录,或源文件。错误码5"
}
else
{
Write-Error "error:The source directory or file could not be found.exit 5"
}
exit 5
}
Write-Verbose '【连接1】连接成功。现在开始复制文件:'
if ($LiteralPath -eq '')
{
if ($Recurse -eq $True)
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接1 -Path $Path -Destination $Destination -Force -Recurse
}
else
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接1 -Path $Path -Destination $Destination -Force
}
}
else
{
if ($Recurse -eq $True)
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接1 -LiteralPath $LiteralPath -Destination $Destination -Force -Recurse
}
else
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接1 -LiteralPath $LiteralPath -Destination $Destination -Force
}
}
Write-Verbose '【连接1】复制文件完成,即将断开连接。'
Remove-PSSession -Session $private:连接1
}
else
{
Write-Verbose '使用ssh秘钥2,连接w2l开始'
[string]$private:temp012 = ssh.exe -l root -i "${global:kasini3000目录}\ssh_key_files_old1\id_rsa" ${目的ip地址} -p $端口 -o PasswordAuthentication=no 'date' *>&1
Write-Verbose $private:temp012
if ( $private:temp012.ToLower().Contains('Permission denied'.ToLower()) )
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:使用ssh秘钥2,在${目的ip地址}上连接w2l失败。错误码15"
}
else
{
Write-Error "error:ssh-key-file2 login failed${目的ip地址}.exit 15"
}
exit 15
}
$private:连接2 = New-PSSession -HostName ${目的ip地址} -Port $端口 -UserName root -KeyFilePath "${global:kasini3000目录}\ssh_key_files_old1\id_rsa"
Write-Verbose '连接成功。现在开始测试LiteralPath,或Path,目录存在否:'
if ($LiteralPath -eq '')
{
$Path存在否 = Invoke-Command -Session $private:连接2 -ScriptBlock { Test-Path $args[0] } -ArgumentList $path
}
else
{
$Path存在否 = Invoke-Command -Session $private:连接2 -ScriptBlock { Test-Path $args[0] } -ArgumentList $LiteralPath
}
if ($Path存在否 -eq $true)
{
}
else
{
Remove-PSSession -Session $private:连接2
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:找不到源目录,或源文件。错误码5"
}
else
{
Write-Error "error:The source directory or file could not be found.exit 5"
}
exit 5
}
Write-Verbose '【连接2】连接成功。现在开始复制文件:'
if ($LiteralPath -eq '')
{
if ($Recurse -eq $True)
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接2 -Path $Path -Destination $Destination -Force -Recurse
}
else
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接2 -Path $Path -Destination $Destination -Force
}
}
else
{
if ($Recurse -eq $True)
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接2 -LiteralPath $LiteralPath -Destination $Destination -Force -Recurse
}
else
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接2 -LiteralPath $LiteralPath -Destination $Destination -Force
}
}
Write-Verbose '【连接2】复制文件完成,即将断开连接。'
Remove-PSSession -Session $private:连接2
}
}
}
if ($IsLinux -eq $True)
{
if ( $global:当前被控机_ip.被控机os类型 -in $win)
{
Write-Error "无法从【linux主控机】复制文件到【win被控机】"
exit 3
}
if ( $global:当前被控机_ip.被控机os类型 -in $linux)
{
if ( ($端口 -eq '') -or ($端口 -eq $null) )
{
$端口 = 22
}
Write-Verbose '使用ssh秘钥1,连接l2l开始'
[string]$private:temp011 = ssh -l root -i '/root/.ssh/id_rsa' ${目的ip地址} -p $端口 -o PasswordAuthentication=no 'date' *>&1
Write-Verbose $private:temp011
if ( $private:temp011.ToLower().Contains('Permission denied'.ToLower()) )
{
$秘钥1连接成功 = $false
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Verbose "错误:使用ssh秘钥1,在${目的ip地址}上连接l2l失败"
}
else
{
Write-Verbose "error:ssh-key-file1 login failed:${目的ip地址}"
}
}
else
{
$秘钥1连接成功 = $true
}
if ( $private:temp011.ToLower().Contains('timed out'.ToLower()) )
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:使用ssh秘钥1,在${目的ip地址}上l2l连接超时。退出码13"
}
else
{
Write-Error "error:ssh-key-file1 login timeout: ${被控机ip地址} .exit 13"
}
exit 13
}
if ($秘钥1连接成功 -eq $true)
{
$private:连接5 = New-PSSession -HostName ${目的ip地址} -Port $端口 -UserName root -KeyFilePath '/root/.ssh/id_rsa'
Write-Verbose '连接成功。现在开始测试LiteralPath,或Path,目录存在否:'
if ($LiteralPath -eq '')
{
$Path存在否 = Invoke-Command -Session $private:连接5 -ScriptBlock { Test-Path $args[0] } -ArgumentList $path
}
else
{
$Path存在否 = Invoke-Command -Session $private:连接5 -ScriptBlock { Test-Path $args[0] } -ArgumentList $LiteralPath
}
if ($Path存在否 -eq $true)
{
}
else
{
Remove-PSSession -Session $private:连接5
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:找不到源目录,或源文件。错误码5"
}
else
{
Write-Error "error:The source directory or file could not be found.exit 5"
}
exit 5
}
Write-Verbose '【连接5】连接成功。现在开始复制文件:'
if ($LiteralPath -eq '')
{
if ($Recurse -eq $True)
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接5 -Path $Path -Destination $Destination -Force -Recurse
}
else
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接5 -Path $Path -Destination $Destination -Force
}
}
else
{
if ($Recurse -eq $True)
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接5 -LiteralPath $LiteralPath -Destination $Destination -Force -Recurse
}
else
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接5 -LiteralPath $LiteralPath -Destination $Destination -Force
}
}
Write-Verbose '【连接5】复制文件完成,即将断开连接。'
Remove-PSSession -Session $private:连接5
}
else
{
Write-Verbose '使用ssh秘钥2,连接l2l开始'
[string]$private:temp012 = ssh -l root -i "${global:kasini3000目录}/ssh_key_files_old1/id_rsa" ${目的ip地址} -p $端口 -o PasswordAuthentication=no 'date' *>&1
Write-Verbose $private:temp012
if ( $private:temp012.ToLower().Contains('Permission denied'.ToLower()) )
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:使用ssh秘钥2,在${目的ip地址}上连接l2l失败。错误码15"
}
else
{
Write-Error "error:ssh-key-file2 login failed${目的ip地址}.exit 15"
}
exit 15
}
$private:连接6 = New-PSSession -HostName ${目的ip地址} -Port $端口 -UserName root -KeyFilePath "${global:kasini3000目录}/ssh_key_files_old1/id_rsa"
Write-Verbose '连接成功。现在开始测试LiteralPath,或Path,目录存在否:'
if ($LiteralPath -eq '')
{
$Path存在否 = Invoke-Command -Session $private:连接6 -ScriptBlock { Test-Path $args[0] } -ArgumentList $path
}
else
{
$Path存在否 = Invoke-Command -Session $private:连接6 -ScriptBlock { Test-Path $args[0] } -ArgumentList $LiteralPath
}
if ($Path存在否 -eq $true)
{
}
else
{
Remove-PSSession -Session $private:连接6
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:找不到源目录,或源文件。错误码5"
}
else
{
Write-Error "error:The source directory or file could not be found.exit 5"
}
exit 5
}
Write-Verbose '【连接6】连接成功。现在开始复制文件:'
if ($LiteralPath -eq '')
{
if ($Recurse -eq $True)
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接6 -Path $Path -Destination $Destination -Force -Recurse
}
else
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接6 -Path $Path -Destination $Destination -Force
}
}
else
{
if ($Recurse -eq $True)
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接6 -LiteralPath $LiteralPath -Destination $Destination -Force -Recurse
}
else
{
#$env:LANG = 'en_US.UTF-8'
Copy-Item -FromSession $private:连接6 -LiteralPath $LiteralPath -Destination $Destination -Force
}
}
Write-Verbose '【连接6】复制文件完成,即将断开连接。'
Remove-PSSession -Session $private:连接6
}
}
}
Write-Verbose '从被控机到主控机,复制文件完成'
exit 0
PowerShell
1
https://gitee.com/qf2008/kasini3000.git
git@gitee.com:qf2008/kasini3000.git
qf2008
kasini3000
project_14055964
master

搜索帮助