39 Star 222 Fork 51

chuanjiao10 / kasini3000

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
k_run_ip_port_w2l_user.ps1 6.76 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 = '',
[Alias('file')][String]$ps1脚本文件名,
[Alias("allparameter")]$传入参数,
[parameter(Position = 0)][Alias("scriptblock")][scriptblock]$powershell代码块 = { },
$复制主控机node_script目录到被控机 = $false
)
function yg严格测试布尔型变量($被测试的布尔形变量)
{
if (($被测试的布尔形变量 -eq 1) -or ($被测试的布尔形变量 -eq $true) -or ($被测试的布尔形变量 -eq 'true'))
{
return $true
}
elseif (($被测试的布尔形变量 -eq 0) -or ($被测试的布尔形变量 -eq $false) -or ($被测试的布尔形变量 -eq 'false'))
{
return $false
}
else
{
Write-Error '错误:不合法的布尔型值,错误码999'
exit 999
}
}
[bool]$复制主控机node_script目录到被控机 = yg严格测试布尔型变量 $复制主控机node_script目录到被控机
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'
}
& "${global:kasini3000目录}/zkj_从主控机到linux被控机_ssh_knowhost免yes.ps1"
if ($IsLinux -eq $True)
{
Write-Error "应该使用另一个脚本!"
exit 21
}
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.'
}
if ( ($ps1脚本文件名 -eq $null) -or ($ps1脚本文件名 -eq '') )
{
#这段代码在run_win2linux_key_pwd.ps1,run_win2win5985_pwd.ps1,run_linux2linux_key_pwd.ps1,k-commit.ps1
$有脚本文件 = $false
if ( ($powershell代码块.Ast.Extent.Text -eq '{ }') -or ($powershell代码块.Ast.Extent.Text -eq '{}') )
{
Write-Error "错误:没有输入脚本文件名,同时也没有输入代码块"
exit 15
}
else
{
if (& 'kcd_被控机运行的代码块_含有kcd.ps1' -输入脚本代码块 $powershell代码块)
{
Write-Error '错误:脚本内含有kcd等关键字'
exit 19
}
}
}
else
{
$有脚本文件 = $true
if ( ($powershell代码块.Ast.Extent.Text -eq '{ }') -or ($powershell代码块.Ast.Extent.Text -eq '{}') )
{
}
else
{
Write-Error "错误:有输入脚本文件名,同时有输入代码块"
exit 16
}
if (Test-Path -LiteralPath $ps1脚本文件名)
{
if (& 'kcd_被控机运行的脚本_含有kcd.ps1' -输入脚本文件名 $ps1脚本文件名)
{
Write-Error '错误!脚本内含有kcd等关键字'
exit 18
}
}
else
{
Write-Error "找不到脚本文件"
exit 17
}
}
if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
{
if ( $PSVersionTable.psversion.major -ge 6)
{
}
else
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:在win中连接linux,依赖powershell 7.2,或以上版本。退出码12"
}
else
{
Write-Error "error: From windows connecting to linux , need powershell version 7.2, or above. Exit code 12"
}
exit 12
}
if ($复制主控机node_script目录到被控机 -eq $true)
{
& 'cpn21复制主控机node_script目录到当前被控机.ps1'
}
[string]$temp001 = ssh.exe -V *>&1
if (-not ($temp001.ToLower().Contains('ssh'.ToLower())) )
{
Write-Error '管理linux被控机,需要ssh.exe。请运行【zkj_install_k记饭店_win主控机.ps1】'
exit 23
}
$端口通了 = & '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
}
if ($ssh_key_file_path -eq '')
{
Write-Verbose '使用ssh密码,连接开始'
$private:连接w2l01 = New-PSSession -HostName ${目的ip地址} -Port $端口 -UserName $用户名
if ( ($private:连接w2l01 -eq $null) -or ($private:连接w2l01 -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:连接w2l01 = New-PSSession -HostName ${目的ip地址} -Port $端口 -UserName $用户名 -KeyFilePath $ssh_key_file_path
if ( ($private:连接w2l01 -eq $null) -or ($private:连接w2l01 -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:连接w2l01 -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:连接w2l01 -Force
}
[scriptblock]$private:cmd1 = `
{
Test-Path -LiteralPath '/etc/kasini3000'
}
$private:返回 = Invoke-Command -Session $private:连接w2l01 -ScriptBlock $private:cmd1
if ($private:返回 -eq $true)
{
}
else
{
[scriptblock]$private:cmd2 = `
{
mkdir '/etc/kasini3000'
}
Invoke-Command -Session $private:连接w2l01 -ScriptBlock $private:cmd2
$env:LANG = 'zh_CN.UTF-8'
Copy-Item -LiteralPath 'c:\ProgramData\kasini3000\node_script' -Destination '/etc/kasini3000' -Recurse -ToSession $private:连接w2l01
}
Invoke-Command -Session $private:连接w2l01 -ScriptBlock {
if (Test-Path '/etc/kasini3000/node_script/kasini3000/add_path.ps1')
{
& '/etc/kasini3000/node_script/kasini3000/add_path.ps1'
}
}
if ( $有脚本文件 -eq $true)
{
Invoke-Command -Session $private:连接w2l01 -FilePath $ps1脚本文件名 -ArgumentList $传入参数
}
else
{
Invoke-Command -Session $private:连接w2l01 -ScriptBlock $powershell代码块 -ArgumentList $传入参数
}
Write-Verbose '执行命令完成,即将断开连接。'
Remove-PSSession -Session $private:连接w2l01
}
Write-Verbose "完成在win2linux被控机【${目的ip地址}】上执行"
exit 0
PowerShell
1
https://gitee.com/chuanjiao10/kasini3000.git
git@gitee.com:chuanjiao10/kasini3000.git
chuanjiao10
kasini3000
kasini3000
master

搜索帮助