在编写Powershell脚本时,有时会需要和Linux机器进行交互。所以这时就需要在Powershell中使用SSH。

本文将介绍如何在Powershell中安装SSH模块,以及如何使用ssh命令。

一 Posh-SSH模块

1 安装

在Windows中点击搜索,powershell_ise

在PowerShell_ise的环境中运行

PS C:\windows\system32> find-module *ssh*

Version    Name                                Repository           Description
------- ---- ---------- -----------
2.0.2 Posh-SSH PSGallery Provide SSH and SCP functionality for executing commands against remote hosts.
1.0.0 SSH PSGallery Provides a PowerShell-based SSH client based on SSH.net http://sshnet.codeplex.com/
1.9 SSHSessions PSGallery Svendsen Tech's SSH-Sessions module provides SSH session creation, management and int...
1.1.3 PowerSSH PSGallery This module detects the first use of an SSH command, automatically runs the SSH agent...
1.0 cEPRSSharepoint PSGallery DSCModule helps in installing & configuring the sharepoint site, Farm etc.,
1.0.4 PSShortcut PSGallery This module eases working with Windows shortcuts (LNK and URL) files.

如果没有find-module的命令,需要安装PackageManagement:

https://www.microsoft.com/en-us/download/details.aspx?id=51451

本文采用Posh-SSH的模块:

Install-Module Posh-SSH

可以通过下面的命令,查看安装的模块包含什么命令:

PS C:\Users\hengwei> get-command -Module posh-ssh
 
CommandType Name ModuleName
----------- ---- ----------
Function Get-PoshSSHModVersion posh-ssh
Function Get-SFTPChildItem posh-ssh
......

2 在PowerShell中使用SSH

可以用下面的脚本进行测试:

$username = "hengwei"
$password = "xxxx"
$secure = $password | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username,$secure)
 
New-SSHSession -ComputerName 40.125.173.212 -Credential $cred -AcceptKey
 
Invoke-SSHCommand -SessionId 0 -Command "cat /etc/redhat-release"
Invoke-SSHCommand -SessionId 0 -Command "df -k"
Host : 40.125.173.212
Output : {CentOS Linux release 7.3.1611 (Core) }
ExitStatus : 0
 
Host : 40.125.173.212
Output : {Filesystem 1K-blocks Used Available Use% Mounted on, /dev/sda2 30929148 1200340 29728808 4% /, devtmpfs 847688 0 847688 0% /dev, tmp
fs 858264 0 858264 0% /dev/shm...}
ExitStatus : 0

二 SSHSessions模块

1 安装

和Posh-SSH类似,安装采用install-module的方式:

Install-module SSHSessions

其命令比Posh-SSH要少,但都是SSH相关的:

PS C:\Users\hengwei> get-command -Module sshsessions
 
CommandType Name ModuleName
----------- ---- ----------
Function ConvertFrom-SecureToPlain SSHSessions
Function Enter-SshSession SSHSessions
Function Get-SshSession SSHSessions
Function Invoke-SshCommand SSHSessions
Function New-SshSession SSHSessions
Function Remove-SshSession SSHSessions

2 在PowerShell中使用SSH

New-SshSession -ComputerName 40.125.173.212 -Username hengwei -Password xxxx
 
PS C:\Users\hengwei> Get-SshSession
 
ComputerName Connected
------------ ---------
40.125.173.212

SSHSessions中有一个enter-sshsession的命令,可以直接进入交互式模式,进行一些交互的操作:

PS C:\Users\hengwei> Enter-SshSession -ComputerName 40.125.173.212
[40.125.173.212]: /home/hengwei # : pwd
/home/hengwei
[40.125.173.212]: /home/hengwei # : mkdir test
[40.125.173.212]: /home/hengwei # : ls
test
[40.125.173.212]: /home/hengwei # : ls -l
total 0
drwxrwxr-x. 2 hengwei hengwei 6 Nov 20 07:29 test
[40.125.173.212]: /home/hengwei # :

也可以使用invoke-sshcommand的模式实现命令

PS C:\Users\hengwei> Invoke-SshCommand -ComputerName 40.125.173.212 -Command "sudo cat /etc/fstab"
[40.125.173.212]
# UUID=b7559ac5-b3a4-4b00-b98a-a2a2611806d0 / xfs defaults 0 0
UUID=b98659b2-5f8c-493e-9304-658905ef1391 /boot xfs defaults 0 0

大家还可以安装一下其他的ssh模块,实现在Powershell中的ssh功能。

三 删除模块

以Posh-SSH模块为例,删除命令:

PS C:\windows\system32> remove-module -name posh-ssh -Force -Verbose -Debug
VERBOSE: Removing the imported "Get-PoshSSHModVersion" function.
VERBOSE: Removing the imported "Get-SFTPChildItem" function.
VERBOSE: Removing the imported "Get-SFTPContent" function.
......

