python多進程登錄遠端服務器
更新時間:2021年10月25日 14:24:05 作者:feikeyan
這篇文章主要介紹了python多進程登錄遠端服務器,文章應用實例簡易的方式詳細講解python多進程登錄遠端服務器的相關資料,需要的朋友可以參考以下文章的具體內(nèi)容
通過Semaphore 來控制對共享資源的的訪問數(shù)量,可以控制同一時刻并發(fā)的進程數(shù) 。
#/usr/bin/python # _*_ coding: utf-8 _*_ import multiprocessing import time import paramiko def ssh(s,i,host):
try:
s.acquire()
print(time.strftime('%H:%M:%S'),multiprocessing.current_process().name + " 獲得鎖運行");
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=host, port=22, username="root", password="yankefei")
print (host+" is login success")
stdin, stdout, stderr = ssh.exec_command("echo
d
a
t
e
&& df -hl")
print(stdout.read().decode('utf-8'))
returncode = stdout.channel.recv_exit_status()
print("returncode:",returncode)
except:
ssh.close()
# time.sleep(i)
print(time.strftime('%H:%M:%S'),multiprocessing.current_process().name + " 釋放鎖結(jié)束");
s.release()
print (host+" is unreachable")
finally:
ssh.close() s.release() if __name__ == "__main__": s = multiprocessing.Semaphore(200) #同時并發(fā)200個進程 for n in range(111): p = multiprocessing.Process(target = ssh, args=(s,2,"192.168.0."+str(n))) p.start()
運行結(jié)果如下圖:

到此這篇關于python多進程登錄遠端服務器的文章就介紹到這了,更多相關多進程 Python內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python 3.6打包成EXE可執(zhí)行程序的實現(xiàn)
這篇文章主要介紹了Python 3.6打包成EXE可執(zhí)行程序的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10
基于K.image_data_format() == ''channels_first'' 的理解
這篇文章主要介紹了基于K.image_data_format() == 'channels_first' 的理解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Python實現(xiàn)隨機森林RF模型超參數(shù)的優(yōu)化詳解
這篇文章主要為大家詳細介紹了基于Python的隨機森林(Random Forest,RF)回歸代碼,以及模型超參數(shù)(包括決策樹個數(shù)與最大深度、最小分離樣本數(shù)、最小葉子節(jié)點樣本數(shù)、最大分離特征數(shù)等)自動優(yōu)化的代碼,感興趣的小伙伴可以了解一下2023-02-02

