问:通过【字符串界面】。如何从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连接
 
 

分享我编写的powershell脚本:ssh-copy-id.ps1的更多相关文章

  1. 技术分享:如何在PowerShell脚本中嵌入EXE文件

    技术分享:如何在PowerShell脚本中嵌入EXE文件 我在尝试解决一个问题,即在客户端攻击中只使用纯 PowerShell 脚本作为攻击负荷.使用 PowerShell 运行恶意代码具有很多优点, ...

  2. 分享原创powershell脚本小工具ctracert.ps1

    ----------[脚本介绍]----------- 脚本名称:ctracert.ps1软件名称:灰主牛 跟踪路由 归属地版 V1.0脚本作用:1跟踪路由.2显示归属地.(注意不带显示时间功能)脚本 ...

  3. 云服务程序在启动的时候执行Powershell脚本

    如果在云服务程序启动时候,需要执行Powershell脚本,我们需要将脚本嵌入到程序中,并且编写一个cmd来执行这个脚本,具体如下: 1.编写测试的Powershell脚本:每隔10分钟 检测dns ...

  4. Powershell使用SSH

    在编写Powershell脚本时,有时会需要和Linux机器进行交互.所以这时就需要在Powershell中使用SSH. 本文将介绍如何在Powershell中安装SSH模块,以及如何使用ssh命令. ...

  5. ansible-playbook编写服务器初始化脚本

    1.原理:通过limit的参数,限制新定义的服务器.即可给新买的服务器初始化优化.(如下图所示) 首先我们编写一个总入口的palybook脚本: init.yml --- - hosts: all u ...

  6. powershell脚本,命令行参数传值,并绑定变量的例子

    这是小技巧文章,所以文章不长.但原创唯一,非常重要.我搜了下,还真没有人发 powershell怎样 [命令行 参数 绑定],所以我决定写成博客. 搜索关键字如下: powershell 命令行 参数 ...

  7. PowerShell脚本自动设置安卓手机wifi代理

    在实际测试工作中,经常要将安卓手机通过wifi代理的形式连接到本机的fiddler或charles服务器代理进行抓包测试.最近一直在想,有没有什么方法可以自动设置安卓手机的wifi代理,曾经想通过修改 ...

  8. 如何在windows计划中调用备份sharepoint2010网站集的powershell脚本

    最近有个项目需要在在windows计划中使用powershell脚本备份sharepoint2010网站集,打开sharepoint的powershell执行命令管理界面的属性 查看: C:\Wind ...

  9. 用PowerShell脚本删除SharePoint 的 Page中的WebPart

    编写PowerShell脚本可以删除page中所有的webpart,也可以根据webpart的属性信息去删除特定的webpart. 下面的PowerShell脚本便是删除对应page中所有的webpa ...

随机推荐

  1. Elasticsearch .net client NEST使用说明 2.x -更新版

    Elasticsearch .net client NEST使用说明 目录: Elasticsearch .net client NEST 5.x 使用总结 elasticsearch_.net_cl ...

  2. HTML 转 PDF的两种实现方式

    itextsharp:不怎么兼容html原代码,特别是div的布局.但是能更灵活的创建原生pdf代码 wkhtmltopdf:基本可以不用写代码,直接将html文件生成pdf 文件存放在自己的百度云盘 ...

  3. C# DataGridView 动态添加列和调整列顺序

    https://yq.aliyun.com/articles/421700 // DataGridView1的ColumnDisplayIndexChanged事件处理方法private void D ...

  4. 阿里云Hadoop集群DataNode连接不上NameNode

    在logs日志中可以看见DataNode多次去连NameNode,但是都失败了. 经过长时间的研究百度,终于知道了原因. 原因就是安全组限制了端口的开放,所以我们只要把相应的端口打开即可.

  5. js及jsp区别

  6. [Shiro] tutorial 1 :SecurityManager and Subject

    SecurityManager是Shiro的绝对核心,不同于java.lang.SecurityManager,每个应用程序都要有一个SecurityManager. 所以我们第一件事就是配置一个Se ...

  7. caffe分类

    1.制作数据  标签   label.bat dir/s/on/b >.txt 2.制作lmdb     convert_lmdb.bat SET GLOG_logtostderr= E:\ca ...

  8. CNN 分割

    测试的是Cifar10数据集,采用VGG模型的网络参数是[32, 'M', 64, 'M', 128, 128, 'M', 256, 256, 'M', 256, 256],准确度大概在90.6左右 ...

  9. markdown在线编辑插件mditor

    官方地址 https://bh-lay.github.io/mditor/ ##使用方法 #1.页面添加dom ```javascript <textarea id="md_edito ...

  10. 学号 20175212 《Java程序设计》第九周学习总结

    学号 20175212 <Java程序设计>第九周学习总结 教材学习内容总结 一.MySQL数据库管理系统 1.在官网上下载并安装MySQL 2.在IDEA中输入测试代码Connectio ...