{"id":1740,"date":"2020-02-12T17:02:00","date_gmt":"2020-02-12T17:02:00","guid":{"rendered":"https:\/\/www.balajibandi.com\/?p=1740"},"modified":"2022-10-21T17:08:32","modified_gmt":"2022-10-21T16:08:32","slug":"cisco-router-backup-using-python","status":"publish","type":"post","link":"https:\/\/www.balajibandi.com\/?p=1740","title":{"rendered":"Cisco router backup using Python"},"content":{"rendered":"\n<p>here is simple script to use paramiko<\/p>\n\n\n\n<p>i used linux box<\/p>\n\n\n\n<p>vi mydeviceback.py ( add below conntent)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sys\nimport time\nimport paramiko \nimport os\nimport cmd\nimport datetime\n\n#set date and time\nnow = datetime.datetime.now()\n\n#authentication\nUSER = 'user'\nPASSWORD = 'password'\nsecret = 'password'\n\n#start FOR ...in \nf = open('cisco_devices')\nfor ip in f.readlines():\n\tip = ip.strip()\n\t#prefix files for backup\n\tfilename_prefix ='cisco_backup' + ip\n\t\n\t#session start\n\tclient = paramiko.SSHClient()\n\tclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n\tclient.connect(ip, username=USER, password=PASSWORD)\n\n\t#ssh shell\n\tchan = client.invoke_shell()\n\ttime.sleep(2)\n\t#enter enable secret\n\tchan.send('en\\n')\n\tchan.send(secret +'\\n')\n\ttime.sleep(1)\n\t#terminal lenght for no paging \n\tchan.send('term len 0\\n')\n\ttime.sleep(1)\n\t#show config and write output\n\tchan.send('sh run\\n')\n\ttime.sleep(10)\n\toutput = chan.recv(99999)\n\t#show output config and write file with prefix, date and time\n\tprint output\n\tfilename = \"%s_%.2i-%.2i-%i_%.2i-%.2i-%.2i\" % (filename_prefix,now.day,now.month,now.year,now.hour,now.minute,now.second)\n\tff = open(filename, 'a')\n\tff.write(output)\n\tff.close()\n\t#close ssh session\n\tclient.close() \n\t\n\tprint ip\n\tf.close()<\/code><\/pre>\n\n\n\n<p>save by issue :wq<\/p>\n\n\n\n<p>also add below devices list as below :<\/p>\n\n\n\n<p>cisco_devices<\/p>\n\n\n\n<p>192.168.1.1<\/p>\n\n\n\n<p>192.168.1.2<\/p>\n\n\n\n<p>save file, make sure both in same folder.<\/p>\n\n\n\n<p>run the <\/p>\n\n\n\n<p>python mydeviceback.py<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>I will be getting another blog with netmiko<\/p>\n\n\n\n<p>Happy Labiing&#8230;&#8230;&#8230;&#8230;.!  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>here is simple script to use paramiko i used linux box vi mydeviceback.py ( add below conntent) save by issue :wq also add below devices list as below : cisco_devices 192.168.1.1 192.168.1.2 save file, make sure both in same folder. run the python mydeviceback.py I will be getting another blog with netmiko Happy Labiing&#8230;&#8230;&#8230;&#8230;.!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1740","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.balajibandi.com\/index.php?rest_route=\/wp\/v2\/posts\/1740","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.balajibandi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.balajibandi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.balajibandi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.balajibandi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1740"}],"version-history":[{"count":1,"href":"https:\/\/www.balajibandi.com\/index.php?rest_route=\/wp\/v2\/posts\/1740\/revisions"}],"predecessor-version":[{"id":1741,"href":"https:\/\/www.balajibandi.com\/index.php?rest_route=\/wp\/v2\/posts\/1740\/revisions\/1741"}],"wp:attachment":[{"href":"https:\/\/www.balajibandi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.balajibandi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.balajibandi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}