【问题标题】:python - running multiple commands with subprocess over sshpython - 通过 ssh 使用子进程运行多个命令
【发布时间】:2018-09-13 18:10:38
【问题描述】:

我正在尝试打开一个子进程以通过我的服务器上的 SMTP 协议发送电子邮件。为此,我需要一个 TLS 安全通道,因此我需要 SSL 模块。我没有成功,因为我无法通过子进程通过 openssl 进行交互。

这就是我设想我的脚本需要如何工作的方式。

1. 打开 ssl 连接:openssl s_client -connect smtp.server.com:587 -starttls smtp

1.1. 通过此频道通信登录:AUTH PLAIN ENCODEDLOGINSTRING==

1.2. 通过此渠道通信发送邮件:MAIL FROM:myemail@server.com

我的脚本不应该返回任何东西,除了“邮件已发送”。

我该如何做到这一点?

注意:smtplib 在我的作业中使用是非法的。

【问题讨论】:

    标签: python email ssh smtp subprocess


    【解决方案1】:

    我发现最好为此使用套接字(不是子进程),然后使用命令“STARTTLS”,然后用 ssl 包装套接字以获得安全的 tls 连接。之后,您可以继续将 rcpt 发送到: 命令等,并通过套接字发送实际邮件。

    旁注:我在一个子进程中使用了这个脚本,我需要它来打印 html 格式的东西;但这是我成功的代码。如果你打算使用它,你需要先设置和格式化(使用换行符和东西(以便 smtp 服务器理解))这些变量:发件人、收件人、主题、正文、服务器、用户名、密码和端口

    # check if server is valid
    try:
        socket.gethostbyname(server)
    except socket.gaierror:
        print("""
        <p>
        Something seems wrong with the server...
        </p>
        """)
        exit()
    
    # make a connection with the server
    mailserver = (server, port)
    socket.mailsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    socket.mailsocket.connect(mailserver)
    step1 = socket.mailsocket.recv(1024)
    step1 = step1.decode()
    if (step1[:3] != '220'):
        print("""
        <p>
        Something seems wrong with the server...
        </p>
        """)
        exit()
    
    # use tls connection with server
    starttls = "STARTTLS" + "\r\n"
    socket.mailsocket.send(starttls.encode())
    step2 = socket.mailsocket.recv(1024)
    step2 = step2.decode()
    if (step2[:3] != '220'):
        print("""
        <p>
        Something seems wrong with the TLS connection...
        </p>
        """)
        exit()
    
    # now use SSL
    ssl = ssl.wrap_socket(socket.mailsocket, ssl_version=ssl.PROTOCOL_TLSv1_2)
    socket.mailsocket = ssl
    
    # set username and password and encode
    base64_str = ("\x00"+username+"\x00"+password).encode()
    base64_str = base64.b64encode(base64_str)
    auth = "AUTH PLAIN ".encode()+base64_str+"\r\n".encode()
    socket.mailsocket.send(auth)
    step3 = socket.mailsocket.recv(1024)
    step3 = step3.decode()
    if (step3[:3] != '235'):
        print("""
        <p>
        It seems like your username/password combination is incorrect...
        </p>
        """)
        exit()
    
    # set sender address
    sender = "mail from: " + sender + "\r\n"
    socket.mailsocket.send(sender.encode())
    step4 = socket.mailsocket.recv(1024)
    step4 = step4.decode()
    if (step4[:3] != '250'):
        print("""
        <p>
        It seems like the sender address is incorrect...
        </p>
        """)
        exit()
    
    # set to address
    to = "rcpt to: " + to + "\r\n"
    socket.mailsocket.send(to.encode())
    step5 = socket.mailsocket.recv(1024)
    step5 = step5.decode()
    if (step5[:3] != '250'):
        print("""
        <p>
        It seems like the destination address is incorrect...
        </p>
        """)
        exit()
    
    # send data command
    data = "data\r\n"
    socket.mailsocket.send(data.encode())
    step6 = socket.mailsocket.recv(1024)
    step6 = step6.decode()
    if (step6[:3] != '354'):
        print("""
        <p>
        It seems like the server responds different on the DATA command...
        </p>
        """)
        exit()
    
    # send message subject and body
    socket.mailsocket.send(subject.encode())
    socket.mailsocket.send(body.encode())
    step7 = socket.mailsocket.recv(1024)
    step7 = step7.decode()
    if (step7[:3] != '250'):
        print("""
        <p>
        It seems like there is something wrong with your message body...
        </p>
        """)
        exit()
    else:
        print("""
        <p>
        Your message is queued and will sent as soon as possible!
        </p>
        """)
    quit = "QUIT\r\n"
    socket.mailsocket.send(quit.encode())
    
    step8 = socket.mailsocket.recv(1024)
    step8 = step8.decode()
    if (step8[:3] == '221'):
        print("""
        <p>
        Closing connection; bye!
        </p>
        """)
    socket.mailsocket.close()
    

    【讨论】:

      猜你喜欢
      • 2013-11-22
      • 2018-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-08
      • 2011-01-25
      • 2013-07-18
      相关资源
      最近更新 更多