39 Star 222 Fork 51

chuanjiao10 / kasini3000

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
k-sshfs-copy-a-file.ps1 4.16 KB
一键复制 编辑 原始数据 按行查看 历史
#建议保存编码为:bom头 + utf8
param
(
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[Alias("ipaddress")][String]$目的ip地址,
[Alias("port")]$端口 = 22 ,
[Alias("password")][string]$ssh密码 = '', #用密码mount sshfs
[Alias("private_ssh_key_file")][string]$主控机ssh私钥路径 = '', #用ssh私钥mount
[ValidateNotNullOrEmpty()]
[Alias("LiteralPath")][string]$源文件全路径,
[ValidateNotNullOrEmpty()]
[Alias("Destination")][string]$目标绝对路径_必须是文件夹
)
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'
}
if (Test-Path -LiteralPath $源文件全路径)
{
}
else
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误,找不到源文件 $源文件全路径 ,退出码1"
}
else
{
Write-Error "Error, cannot find source file $源文件全路径 , exit 1."
}
exit 1
}
if (& 'tcp--ping-v5.ps1' ${目的ip地址} -Port ${端口} -Quiet)
{
}
else
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误,目标ip【${目的ip地址}】的【${端口}】端口不通,退出码2。"
}
else
{
Write-Error "Error, target ip ${port} or port ${端口} is blocked, exit 2."
}
exit 2
}
if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "这个脚本不支持windows,退出码3。"
}
else
{
Write-Error "This script does not support windows,exit 3."
} exit 3
}
$username = /usr/bin/whoami
if ($username -ne 'root')
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error '错误:非管理员,退出码4。'
}
else
{
Write-Error "not root,exit 4."
}
exit 4
}
[string]$sshfsy = which sshfs
if ( ($null -eq $sshfsy) -or ($sshfsy -eq '') )
{
Write-Warning 'rpm -ivh https://mirrors.aliyun.com/almalinux/8.7/PowerTools/x86_64/os/Packages/fuse-sshfs-2.8-5.el8.x86_64.rpm'
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "这个脚本需要 'sshfs' 命令,但找不到它。退出码 6"
}
else
{
Write-Error "This script requires the 'sshfs' command, but it cannot be found. Exit 6"
}
exit 6
}
if ($IsLinux -eq $True)
{
$etcmnt = '/etc/kasini3000/mnt'
if ($主控机ssh私钥路径 -ne '')
{
#用秘钥mount sshfs
if (Test-Path -LiteralPath '/bin/ash') # 改善alpine-linux主控机兼容性
{
sshfs -o allow_other,IdentityFile=${主控机ssh私钥路径} root@${目的ip地址}:/ $etcmnt
}
else
{
sshfs -o allow_other,nonempty,IdentityFile=${主控机ssh私钥路径} root@${目的ip地址}:/ $etcmnt
}
}
else
{
#用密码mount sshfs
if (Test-Path -LiteralPath '/bin/ash') # 改善alpine-linux主控机兼容性
{
$ssh密码 | sshfs -o allow_other -o password_stdin root@${目的ip地址}:/ $etcmnt
}
else
{
$ssh密码 | sshfs -o allow_other,nonempty -o password_stdin root@${目的ip地址}:/ $etcmnt
}
}
Start-Sleep -Seconds 1
[string]$temp01 = df -hT | Select-String "${目的ip地址}"
# if ( $temp01.ToLower().Contains($目的ip地址.ToLower()) )
if ( ($null -eq $temp01) -or ($temp01 -eq '') )
{
/usr/bin/scp /root/kasini3000/ssh_key_files_old1/authorized_keys root@${目的ip地址}:/root/.ssh/
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:sshfs映射失败!退出码5。"
}
else
{
Write-Error "Error: sshfs mapping failed! Exit 5."
}
exit 5
}
else
{
$sshfs映射后的目标绝对路径 = $etcmnt + $目标绝对路径_必须是文件夹
Write-Verbose $sshfs映射后的目标绝对路径
if (Test-Path -LiteralPath $sshfs映射后的目标绝对路径)
{
}
else
{
Write-Warning "错误,目标ip上的路径不存在,现在将建立它 $目标绝对路径_必须是文件夹"
mkdir $sshfs映射后的目标绝对路径
}
Copy-Item -LiteralPath $源文件全路径 -Destination $sshfs映射后的目标绝对路径
if (Test-Path -LiteralPath '/etc/kasini3000/mnt/root/.ssh/authorized_keys')
{
chmod 600 '/etc/kasini3000/mnt/root/.ssh/authorized_keys'
}
Start-Sleep -Seconds 1
umount /etc/kasini3000/mnt
Write-Verbose '信息:复制文件成功!'
}
}
exit 0
PowerShell
1
https://gitee.com/chuanjiao10/kasini3000.git
git@gitee.com:chuanjiao10/kasini3000.git
chuanjiao10
kasini3000
kasini3000
master

搜索帮助