{"id":86,"date":"2017-03-20T19:05:54","date_gmt":"2017-03-20T17:05:54","guid":{"rendered":"http:\/\/developer.ps\/?p=86"},"modified":"2026-09-22T20:52:17","modified_gmt":"2026-09-22T18:52:17","slug":"a-python-script-to-backup-cisco-routers-and-switches","status":"publish","type":"post","link":"https:\/\/developer.ps\/index.php\/2017\/03\/20\/a-python-script-to-backup-cisco-routers-and-switches\/","title":{"rendered":"A Python Script to Backup Cisco Routers and Switches"},"content":{"rendered":"<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">#!\/usr\/bin\/python\r\n\r\nimport pexpect,sys,string,os,time,subprocess,getpass\r\n\r\nif len(sys.argv) !=2:\r\nprint (\"usage: backup [user@]hostname\")\r\nelse:\r\ninput_data = sys.argv[1]\r\ndata=input_data.split(\"@\")\r\nswitch_un = data[0]\r\nswitch_ip = data[1]\r\nswitch_pw1 = getpass.getpass(\"Enable Password:\")\r\nswitch_pw2 = getpass.getpass(\"Privilege Password:\")\r\nbasic_backup_directory = \"\/tmp\/Network_Backups\"\r\nprint (\"Please wait ...\")\r\n\r\ntry:\r\ntry:\r\nchild = pexpect.spawn('ssh %s@%s' % (switch_un, switch_ip))\r\nchild.timeout = 10\r\nchild.expect('assword:')\r\nchild.sendline(switch_pw1)\r\nexcept pexpect.TIMEOUT:\r\nraise OurException(\"Couldn't log on to the switch\")\r\n\r\nchild.sendline('disable')\r\nchild.expect('&gt;')\r\nchild.sendline('terminal length 0')\r\nchild.expect('&gt;')\r\nchild.sendline('enable')\r\nchild.expect('Password:')\r\nchild.sendline(switch_pw2)\r\nchild.expect('#')\r\nnetwork_hostname = child.before\r\n#network_hosname contains some strange characters that should be removed\r\nj=0\r\nnetwork_hostname_2=\"\"\r\nfor i in range(len(network_hostname)):\r\nif ord(network_hostname[i]) &gt; 32:\r\nnetwork_hostname_2+=network_hostname[i]\r\n\r\nchild.sendline('show run')\r\nchild.expect(network_hostname_2+\"#\")\r\ndata= child.before\r\n\r\nexcept (pexpect.EOF,pexpect.TIMEOUT), e:\r\nerror(\"Error while trying to login.\")\r\nraise\r\n\r\nthis_date=time.strftime(\"%d%m%Y\")\r\nthis_moment=time.strftime(\"%X\")\r\nfull_backup_directory= basic_backup_directory+\"\/\"+network_hostname_2\r\n\r\n#creating a directory and then opening\/creating\/writing into a file in the specified directory.\r\nif not os.path.exists(full_backup_directory):\r\nos.makedirs(full_backup_directory)\r\nfile_name= network_hostname_2+\"_\"+this_date+\".txt\"\r\nwith open (os.path.join(full_backup_directory,file_name),'wb') as temp_file:\r\ntemp_file.write(data)\r\n\r\noutput= subprocess.check_output([\"rsync\",\"-rvzhe\",\"ssh\",full_backup_directory,\"root@192.168.XX.XX:\/mnt\/STORAGE_01\/backups\/Switches\"])\r\nprint output<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>#!\/usr\/bin\/python import pexpect,sys,string,os,time,subprocess,getpass if len(sys.argv) !=2: print (&#8220;usage: backup [user@]hostname&#8221;) else: input_data = sys.argv[1] data=input_data.split(&#8220;@&#8221;) switch_un = data[0] switch_ip = data[1] switch_pw1 = getpass.getpass(&#8220;Enable Password:&#8221;) [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,7,11],"tags":[],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-cisco","category-networking","category-python"],"_links":{"self":[{"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":2,"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"predecessor-version":[{"id":94,"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/posts\/86\/revisions\/94"}],"wp:attachment":[{"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}