您的位置:老铁SEO > 站长新闻 > 网站交易 >

powershell远程连接linux,远程访问 Linux 服务器教程

文章来源:http://www.6cu.com

作者:外链网站

人气:15

2021-03-25 10:56:06

  如果我们需要在Windows系统下,远程访问Linux服务器,需要借助第三方模块Posh-SSH来完成访问和执行远程命令。

  首先需要在本地计算机安装Posh-SSH模块

  Install-Module-Name Posh-SSH

  通过New-SSHSession建立本地计算机和远程Linux服务器之间的会话session

  $session=New-SSHSession-ComputerName$server-Port$port-Credential$cred

  通过Invoke-SSHCommand在远程Linux服务器上执行shell命令

  Invoke-SSHCommand-Command$cmd-SSHSession$session

  执行完任务后,记得关系会话session

  Remove-SSHSession-SSHSession$session

  完整的代码示例

  function getCred($username,$password){

  $pass=ConvertTo-SecureString$password-AsPlainText-Force

  $cred=New-Object System.Management.Automation.PSCredential-ArgumentList$username,$pass

  return$cred

  }

  $server='47.100.1.234'

  $port='22'

  $username='abc'

  $password='123456'

  $cred=getCred-username$username-password$password

  $session=New-SSHSession-ComputerName$server-Port$port-Credential$cred

  $cmd='uname-a'

  Invoke-SSHCommand-Command$cmd-SSHSession$session

  Remove-SSHSession-SSHSession$session


相关文章

在线客服

外链咨询

扫码加我微信

微信:juxia_com

返回顶部