Python Paramiko E公司

2024-04-25 19:41:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个python程序。我有conf文件,我在conf文件中写了路由器配置命令,我想在paramiko中执行这些命令。我有一个问题-错误信息如下。你能帮帮我吗?

代码:

#!/usr/bin/env python
import paramiko
ip="10.100.1.200"
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,username="admin",password="pass")

text=open("conf")

for komut in text.readlines():

        stdin, stdout, stderror = ssh.exec_command(komut)
        for line in stdout.readlines():
                        print line.strip()


ssh.close()

text.close()

错误消息:

^{pr2}$

Tags: 文件textin命令程序ipparamikofor
2条回答

尝试使用以下代码:

import paramiko

if __name__ == "__main__":
    ip = "127.0.0.1"
    username = "admin"
    password = "root"

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(ip,username=username,password=password)
    ssh_transport = ssh.get_transport()

    for command in ("ls /tmp", "date"):
        chan = ssh_transport.open_session()
        chan.exec_command(command)
        exit_code = chan.recv_exit_status()
        stdin = chan.makefile('wb', -1)         # pylint: disable-msg=W0612
        stdout = chan.makefile('rb', -1)
        stderr = chan.makefile_stderr('rb', -1)  # pylint: disable-msg=W0612
        output = stdout.read()
        print output

尝试以下代码,首先将conf文件复制到远程服务器并执行该文件。在

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
host = str(host)
user = 'root'
pw = 'root123'
ssh.connect(host, username=user, password=pw)

filepath = "conf"
remote_path = "/root/qats_tool.tar"

sftp = ssh.open_sftp()
sftp.put(filepath, remote_path)

command="./conf"
stdin, stdout, stderr = ssh.exec_command(command)
sftp.close()

相关问题 更多 >