首页 > 解决方案 > 通过 paramiko 自动化到多个 SSH

问题描述

我对 Paramiko 图书馆很陌生。

我有一个文本文件,其中包含 [ipaddress1, string1], [ipaddress2, string2], [ipaddress3, string3] 的列表。每个 ipaddress 都是它背后的 RPi 服务器。

我想与每个 IP 地址建立 SSH 连接并将字符串复制到位于相应 RPi 中的文件中。

所以基本上,例如“abc”的字符串应该写入RPi(/home/pi/)内的文件xyz.yaml。

我如何进行这种特定的自动化。获得支持会很棒。

以下是连接到一台服务器的代码片段,如何更改以同时连接到多台服务器并执行上述任务:

    import sys
import time
import paramiko
import getpass

my_id = "id"
my_password = "pass"
port = "17455"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
in_file = []
out_file = open('connection_results.txt', 'w')
in_file = open('list_of_servers.txt', 'r')
print(in_file)
for server in in_file:
    print("first host", server)
    hosts = server.split(',')
    for host in hosts:
        print("Checking server", host)
        time.sleep(3)
        try:
            print(host, my_id, my_password, port)
            ssh.connect(hostname=host, username=my_id,
    

                password=my_password, port=port)

        terminal = ssh.invoke_shell()
        # terminal.send('junk')
        terminal.send('\n')
        time.sleep(2)
        output = terminal.recv(10240)
        # print(output)

        command = 'hostname'

        (stdin, stdout, stderr) = ssh.exec_command(command)

        for line in stdout.readlines():
            print("Connected to", line)
            out_file.write("connected to " + line + "\n")

        terminal.send('exit')
        terminal.send('\n')
        time.sleep(2)

        ftp = ssh.open_sftp()

        with ftp.open('/home/men/testingnew.py', 'r+') as file:
            read_file = file.read()
            read_file = read_file.replace(
                "unique_id", "AZUre_IOT_adsfdfdf")
            print("File read")
        with ftp.open('/home/men/testingnew.py', "w") as file:
            file.write(read_file)
            
            print('Written to file')
            f.flush()
            ftp.close()
            ssh.close()

    except:
        out_file.write("Could not connect to " + host + "\n")


in_file.close()
out_file.close()

标签: pythonpython-3.xautomationparamiko

解决方案


我在阅读文件后通过添加 decode 和 rstrip 解决了这个问题:

import sys
import time
import paramiko
import getpass

my_id = "id"
my_password = "pass"
port = "17455"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
in_file = []
out_file = open('connection_results.txt', 'w')
in_file = open('list_of_servers.txt', 'r')
print(in_file)
for server in in_file:
    print("first host", server)
    hosts = server.split(',')
    for host in hosts:
        print("Checking server", host)
        time.sleep(3)
        try:
            print(host, my_id, my_password, port)
            ssh.connect(hostname=host, username=my_id,
    

            password=my_password, port=port)

    terminal = ssh.invoke_shell()
    # terminal.send('junk')
    terminal.send('\n')
    time.sleep(2)
    output = terminal.recv(10240)
    # print(output)

    command = 'hostname'

    (stdin, stdout, stderr) = ssh.exec_command(command)

    for line in stdout.readlines():
        print("Connected to", line)
        out_file.write("connected to " + line + "\n")

    terminal.send('exit')
    terminal.send('\n')
    time.sleep(2)

    ftp = ssh.open_sftp()

    with ftp.open('/home/men/testingnew.py', 'r+') as file:
        read_file = file.read().decode('utf-8').rstrip('/n')

        read_file = read_file.replace(
            "unique_id", "AZUre_IOT_adsfdfdf")
        print("File read")
    with ftp.open('/home/men/testingnew.py', "w") as file:
        file.write(read_file)
        
        print('Written to file')
        f.flush()
        ftp.close()
        ssh.close()

except:
    out_file.write("Could not connect to " + host + "\n")


in_file.close()
out_file.close()

推荐阅读