V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Achilless
V2EX  ›  问与答

关于 paramiko 多线程 ssh

  •  
  •   Achilless · 2019-10-15 23:25:49 +08:00 · 1036 次点击
    这是一个创建于 1648 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我现在有 4000 台主机要定时检测他们的 ssh 可达性,思路是 paramiko+线程池并发检测,结果入库。

    但是现在遇到个很奇怪的现象,无论线程池设置为 5 或 50,最后的结果都是 100 到 300 之间的数量,根本才执行了 10%都不到。

    但是我用差不多的代码推送配置的时候倒是没这问题。

    代码在这: http://www.ksprd.top/blog/45/

    请问是哪里写的有问题么

    6 条回复    2019-10-22 13:08:09 +08:00
    Achilless
        1
    Achilless  
    OP
       2019-10-15 23:58:13 +08:00
    搞定了,智障了,没关 ssh
    Achilless
        2
    Achilless  
    OP
       2019-10-15 23:59:44 +08:00
    现在有另一个顽疾,一直没解决,最后跑完的结果,根据线程池的大小,结果是不一样的。比如 60 个线程的时候,结果是 3300,50 个线程的时候结果是 3000.。。。真要哭了,是 ssh 的坑么
    ps1aniuge
        3
    ps1aniuge  
       2019-10-16 00:16:45 +08:00
    win,linux,加 powershell 7 preview 版,若是 linux 则用 sshpass+ssh。

    $a = get-content ip.txt
    $a | ForEach-Object -ThrottleLimit 50 -Parallel {
    # sshpass ssh ip 等
    }

    powershell [强] [简] 死你!
    =========安装==========
    你需要把 powershell 改成 powershell-preview,即安装非稳定版的 powershell。

    centos7 及以上,安装 powershell:
    curl -o /etc/yum.repos.d/microsoft.repo https://packages.microsoft.com/config/rhel/7/prod.repo
    sudo yum remove -y powershell #删除旧版
    sudo yum install -y powershell
    pwsh -c 'mkdir -p "$env:HOME/.config/powershell" '
    pwsh -c 'Add-Content -Value "Set-PSReadlineOption -EditMode Windows" -LiteralPath $profile '
    pwsh -c 'Add-Content -Value "`nSubsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile" -LiteralPath /etc/ssh/sshd_config '



    ubuntu1604:
    sudo curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
    sudo curl -o /etc/apt/sources.list.d/microsoft.list https://packages.microsoft.com/config/ubuntu/16.04/prod.list
    sudo apt-get update
    sudo apt-get remove -y powershell #删除旧版
    sudo apt-get install -y powershell
    pwsh -c 'mkdir -p "$env:HOME/.config/powershell" '
    pwsh -c 'Add-Content -Value "Set-PSReadlineOption -EditMode Windows" -LiteralPath $profile '
    pwsh -c 'Add-Content -Value "`nSubsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile" -LiteralPath /etc/ssh/sshd_config '


    ubuntu1804:
    sudo curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
    sudo curl -o /etc/apt/sources.list.d/microsoft.list https://packages.microsoft.com/config/ubuntu/18.04/prod.list
    sudo apt-get update
    sudo apt-get remove -y powershell #删除旧版
    sudo apt-get install -y powershell
    pwsh -c 'mkdir -p "$env:HOME/.config/powershell" '
    pwsh -c 'Add-Content -Value "Set-PSReadlineOption -EditMode Windows" -LiteralPath $profile '
    pwsh -c 'Add-Content -Value "`nSubsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile" -LiteralPath /etc/ssh/sshd_config '



    debian9:
    sudo apt-get update
    sudo apt-get install curl gnupg apt-transport-https
    curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
    sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-stretch-prod stretch main" > /etc/apt/sources.list.d/microsoft.list'
    sudo apt-get update
    sudo apt-get remove -y powershell #删除旧版
    sudo apt-get install -y powershell
    pwsh -c 'mkdir -p "$env:HOME/.config/powershell" '
    pwsh -c 'Add-Content -Value "Set-PSReadlineOption -EditMode Windows" -LiteralPath $profile '
    pwsh -c 'Add-Content -Value "`nSubsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile" -LiteralPath /etc/ssh/sshd_config '




    =========资料==========

    ┏┳━━━━━━━━━━━┓
    ┃┃███████████┃
    ┃┃███████████┃
    ┣┫███████┏━┓█┃
    ┃┃███████┃P┃█┃
    ┃┃███████┃O┃█┃
    ┃┃███秘███┃W┃█┃
    ┣┫███████┃E┃█┃
    ┃┃███████┃R┃█┃
    ┃┃███████┃S┃█┃
    ┃┃███████┃H┃█┃
    ┣┫███████┃E┃█┃
    ┃┃███籍███┃L┃█┃
    ┃┃███████┃L┃█┃
    ┃┃███████┗━┛█┃
    ┣┫███████████┃
    ┃┃██V7.0 preview3 ██┃
    ┃┃███████████┃
    ┗┻━━━━━━━━━━━┛

    新增并发参数
    | ForEach-Object -Parallel

    例子:
    1
    get-content ip.txt |foreach-object -Parallel{ ping.exe $_ }

    2
    Get-Content /powershell/ip.txt |ForEach-Object -Parallel {if (Test-Connection -Count 1 -TargetName $_ -Quiet) {$_} } 6>$null

    3
    $a = 1..100
    $a | ForEach-Object -ThrottleLimit 5 -Parallel {
    Write-Host $_
    Start-Sleep -Seconds 3
    }


    手册:
    https://github.com/PowerShell/PowerShell-RFC/blob/master/4-Experimental-Accepted/RFC0044-ForEach-Parallel-Cmdlet.md

    参数:
    -Parallel
    -ThrottleLimit
    -TimeoutSeconds parameter takes an integer that specifies the maximum time to wait for completion before the command is aborted
    -AsJob
    ly4572615
        4
    ly4572615  
       2019-10-16 09:24:27 +08:00
    你们搞得太麻烦了,ansible 了解一下?
    k8ser
        5
    k8ser  
       2019-10-16 09:55:16 +08:00
    你网站挂了
    ps1aniuge
        6
    ps1aniuge  
       2019-10-22 13:08:09 +08:00
    $a = get-content ip.txt
    $a | ForEach-Object -ThrottleLimit 50 -Parallel {
    sshpass -p ‘xxx’ ssh user@ip #详细命令略
    if ($LASTEXITCODE -ne 0)
    {'xx'} else {'yy'}
    }

    talk is cheap ,show me code .
    在安装 bash,python,powershell 的情况下,还有比这更简单的代码么?你倒是贴出来呀。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2903 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 13:26 · PVG 21:26 · LAX 06:26 · JFK 09:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.