使用多线程通过跳跃发送多个命令
我创建了一个脚本,在其中将15000个命令发送到70个开关,因此每个交换机约为200个命令。 对于每个交换机,我通过Jumpssh(通过跳转服务器)创建一个SSH连接:建立
from jumpssh import SSHSession
# establish ssh connection between your local machine and the jump server
gateway_session = SSHSession('gateway.example.com',
'my_user', password='my_password').open()
# from jump server, establish connection with a remote server
remote_session = gateway_session.get_remote_session('remote.example.com',
password='my_password2')
连接后,我将使用MultineReading发送200个命令:
def main_loop():
for switch in result.keys():
gateway_session = SSHSession('gateway.example.com',
'my_user', password='my_password').open()
remote_session = gateway_session.get_remote_session(switch["ip"],
password='my_password2')
with ThreadPoolExecutor(max_workers=8) as executor:
for command in result[switch]["commands"]:
executor.submit(launch_command, command, remote_session)
def launch_command(command, remote_session):
process = remote_session.get_cmd_output(command)
问题是,如果我使用7或8个工人,则是无法正常工作(我丢失了一些信息),而且我会遇到此错误:
secsh channel 10 open FAILED: open failed: Connect failed
secsh channel 123 open FAILED: open failed: Connect failed
secsh channel 42 open FAILED: open failed: Connect failed
secsh channel 76 open FAILED: open failed: Connect failed
secsh channel 12 open FAILED: open failed: Connect failed
secsh channel 55 open FAILED: open failed: Connect failed
...
如果我使用的6名工人或更少,我没有任何错误,一切正常。
我的印象是它每叶创建多个SSH连接,因此这就是为什么存在错误的原因,因为也许我达到了交换机中允许的最大会话数,但是根据我的代码,每个交换机只有一个连接是设置。
也许有人遇到了同样的问题?
谢谢你,
I created a script where I send 15 000 commands to 70 switchs, so around 200 commands per switch.
For each switch, I create one SSH connection via jumpSSH (via a jump server) this way:
from jumpssh import SSHSession
# establish ssh connection between your local machine and the jump server
gateway_session = SSHSession('gateway.example.com',
'my_user', password='my_password').open()
# from jump server, establish connection with a remote server
remote_session = gateway_session.get_remote_session('remote.example.com',
password='my_password2')
Once the connection is established, I'm sending 200 commands using multithreading:
def main_loop():
for switch in result.keys():
gateway_session = SSHSession('gateway.example.com',
'my_user', password='my_password').open()
remote_session = gateway_session.get_remote_session(switch["ip"],
password='my_password2')
with ThreadPoolExecutor(max_workers=8) as executor:
for command in result[switch]["commands"]:
executor.submit(launch_command, command, remote_session)
def launch_command(command, remote_session):
process = remote_session.get_cmd_output(command)
The problem is that if I'm using 7 or 8 workers, it's not working properly (some information I get is missing) and I'm getting this error:
secsh channel 10 open FAILED: open failed: Connect failed
secsh channel 123 open FAILED: open failed: Connect failed
secsh channel 42 open FAILED: open failed: Connect failed
secsh channel 76 open FAILED: open failed: Connect failed
secsh channel 12 open FAILED: open failed: Connect failed
secsh channel 55 open FAILED: open failed: Connect failed
...
If I'm using 6 workers or less, I have no any error and everything is working correctly.
I have the impression that it's creating more than one SSH connection per leaf and so that's why there is this error, because maybe I'm reaching the number max of sessions allowed in switches, but according to my code, only one connection per switch is set up.
Maybe someone experienced the same issue?
Thank you,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论