通过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()

解决方法

读取文件后,我通过添加解码和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()