同时,删除Posh-SSH的目录:

C:\Program Files\WindowsPowerShell\Modules\Posh-SSH

这样就彻底删除了Posh-SSH模块。

Powershell使用SSH的更多相关文章

  1. Azure PowerShell (12) 通过Azure PowerShell创建SSH登录的Linux VM

    <Windows Azure Platform 系列文章目录> 本章将介绍如何使用Azure PowerShell,创建SSH登录的Linux VM 前提要求: 1.安装Azure Pow ...

  2. powershell下ssh客户端套件实现

    有时会需要和Linux机器进行交互.所以这时就需要在Powershell中使用SSH. 0x01 查找Powershell中的SSH功能模块 如图,显示没有find-module的命令,需要安装Pac ...

  3. git SSH keys

    An SSH key allows you to establish a secure connection between your computer and GitLab. Before gene ...

  4. 在win中,给powershell客户端,搭建sshd服务器。

    下载:https://github.com/PowerShell/Win32-OpenSSH/releases     问:为什么要用这个sshd?答:这是微软用,openssh官方的源码,源码网址: ...

  5. Gitlab的SSH配置(linux和windows双版本)

    1. 步骤 1.首先现在电脑端安装好git,windows端请安装Git for Windows,Linux端请自行网上查询(Ubuntu: sudo apt-get install git)   2 ...

  6. gitlab 添加ssh秘钥

    在创建新的ssh秘钥对之前,要先确认一下电脑中是否已经有了一对秘钥: Git Bash on Windows / GNU/Linux / macOS / PowerShell: cat ~/.ssh/ ...

  7. Docker windows nano server容器中安装ssh实现远程登录管理

    [问题] 使用ServiceMonitor.exe作为前台进程运行起来的容器无法attach. 无法远程连接到运行中的容器中进行管理. [解决方法] 在container中新建管理员用户,通过SSH实 ...

  8. 食用Win系统自带的PowerShell登录服务器

    运行powershell输入ssh root@你的服务器ip -p你的端口 切换rm ~/.ssh/known_hosts cmd 运行 ping 你的ip -t一直ping ctrl+c停止 tra ...

  9. 使用PowerShell连接Ubuntu

    Ubuntu安装PowerShell Ubuntu安装PowerShell帮助文档 # Download the Microsoft repository GPG keys wget -q https ...

随机推荐

  1. 13.常见模块re-正则模块

    1.正则 正则表达式是计算机科学的一个概念,正则表通常被用来检索.替换那些符合某个模式(规则)的文本.也就是说使用正则表达式可以在字符串中匹配出你需要的字符或者字符串,甚至可以替换你不需要的字符或者字 ...

  2. Struts的url-pattern配置问题

    一,servlet容器对url的匹配过程: 当一个请求发送到servlet容器的时候,容器先会将请求的url减去当前应用上下文的路径作为servlet的映射url,比如我访问的是http://loca ...

  3. dp2--合并石子(一)

    dp2--合并石子(一) 一.心得 二.题目 石子合并(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述     有N堆石子排成一排,每堆石子有一定的数量.现要将 ...

  4. SQL Server 2016 —— 聚集列存储索引的功能增强

    作者 Jonathan Allen,译者         邵思华         发布于     2015年6月14日   聚集列存储索引(CC Index)是SQL Server 2014中两大最引 ...

  5. java:输出流程printStream

    // TODO 自动生成的方法存根 //路径文件 File file = new File("F:"+File.separator+"work"+File.se ...

  6. 2017-02-20 安装Sql Server2016+配置Java环境

    昨天在安装Sql Server 2016时,在前面的规则检查结果中,出现“Polybase要求安装Oracle JRE7更新51(64位)或更高版本”规则失败的错误,如图 解决方案为,下载安装jdk, ...

  7. Microsoft Prism安装使用教程 搭建WPF松耦合架构框架

    Microsoft Prism安装使用教程 搭建WPF松耦合架构框架 Prism是由微软Patterns & Practices团队开发的项目,目的在于帮助开发人员构建松散耦合的.更灵活.更易 ...

  8. uva 10168 哥德巴赫猜想

    https://vjudge.net/problem/UVA-10168 给出一个整数n,问是否能将它化为四个素数相加的形式,如果可以的话就输出这四个数.显然n<8时是不可能的.对于大于等于8得 ...

  9. MySQL 索引的知识整理

    前言:       很多面试者,在面试的时候,都会回答,”索引就相当于一本书的字典,有了他能够很快的找到数据”, 这种答案好像在读书的时候老师告诉这么说的吧.今天来全面的描述一下数据库索引的原理及优化 ...

  10. SQL Compare

    http://pan.baidu.com/s/1jGIEEm6 来源:数据库同步工具: Red Gate Sql Compare