脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python多进程登录远端服务器

python多进程登录远端服务器

2022-02-17 13:29feikeyan Python

这篇文章主要介绍了python多进程登录远端服务器,文章应用实例简易的方式详细讲解python多进程登录远端服务器的相关资料,需要的朋友可以参考以下文章的具体内容

通过Semaphore 来控制对共享资源的的访问数量,可以控制同一时刻并发的进程数 。

?
1
2
3
4
5
6
7
8
9
10
11
#/usr/bin/python
 
# _*_ coding: utf-8 _*_
 
import multiprocessing
 
import time
 
import paramiko
 
def ssh(s,i,host):

try:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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:

?
1
2
3
4
5
6
7
8
9
ssh.close()
 
# time.sleep(i)
 
print(time.strftime('%H:%M:%S'),multiprocessing.current_process().name + " 释放锁结束");
 
s.release()
 
print (host+" is unreachable")

finally:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
ssh.close()
 
s.release()
 
if __name__ == "__main__":
 
s = multiprocessing.Semaphore(200) #同时并发200个进程
 
for n in range(111):
 
p = multiprocessing.Process(target = ssh, args=(s,2,"192.168.0."+str(n)))
 
p.start()

运行结果如下图:

python多进程登录远端服务器

到此这篇关于python多进程登录远端服务器的文章就介绍到这了,更多相关多进程 Python内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.tuicool.com/articles/M3iQVju

延伸 · 阅读

精彩推荐