1 Star 0 Fork 54

feiman/kasini3000

forked from chuanjiao10/kasini3000 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
k_copyfrom_ip_port_l2l_user.ps1 5.31 KB
一键复制 编辑 原始数据 按行查看 历史
#建议保存编码为:bom头 + utf8
param
(
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[Alias("ipaddress")][String]$目的ip地址,
[Alias("port")][uint16]$端口 = 22,
[ValidateNotNullOrEmpty()]
[Alias("user")][String]$用户名 = 'root',
[String]$ssh_key_file_path = '',
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$LiteralPath,
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$Destination,
[Switch]$Recurse,
#缩水封装
[String[]]$Exclude #源目录,需要使用【-path】和【*】才灵。如: -path 'd:\xxx\*'
)
if (Test-Path -LiteralPath $Destination)
{
}
else
{
Write-Error "错误:找不到目标目录。退出码4"
exit 4
}
if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Warning '错误:应该使用另一个脚本。错误码22'
}
else
{
Write-Warning 'Error: Another script should be used.exit 22'
}
exit 22
}
if ($IsLinux -eq $True)
{
& '/etc/kasini3000/0k_source.ps1'
}
if ($IsLinux -eq $True)
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Warning '错误:应该使用另一个脚本。'
}
else
{
Write-Warning 'Error: Another script should be used.'
}
}
Write-Verbose '从主控机到被控机,复制文件开始'
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Warning '本脚本限制:需要用户交互输入密码,无法自动。'
}
else
{
Write-Warning 'Limitation of this script: It requires the user to enter the password interactively, not automatically.'
}
$端口通了 = & '/etc/kasini3000/tcp--ping-v3.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
}
if ($IsLinux -eq $True)
{
if ($ssh_key_file_path -eq '')
{
Write-Verbose '使用ssh密码,连接开始'
$private:连接l2l11 = New-PSSession -HostName ${目的ip地址} -Port $端口 -UserName $用户名
if ( ($private:连接l2l11 -eq $null) -or ($private:连接l2l11 -eq '') )
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:使用密码连接失败。错误码25"
}
else
{
Write-Error "error:node port cloesd.exit 25"
}
exit 25
}
}
else
{
Write-Verbose '使用ssh秘钥,连接开始'
$private:连接l2l11 = New-PSSession -HostName ${目的ip地址} -Port $端口 -UserName $用户名 -KeyFilePath $ssh_key_file_path
if ( ($private:连接l2l11 -eq $null) -or ($private:连接l2l11 -eq '') )
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:使用密码连接失败。错误码26"
}
else
{
Write-Error "error:node port cloesd.exit 26"
}
exit 26
}
}
Write-Verbose '连接成功。现在开始执行命令:'
$主控机公钥sha1 = Get-FileHash -Algorithm sha1 -LiteralPath "${global:kasini3000_data_path}\ssh_key_files_old1\authorized_keys"
$被控机公钥sha1 = Invoke-Command -Session $private:连接l2l11 -ScriptBlock { Get-FileHash -Algorithm sha1 -LiteralPath '/root/.ssh/authorized_keys' }
Write-Verbose ("公钥: {0} '---' {1} " -f ${主控机公钥sha1}.Hash,${被控机公钥sha1}.Hash)
if ($主控机公钥sha1.Hash -ne $被控机公钥sha1.Hash)
{
Write-Verbose "主控机-被控机之间,公钥有不同"
Copy-Item -LiteralPath "${global:kasini3000_data_path}\ssh_key_files_old1\authorized_keys" -Destination '/root/.ssh/' -ToSession $private:连接l2l11 -Force
}
[scriptblock]$private:cmd1 = `
{
Test-Path -LiteralPath '/etc/kasini3000'
}
$private:返回 = Invoke-Command -Session $private:连接l2l11 -ScriptBlock $private:cmd1
if ($private:返回 -eq $true)
{
}
else
{
[scriptblock]$private:cmd2 = `
{
mkdir '/etc/kasini3000'
}
Invoke-Command -Session $private:连接l2l11 -ScriptBlock $private:cmd2
$env:LANG = 'zh_CN.UTF-8'
Copy-Item -LiteralPath 'c:\ProgramData\kasini3000\node_script' -Destination '/etc/kasini3000' -Recurse -ToSession $private:连接l2l11
}
Invoke-Command -Session $private:连接l2l11 -ScriptBlock {
if (Test-Path '/etc/kasini3000/node_script/kasini3000/tj在被控机添加path.ps1')
{
& '/etc/kasini3000/node_script/kasini3000/tj在被控机添加path.ps1'
}
}
Write-Verbose '连接成功。现在开始测试LiteralPath目录存在否:'
$Path存在否 = Invoke-Command -Session $private:连接l2l11 -ScriptBlock { Test-Path $args[0] } -ArgumentList $LiteralPath
if ($Path存在否 -eq $true)
{
}
else
{
Remove-PSSession -Session $private:连接l2l11
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 ($Recurse -eq $True)
{
Copy-Item -FromSession $private:连接l2l11 -LiteralPath $LiteralPath -Destination $Destination -Force -Recurse -Exclude $Exclude
}
else
{
Copy-Item -FromSession $private:连接l2l11 -LiteralPath $LiteralPath -Destination $Destination -Force -Exclude $Exclude
}
Write-Verbose '复制文件完成,即将断开连接。'
Remove-PSSession -Session $private:连接l2l11
}
Write-Verbose '从主控机到被控机,复制文件完成'
exit 0
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PowerShell
1
https://gitee.com/feiman8888/kasini3000.git
[email protected]:feiman8888/kasini3000.git
feiman8888
kasini3000
kasini3000
master

搜索帮助