SSH 客户端到 PowerShell 中的客户端托管

时间:2021-07-12 06:14:36

标签: powershell ssh multiplayer lan

开始在 PowerShell 中编写一个小型纸牌游戏,以熟悉工作软件,并且随着我想添加的内容不断增加。我想过做多人游戏并偶然发现了 SSH。一直在摆弄它一段时间,这让我有点怀疑自己是否可以在 PowerShell 中进行多人“游戏”。我曾尝试在 PowerShell 中寻找与多人游戏相关的任何内容,但几乎没有找到。

所以这是我的问题。

我在多人游戏部分设置了主持/加入游戏的能力。选择主机检查您是否安装了 OpenSSH。如果你不安装它。设置防火墙例外。然后将您的用户和 IP 拉到一起,并将其提供给主机以供其他人加入。

然后加入者使用该 ip 输入它并连接到主机并共享他们的文件。

我面临的问题:

  • 通过这种方法只能在局域网中连接。如果需要在路由器中完成端口转发并且无法在 PowerShell 中编写脚本,则无法完成,因为无法期望用户执行此操作。

  • 当通过 SSH 连接时,脚本停止。这是因为 SSH 打开了一个新的命令提示符,需要将其放回 PowerShell。我该如何克服?

  • 传输文件时 计算机之间的数据启动。我需要每隔一段时间检查一次数据更新,看看其他玩家是否做了任何事情。好像轮到他们了,现在是我的了。我怎么做这样的支票?

我对 PowerShell 非常陌生,我知道它的预期目的不是制作游戏。这一切都是为了让我以一种有趣的方式学习它的能力。如果做不到,就做不到。我知道我现在要求分配。我只需要一些指针,因为我没有其他人可以谈论这个,因为没有人了解我工作的 PowerShell。先谢谢大家

这是我目前关于 SSH 的代码。请记住,我在这方面根本不是最好的

new-Item -Path 'C:\Program Files' -Name "CardZdata" -ItemType directory
$path = 'C:\Program Files\CardZdata'
$hostippath = ($path + '\' + 'hostip' + '.xml')


    #Main Menu
    clear
    Write-Output 'Gamemode Selection'
    Write-Output '-------------------'
    Write-Output 'Singleplayer     [s]'
    Write-Output 'Multiplayer SSH  [m]'
    Write-output 'Multiplayer LAN  [l]'
    
    $gamemode = read-host
    if ($gamemode -eq 's')
        {
        clear
        Write-Output 'Gamemode Selection'
        Write-Output '-------------------'
        Write-Output 'Blackjack    [b]'
        Write-Output 'Poker        [p]'
        }
    elseif ($gamemode -eq 'm')
        {
        clear
        $test = Get-WindowsCapability -Online | ? Name -like 'OpenSSH.Client*'
        if ($test.State -eq "NotPresent") 
            {
            Write-Output 'SSH (Client) is not installed on your PC. it is needed to connect to the server.'
            Write-Output 'do you wish to install it? [y/n]'
            $SSHchoice = Read-Host
            if ($SSHchoice = 'y')
                {
                # Install the OpenSSH Client
                Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
                }
            else
                {
                Write-Output 'Sorry, you cant play Multiplayer without SSH.'
                break
                }
                $test = Get-WindowsCapability -Online | ? Name -like 'OpenSSH.Client*'
            if ($test.State -eq "NotPresent") 
                {
                Write-Output "Error - Installation Failed"
                break
                }
                
            }
    
        clear
        Write-Output 'Multiplayer SSH connects you to games hosted by others. or you can host them yourself.'
        Write-Output 'Uninstall SHH client [u] !Warning!- You wont be able to connect to Multiplayer servers'
        Write-Output ''
        Write-Output 'Gamemode Selection'
        Write-Output '-------------------'
        Write-Output 'Host         [h]'
        Write-Output 'Join         [j]'
        $hj = Read-Host
        if ($hj -eq 'h')
        {
        $test = Get-WindowsCapability -Online | ? Name -like 'OpenSSH.Server*'
        if ($test.State -eq "NotPresent") 
            {
            Write-Output 'SSH (Server) is not installed on your PC. it is needed to connect to the server.'
            Write-Output 'do you wish to install it? [y/n]'
            $SSHchoice = Read-Host
            if ($SSHchoice = 'y')
                {
                # Install the OpenSSH Client
                Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
                }
                $test = Get-WindowsCapability -Online | ? Name -like 'OpenSSH.Server*'
            if ($test.State -eq "NotPresent") 
                {
                Write-Output "Error - Installation Failed"
                break
                }
            else
                {
                Write-Output 'Sorry, you cant Host Multiplayer without the SSH Server.'
                break
                }
            }
            #StartMultiplayer
            Write-Output '!Starting Multiplayer Server On Local Host!'
            Start-Service sshd
            Set-Service -Name sshd -StartupType 'Automatic'
            $test = Get-NetFirewallRule -Name sshd
            if ($test.Name -ne "sshd")
                {
                Write-Output 'Setting Port'
                New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
                }
            Restart-Service sshd
            Write-Output 'Server Set Up Successfully!'
            $userip = (Get-NetIPAddress -AddressFamily IPv4 -SuffixOrigin Dhcp).IPAddress
            Write-Output 'Give this to users:'
            $hostip = ($env:UserName + "@" + $userip)
            $hostip
            $hostip | export-clixml $hostippath -force
            Read-Host
          }
          if ($hj -eq 'j')
              {
              Write-Output 'Join Server'
              Write-Output '-------------------'
              Write-Output 'Server list      [s] - WorkInProgress'
              Write-Output 'Direct Connect   [d]'
              $sd = Read-Host
              if ($sd -eq 's')
                  {
                  Write-Output 'why?'
                  Read-Host
                  }
              if ($sd -eq 'd')
                  {
                  Write-Output 'Enter a code that is given by the Host'
                  Write-Output 'Example: [username@ipaddress]'
                  $hostip = read-host
                  $hostip | export-clixml $hostippath -force
                  
                  }
              }
        
        Read-Host
        }
    elseif ($gamemode -eq 'l')
        {
        clear
        Write-Output 'Gamemode Selection'
        Write-Output '-------------------'
        }

这里是主要区域:

#StartMultiplayer
Write-Output '!Starting Multiplayer Server On Local Host!'
Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'
$test = Get-NetFirewallRule -Name sshd
if ($test.Name -ne "sshd")
    {
    Write-Output 'Setting Port'
    New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
    }
Restart-Service sshd
Write-Output 'Server Set Up Successfully!'
$userip = (Get-NetIPAddress -AddressFamily IPv4 -SuffixOrigin Dhcp).IPAddress
Write-Output 'Give this to users:'
$hostip = ($env:UserName + "@" + $userip)
$hostip
$hostip | export-clixml $hostippath -force
Read-Host

0 个答案:

没有答案
相关问题