Python learning for Network Engineers | Part 16 |Cisco backup script string formatting add timestamp
𝗙𝗼𝗿 𝗖𝗼𝗺𝗽𝗹𝗲𝘁𝗲 𝗣𝘆𝘁𝗵𝗼𝗻 𝗙𝗼𝗿 𝗡𝗲𝘁𝘄𝗼𝗿𝗸 𝗘𝗻𝗴𝗶𝗻𝗲𝗲𝗿𝘀 𝗖𝗼𝘂𝗿𝘀𝗲:
𝗘𝗻𝗿𝗼𝗹𝗹 𝗳𝗼𝗿 𝗯𝗲𝗹𝗼𝘄 𝗨𝗱𝗲𝗺𝘆 𝗖𝗹𝗮𝘀𝘀: 𝟐𝟎𝟐𝟐 𝐕𝐞𝐫𝐬𝐢𝐨𝐧
𝑫𝒊𝒔𝒄𝒐𝒖𝒏𝒕𝒆𝒅 𝑹𝒆𝒇𝒆𝒓𝒓𝒂𝒍 𝑳𝒊𝒏𝒌:
https://www.udemy.com/course/python-for-network-engineers/?referralCode=35A75AAE1ACA94A15829 Python Series Complete video Playlist URL :https://www.youtube.com/watch?v=sG_RiytUA38&list=PLOocymQm7YWakdZkBfCRIC06fv7xQE85N
To stay updated with my latest videos Please subscribe to my channel by clicking below
https://www.youtube.com/channel/UCcA2nhdC0wzqyv9x1lk5NnA?sub_confirmation=1
#CiscoPythonBackup
#PythonStringFormatting
#PythonTimeFormatting
This video demonstrates string formatting in python 3.
Different types of string formatting eg %s %i in python
Shows how to format time stamp data in python using, type formatting
print (datetime.datetime.now().day)
TNOW = datetime.datetime.now()
print (TNOW.day)
print (TNOW.month)
print (TNOW.year)
print (TNOW.hour)
print (TNOW.minute)
print (TNOW.second)
TNOW = datetime.datetime.now()
print (str(TNOW.day) +’-‘+str(TNOW.month)+’-‘+str(TNOW.year)+’_’+ str(TNOW.hour) + ‘-‘ +str(TNOW.minute) + ‘-‘ + str(TNOW.second) )
print(“%.2i-%.2i-%i_%.2i-%.2i-%.2i” % (TNOW.day,TNOW.month,TNOW.year,TNOW.hour,TNOW.minute,TNOW.second))
help() : FORMATTING
‘{:%d-%m-%Y_%H:%M:%S}’.format(TM)
TNOW = datetime.datetime.now()
TFORMAT = ‘{:%d-%m-%Y_%H:%M:%S}’.format(TNOW)
print(TFORMAT)
DEVICE_ACCESS.send(“copy nvram:startup-config scp://user@10.10.10.99//data/05_PYTHON_DEMO/ROUTER_” + RTR +”_”+ TFORMAT + “nnnn”)
Sample Script
import paramiko
import time
from getpass import getpass
import datetime
TNOW = datetime.datetime.now().replace(microsecond=0)
TFORMAT = ‘{:%d-%m-%Y_%H:%M:%S}’.format(TNOW)
username = ‘admin’
password = ‘admin’
scp_pass = getpass( prompt = ‘Enter SCP server Password :’)
DEVICE_LIST = open (’09_devices’)
for RTR in DEVICE_LIST:
RTR = RTR.strip()
print (‘n #### Connecting to the device ‘ + RTR + ‘####n’ )
SESSION = paramiko.SSHClient()
SESSION.set_missing_host_key_policy(paramiko.AutoAddPolicy())
SESSION.connect(RTR,port=22,
username=username,
password=password,
look_for_keys=False,
allow_agent=False)
DEVICE_ACCESS = SESSION.invoke_shell()
DEVICE_ACCESS.send(‘copy nvram:startup-config scp://shibi.v@10.10.10.100//data/05_PYTHON_DEMO/ROUTER_’ + RTR + ‘_’+TFORMAT + ‘nnnn’)
time.sleep(5)
DEVICE_ACCESS.send(scp_pass +’n’)
time.sleep(1)
print (‘Backup completed for the device ‘ + RTR + ‘nn’)
SESSION.close
Views : 3389
network engineer