添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

paramiko.ssh_exception.SSHException。读取SSH协议横幅时出错[Errno 104] 连接被对方inpyhton重置了

0 人关注

在列表中给出超过19条记录,并通过给定的sftp连接细节将其传递到循环中,但在运行19条记录后出现了错误。以下是我的代码。

import paramiko
import sys, os
import time 
lsit1 = ["CBCGG1900089HM", "CBCGG1900090HM", "CBCGG1900091HM", "CBCGG1900092HM", "CBCGG1900093HM", "CBCGG1900094HM", "CBCGG1900095HM", "CBCGG1900096HM", "CBCGG1900097HM", "CBCGG1900098HM", "CBCGG1900099HM", "CBCGG1900100", "CBCGG1900101HM", "CBCGG1900102HM", "CBCGG1900103HM", "CBCGG1900104HM", "CBCGG1900105HM", "CBCGG1900106HM", "CBCGG1900108HM", "CBCGG1900109HM", "CBCGG1900112HM", "CBCGG1900116HM", "CBCGG1900120HM", "CBCGG1900124HM", "CBCGG1900128HM", "CBCGG1900130HM", "CBCGG1900131HM", "CBCGG1900132HM", "CBCGG1900136HM", "CBCGG1900142HM", "CBCGG1900149HM", "CBCGG1900151HM", "CBCGG1900153", "CBCGG1900161", "CBDGG1900001", "BCYG1800038", "BCYG1800041", "BCYG1800045", "BCYG1800047", "BCYG1800049"]
col_id = []
def sftpfun(i):
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(hostname='abc.com',timeout=6000,username='abc',password='xxbb')
        ftp = ssh.open_sftp()
        print("bc")
        ftp.get(f'/CT_Logs/Optima660_Beijing/{i}/image_analysis.csv', f'/home/hvr/sftp_python/Optima660_Beijing {i}/image_analysis.csv')
        ssh.close()
    except:
        col_id.append(f"Optima660_Beijing/{i}")
        with open(f"/home/hvr/sftp_python/logs/sftp_logs.txt", 'w') as f:
            f.write(i)
for i,j in zip(lsit1,range(len(lsit1))):
    sftpfun(i)
    if j%15==0:
        time.sleep(15)
    
5 个评论
请澄清你的具体问题,或提供额外的细节,以强调你所需要的确切内容。按照目前的写法,很难说清楚你的要求是什么。
@Community my connection is disconnected every 19seconds
AKX
首先,如果出现错误,你没有关闭SSH连接,所以当你保持连接开放时,远程主机有可能开始阻止你。你还使用了一个裸的 except: ,所以很难知道实际的错误是什么。
AKX
第二,我当然希望那个主机名、用户名和密码不是真正的凭证,因为你刚刚把它们泄露给了互联网。
AKX
还有:为什么你要为列表中的每个项目抓取一个新的SSH连接?
python
python-3.6
sftp
paramiko
Thentu matcha
Thentu matcha
发布于 2022-08-21
1 个回答
AKX
AKX
发布于 2022-08-22
已采纳
0 人赞同

你可以使用单一的SSH/SFTP连接。

import time
import paramiko
items = [
    "CBCGG1900089HM",
    "CBCGG1900090HM",
    "CBCGG1900091HM",
    "CBCGG1900092HM",
    "CBCGG1900093HM",
    # ...
    "BCYG1800049",
with paramiko.SSHClient() as ssh:
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(
        hostname="amecoll01.logon.ds.ge.com",
        timeout=6000,
        username="WSO",
        password="#AMEuser",
    ftp = ssh.open_sftp()
    for i, item in enumerate(items):
        src = f"/CT_Logs/Optima660_Beijing/{item}/image_analysis.csv"
        dest = f"/home/hvr/sftp_python/Optima660_Beijing {item}/image_analysis.csv"
            ftp.get(src, dest)
            print("OK", dest)
        except Exception as e: