分享我編寫的powershell腳本:ssh-copy-id.ps1


 

問: 通過【字符串界面】。如何從win,通過ssh,連接到sshd?
答:
在任意版本win中,通過cmd.exe,powershell.exe中調用ssh.exe,連接sshd。
 

問: 通過【powershell對象界面】。如何從win,通過ssh,連接到sshd?
答:
ssh客戶端,需要安裝powershell6.0,及以上。
ssh服務器端,需要改寫sshd_config,加上
Subsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile
並重啟sshd。
powershell傳教士 2019-02-20
 

問: 通過powershell對象界面。如何從win,通過ssh,連接到sshd?poweershell代碼是什么?
答:
$連接1 = New-PSSession -HostName 1.1.1.1 -UserName root 
invoke-command -ScriptBlock { xx命令 } -Session $連接1
#或 invoke-command -FilePath 客戶機上的腳本_在服務器上運行.ps1  -Session $連接1
 

問: 上面命令每次都需要輸入密碼么?
答:
對,powershell團隊永遠不會開發保存密碼的功能。因為那是低安全級別。

問:
為什么要用new-pssession,不用ssh.exe?
powershell對象界面,比字符界面有哪些好處?
服務器端,客戶端,都用powershell有啥好處?
答:
1 服務器端<--->客戶端之間,傳遞強類型變量,而無需序列化/反序列化。
2 自動從客戶端,傳遞腳本文件,到服務器端。如:
invoke-command -FilePath 客戶機上的腳本_在服務器上運行.ps1  -Session $連接1
 
 
問: 如何實現ssh免密?
答:
1 使用無密碼ssh秘鑰對。ssh秘鑰比ssh密碼長度長,更安全。還支持服務端,客戶端雙向認證。

2 使用有密碼ssh秘鑰對+【ssh-agent.exe】
注意:這里的ssh秘鑰對,適合於ssh.exe連接sshd,也適合於new-pssession連接sshd。

問: 怎么生成ssh秘鑰對?
答:
1 ssh-keygen.exe回車,可以加參數。
2 使用puttygen.exe。
3 使用xshell ---》工具 ---》新建 用戶秘鑰生成向導。
等。
 

問: 用私鑰,通過powershell對象界面。如何從win,通過ssh,連接到sshd?poweershell代碼是什么?
答:
$連接1 = New-PSSession -HostName 1.1.1.1 -UserName root -KeyFilePath $env:USERPROFILE\.ssh\id_rsa
invoke-command -ScriptBlock { xx命令 } -Session $連接1
#或 invoke-command -FilePath 客戶機上的腳本_在服務器上運行.ps1  -Session $連接1
 
 
問: ssh-copy-id.ps1腳本的作用是?
答:
把ssh秘鑰的【公鑰】從win、linux本機,復制到目標ssh服務器。
不用輸入密碼,從而達到ssh免密自動化。
注意:這里的ssh秘鑰對,適合於ssh.exe連接sshd,也適合於new-pssession連接sshd。
 

問: 去哪里下載win版ssh服務器,客戶端?
答:
https://github.com/PowerShell/Win32-OpenSSH/releases
 

問: 為什么說我編寫的ssh-copy-id.ps1,比linux的ssh-copy-id還好用?
答:
linux的ssh-copy-id,對每台目標sshd服務器,都需要手動輸入密碼。不適合於批量操作。
而我編寫的腳本,可以保存密碼。你也可以改寫腳本,傳入密碼。
ssh-copy-id4.ps1:win,linux通用。
注意:不是遠程連接保存密碼,而是公鑰部署使用事先保存的密碼!
 

問: ssh-copy-id.ps1腳本的使用場合是?
答:
ssh服務器:可以是win,可以是linux。
運行ssh-copy-id.ps1腳本的ssh客戶機:可以是win,可以是linux。
 

問: 運行ssh-copy-id.ps1有什么前提要求?
答:
1 至少powershell 5.0。win7請直接安裝ps5.1:
https://docs.microsoft.com/zh-cn/powershell/wmf/5.1/install-configure
win10跳過此步驟。
 
2 安裝ps6.x。如:6.1.3
https://github.com/PowerShell/PowerShell/releases
 
3 (可選)需要用管理員權限,開啟powershell.exe,運行install-module powershellget -Force
 
4 (必選)需要用管理員權限,開啟【一個新的!】powershell.exe,運行install-module winscp
 
ssh-copy-id6.ps1
 
# 免責聲明:使用本腳本帶來的一切不良后果,本人概不負責!

<#
腳本目的:
從win、linux中復制【本機ssh公鑰】,到【目的linux版ssh服務器】。

前提條件:
install-module winscp

用法:
ssh-copy-id6.ps1 -目的ip 1.2.3.4 -用戶密碼明文 '你的密碼明文'
建議保存編碼為:bom頭 + utf8

腳本最新版下載地址:
https://pan.baidu.com/s/16deKKe3ZnCg809lffiVZWg

#> param ( $目的ip = '192.168.1.2', $用戶密碼明文 = '這里填入你的ssh密碼明文作為默認值,或在命令行輸入參數' ) #先運行ssh-keygen回車,產生key文件。https://github.com/PowerShell/Win32-OpenSSH/releases if (($PSEdition -eq 'Desktop') -or (test-path c:\)) { $key文件1 = "$env:USERPROFILE\.ssh\id_rsa.pub" $key文件2 = "$env:USERPROFILE\.ssh\authorized_keys" Copy-Item -LiteralPath $key文件1 -Destination $key文件2 } if (($PSEdition -eq 'Core') -or (test-path /tmp)) { $key文件1 = "/root/.ssh/id_rsa.pub" $key文件2 = "/root/.ssh/authorized_keys" Copy-Item -LiteralPath $key文件1 -Destination $key文件2 } $用戶名 = 'root' $用戶密碼密文 = ConvertTo-SecureString $用戶密碼明文 -AsPlainText -Force $我的登陸憑據 = New-Object System.Management.Automation.PSCredential ($用戶名,$用戶密碼密文) #QQ群號=183173532,名稱=powershell交流群,2019-06-13 $sftp連接參數 = new-WinSCPSessionOption -Protocol Sftp -HostName $目的ip -Credential $我的登陸憑據 $指紋 = Get-WinSCPHostKeyFingerprint -SessionOption $sftp連接參數 -Algorithm SHA-256 $sftp連接參數.SshHostKeyFingerprint = $指紋 $sftp連接 = new-WinSCPSession -SessionOption $sftp連接參數 if (Test-WinSCPPath -Path '/root/.ssh' -WinSCPSession $sftp連接) { Remove-WinSCPItem -Path '/root/.ssh' -Confirm:$false -WinSCPSession $sftp連接 } $權限700 = New-WinSCPTransferOption -FilePermissions (New-WinSCPItemPermission -Octal 700) New-WinSCPItem -Path '/root/.ssh' -ItemType Directory -TransferOptions $權限700 -WinSCPSession $sftp連接 $權限600 = New-WinSCPTransferOption -FilePermissions (New-WinSCPItemPermission -Octal 600) Send-WinSCPItem -LocalPath $key文件2 -RemotePath '/root/.ssh/' -TransferOptions $權限600 -WinSCPSession $sftp連接 Remove-WinSCPSession -WinSCPSession $sftp連接

 

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM