python多進程登錄遠端服務器
發(fā)布日期:2021-12-20 10:28 | 文章來源:CSDN
通過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 + " 釋放鎖結束");
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()
運行結果如下圖:

到此這篇關于python多進程登錄遠端服務器的文章就介紹到這了,更多相關多進程 Python內容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持本站!
版權聲明:本站文章來源標注為YINGSOO的內容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內容來源于網友推薦、互聯(lián)網收集整理而來,僅供學習參考,不代表本站立場,如有內容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。
相關文章
關注官方微信