使用python备份路由器配置

时间:2017-05-30 11:14:09

标签: python python-2.7 paramiko cisco cisco-ios

我正在使用python和paramiko。 我试图使用python备份网络设备路由器。下面是我的脚本。 但是我得到错误

try {
            JSONObject jsonObject = new JSONObject(response);
            JSONArray posts = jsonObject.getJSONArray("posts");
            for(int i=0 ; i<posts.length() ; i++) {
                JSONObject fileObj = posts.getJSONObject(i);
                String fName = fileObj.getString("firstname");
                String created_at = fileObj.getString("created_at");
                String post_desc =  fileObj.getString("post_desc");

                Log.e("Details",fName+""+created_at+""+post_desc);

                JSONArray files = fileObj.getJSONArray("files");
                if(!files.isEmpry()){
                    for(int j=0 ; j<files.length() ; j++)
                    {
                        JSONObject Jsonfilename = files.getJSONObject(j);
                        String filename = Jsonfilename.getString("file_name");
                    }    
                    }else{
                            //what you want to do
                 }

            }

        }catch (Exception e)
        {
            Log.e("Error",""+e);
        }

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法;我看到exec_command()对某些路由器失败了,但我无法确保它适合您的问题:

import paramiko
import sys
import time

HOST = "10.11.214.143"
USER = "admin"
PASS = "passwd"


client1=paramiko.SSHClient()
client1.set_missing_host_key_policy(paramiko.AutoAddPolicy())

client1.connect(HOST,username=USER,password=PASS)
print "SSH connection to %s established" %HOST

console = client1.invoke_shell()
console.recv(65535)

console.send("copy nvram:startup-config tftp: 10.11.214.144\n")

# Adjust sleep as it's needed by your command
time.sleep(.5)
console.recv(65535)

# Eventually quit gracefully with the right command
console.send("quit\n")
time.sleep(.5)

client1.close()