python多進(jìn)程登錄遠(yuǎn)端服務(wù)器
通過Semaphore 來控制對共享資源的的訪問數(shù)量,可以控制同一時刻并發(fā)的進(jìn)程數(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 + " 獲得鎖運(yùn)行");
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個進(jìn)程 for n in range(111): p = multiprocessing.Process(target = ssh, args=(s,2,"192.168.0."+str(n))) p.start()
運(yùn)行結(jié)果如下圖:

到此這篇關(guān)于python多進(jìn)程登錄遠(yuǎn)端服務(wù)器的文章就介紹到這了,更多相關(guān)多進(jìn)程 Python內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python從ZabbixAPI獲取信息及實現(xiàn)Zabbix-API 監(jiān)控的方法
這篇文章主要介紹了Python從ZabbixAPI獲取信息及實現(xiàn)Zabbix-API 監(jiān)控的方法,需要的朋友可以參考下2018-09-09
PyCharm安裝庫numpy失敗問題的詳細(xì)解決方法
今天使用pycharm編譯python程序時,由于要調(diào)用numpy包,但又未曾安裝numpy,于是就根據(jù)pycharm的提示進(jìn)行安裝,最后竟然提示出錯,下面這篇文章主要給大家介紹了關(guān)于PyCharm安裝庫numpy失敗問題的詳細(xì)解決方法,需要的朋友可以參考下2022-06-06
5分鐘快速掌握Python定時任務(wù)框架的實現(xiàn)
python使用PySimpleGUI設(shè)置進(jìn)度條及控件使用
Python基于火山引擎豆包大模型搭建QQ機(jī)器人詳細(xì)教程(2024年最新)

