Best Python code snippet using Airtest
amf.py
Source:amf.py
...111 #print(text)112 i_text = 0113 n = Notification()114 n.lien = url115 part = read_until(text, i_text, "NOM")116 i_text += len(part)117 part = read_until(text, i_text, ":")118 i_text += len(part)119 i_text += 1120 part = read_until(text, i_text, "NOTIFICATION")121 i_text += len(part)122 n.dirigeant = part.strip()123 part = read_until(text, i_text, ":")124 i_text += len(part)125 i_text += 1126 part = read_until(text, i_text, "COORDONNEES")127 i_text += len(part)128 n.type_notification = part.strip()129 part = read_until(text, i_text, "NOM")130 i_text += len(part)131 part = read_until(text, i_text, ":")132 i_text += len(part)133 i_text += 1134 part = read_until_2(text, i_text, "LEI", "DETAIL")135 i_text += len(part)136 n.emetteur = part.strip()137 part = read_until(text, i_text, "DETAIL")138 i_text += len(part)139 while text[i_text : i_text + 6] == "DETAIL" :140 t = Transaction()141 part = read_until(text, i_text, "DATE")142 i_text += len(part)143 part = read_until(text, i_text, ":")144 i_text += len(part)145 i_text += 1146 part = read_until(text, i_text, "LIEU")147 i_text += len(part)148 t.date = part.strip()149 part = read_until(text, i_text, ":")150 i_text += len(part)151 i_text += 1152 part = read_until(text, i_text, "NATURE")153 i_text += len(part)154 t.lieu = part.strip()155 part = read_until(text, i_text, ":")156 i_text += len(part)157 i_text += 1158 part = read_until(text, i_text, "DESCRIPTION")159 i_text += len(part)160 t.nature = part.strip()161 part = read_until(text, i_text, ":")162 i_text += len(part)163 i_text += 1164 part = read_until_2(text, i_text, "CODE", "INFORMATION")165 i_text += len(part)166 t.instrument = part.strip()167 part = read_until(text, i_text, "AGREGEES")168 i_text += len(part)169 part = read_until(text, i_text, "PRIX")170 i_text += len(part)171 part = read_until(text, i_text, ":")172 i_text += len(part)173 i_text += 1174 part = read_until(text, i_text, "VOLUME")175 i_text += len(part)176 t.prix = part.strip()177 part = read_until(text, i_text, ":")178 i_text += len(part)179 i_text += 1180 part = read_until(text, i_text, "TRANSACTION")181 i_text += len(part)182 t.volume = part.strip()183 part = read_until(text, i_text, "LIE")184 i_text += len(part)185 part = read_until(text, i_text, ":")186 i_text += len(part)187 i_text += 1188 part = read_until_2(text, i_text, "DETAIL", "DATE")189 i_text += len(part)190 t.lie = part.strip()191 n.transactions.append(t)192 print(t.date)193 print(t.lieu)194 print(t.nature)195 print(t.instrument)196 print(t.prix)197 print(t.volume)198 print(t.lie)199 part = read_until(text, i_text, ":")200 i_text += len(part)201 i_text += 1202 part = read_until(text, i_text, "COMMENTAIRE")203 i_text += len(part)204 n.date = part.strip()205 part = read_until(text, i_text, ":")206 i_text += len(part)207 i_text += 1208 part = read_until(text, i_text, "\"")209 i_text += len(part)210 n.commentaires = part.strip()211 print(n.lien)212 print(n.dirigeant)213 print(n.type_notification)214 print(n.emetteur)215 print(n.date)216 print(n.commentaires)217 columns = ["lien", "dirigeant", "type", "emetteur", "date_transaction", "lieu", "nature", "instrument", "prix", "volume", "lie", "date_notification", "commentaires"]218 for i in range(len(n.transactions)) :219 t = n.transactions[i]220 values = [n.lien, n.dirigeant, n.type_notification, n.emetteur, t.date, t.lieu, t.nature, t.instrument, t.prix, t.volume, t.lie, n.date, n.commentaires]221 database.Insert(table, columns, values)222class Transaction() :223 date = ""224 lieu = ""225 nature = ""226 instrument = ""227 prix = ""228 volume = ""229 lie = ""230class Notification() :231 lien = ""232 dirigeant = ""233 type_notification = ""234 emetteur = ""235 transactions = []236 date = ""237 commentaires = ""238 def __init__(self) : 239 self.transactions = []240def parse2(url, text, database) :241 print("PARSING 2 : ")242 i_text = 0243 part = read_until(text, i_text, "DECLARANT.")244 i_text += len(part)245 i_text += 10246 part = read_until(text, i_text, "DECLARANT")247 i_text += len(part)248 emetteur = part.strip()249 part = read_until(text, i_text, ":")250 i_text += len(part)251 i_text += 1252 part = read_until(text, i_text, "INSTRUMENT")253 i_text += len(part)254 dirigeant = part.strip()255 part = read_until(text, i_text, ":")256 i_text += len(part)257 i_text += 1258 part = read_until(text, i_text, "NATURE")259 i_text += len(part)260 instrument = part.strip()261 part = read_until(text, i_text, ":")262 i_text += len(part)263 i_text += 1264 part = read_until(text, i_text, "DATE")265 i_text += len(part)266 nature = part.strip()267 part = read_until(text, i_text, ":")268 i_text += len(part)269 i_text += 1270 part = read_until(text, i_text, "DATE")271 i_text += len(part)272 date_transaction = part.strip()273 part = read_until(text, i_text, ":")274 i_text += len(part)275 i_text += 1276 part = read_until(text, i_text, "LIEU")277 i_text += len(part)278 date_notification = part.strip()279 part = read_until(text, i_text, ":")280 i_text += len(part)281 i_text += 1282 part = read_until(text, i_text, "PRIX")283 i_text += len(part)284 lieu = part.strip()285 part = read_until(text, i_text, ":")286 i_text += len(part)287 i_text += 1288 part = read_until(text, i_text, "MONTANT")289 i_text += len(part)290 prix = part.strip()291 part = read_until(text, i_text, ":")292 i_text += len(part)293 i_text += 1294 part = read_until_2(text, i_text, "INFORMATIONS", "\"Les")295 i_text += len(part)296 montant = part.strip()297 commentaires = ""298 print(text[i_text : i_text + 4])299 if text[i_text : i_text + 4] == "INFO" :300 print("coucou")301 part = read_until(text, i_text, ":")302 i_text += len(part)303 i_text += 1304 part = read_until(text, i_text, "\"Les")305 i_text += len(part)306 commentaires = part.strip()307 print("url = " + url)308 print("emetteur = " + emetteur)309 print("dirigeant = " + dirigeant)310 print(instrument)311 print(nature)312 print(date_transaction)313 print(date_notification)314 print(lieu)315 print(prix)316 print(montant)317 print(commentaires)318 columns = ["lien", "dirigeant", "emetteur", "date_transaction", "lieu", "nature", "instrument", "prix", "montant", "date_notification", "commentaires"]319 values = [url, dirigeant, emetteur, date_transaction, lieu, nature, instrument, prix, montant, date_notification, commentaires]320 database.Insert(table, columns, values)321def read_until(text, i_text, end) :322 result = ""323 while i_text < len(text) and text[i_text : i_text + len(end)] != end :324 result += text[i_text]325 i_text += 1326 return result327def read_until_2(text, i_text, end1, end2) :328 result = ""329 while i_text < len(text) and text[i_text : i_text + len(end1)] != end1 and text[i_text : i_text + len(end2)] != end2 :330 result += text[i_text]331 i_text += 1...
test2.py
Source:test2.py
...65 print(device['name'] + "(" + device['ipaddr'] + "ï¼æ æ³è¿æ¥")66 print("Backup " + device['name'] + "(" + device['ipaddr'] + ")'s config failure")67 return 068 try:69 tn.read_until(b"Username:")70 tn.write(device['user'].encode('utf-8') + b"\n")71 tn.read_until(b"Password:")72 tn.write(device['passwd'].encode('utf-8') + b"\n")73 tn.read_until(b">")74 tn.write(b"su \n")75 tn.read_until(b"Password:")76 tn.write(device['supasswd'].encode('utf-8') + b"\n")77 tn.read_until(b">")78 tn.write(b"ftp " + ftp_server['ip'].encode('utf-8') + b"\n")79 tn.read_until(b"):")80 tn.write(ftp_server['user'].encode('utf-8') + b"\n")81 tn.read_until(b"Password:")82 tn.write(ftp_server['passwd'].encode('utf-8') + b"\n")83 tn.read_until(b"[ftp]")84 tn.write(b"cd " + ftp_server['bak_dir'].encode('utf-8') + b"\n")85 tn.read_until(b"[ftp]")86 tn.write(b"put config.cfg " + device['hostname'].encode('utf-8') + b".cfg\n")87 tn.read_until(b"[ftp]")88 tn.write(b"quit\n")89 tn.read_until(b">")90 tn.write(b"quit\n")91 tn.close()92 print("Backup " + device['name'] + "(" + device['ipaddr'] + ")'s config successful.")93 return 194 except:95 print("Encounter error when backuping " + device['name'])96 print("Backup " + device['name'] + "(" + device['ipaddr'] + ")'s config failure")97 return 098def bak_H3C_Rv5_cfg(device, ftp_server):99 #éç¨S7507ãR20XXãR30XX100 print(sys._getframe().f_code.co_name)101 print("Begin to backup " + device['name'] + "(" + device['ipaddr'] + ")'s config ...")102 try:103 tn = telnetlib.Telnet(device['ipaddr'])104 tn.set_debuglevel(2)105 except:106 print(device['name'] + "(" + device['ipaddr'] + "ï¼æ æ³è¿æ¥")107 print("Backup " + device['name'] + "(" + device['ipaddr'] + ")'s config failure")108 return 0109 try:110 tn.read_until(b"Username:")111 tn.write(device['user'].encode('utf-8') + b"\n")112 tn.read_until(b"Password:")113 tn.write(device['passwd'].encode('utf-8') + b"\n")114 tn.read_until(b">")115 tn.write(b"su \n")116 tn.read_until(b"Password:")117 tn.write(device['supasswd'].encode('utf-8') + b"\n")118 tn.read_until(b">")119 tn.write(b"ftp " + ftp_server['ip'].encode('utf-8') + b"\n")120 tn.read_until(b"):")121 tn.write(ftp_server['user'].encode('utf-8') + b"\n")122 tn.read_until(b"Password:")123 tn.write(ftp_server['passwd'].encode('utf-8') + b"\n")124 tn.read_until(b"[ftp]")125 tn.write(b"cd " + ftp_server['bak_dir'].encode('utf-8') + b"\n")126 tn.read_until(b"[ftp]")127 tn.write(b"put startup.cfg " + device['hostname'].encode('utf-8') + b".cfg\n")128 tn.read_until(b"[ftp]")129 tn.write(b"quit\n")130 tn.read_until(b">")131 tn.write(b"quit\n")132 tn.close()133 print("Backup " + device['name'] + "(" + device['ipaddr'] + ")'s config successful.")134 return 1135 except:136 print("Encounter error when backuping " + device['name'])137 print("Backup " + device['name'] + "(" + device['ipaddr'] + ")'s config failure")138 return 0139def bak_H3C_Rv7_cfg(device, ftp_server):140 # éç¨3600-28ã3600-51141 print(sys._getframe().f_code.co_name)142 print("Begin to backup " + device['name'] + "(" + device['ipaddr'] + ")'s config ...")143 try:144 tn = telnetlib.Telnet(device['ipaddr'])145 tn.set_debuglevel(2)146 except:147 print(device['name'] + "(" + device['ipaddr'] + "ï¼æ æ³è¿æ¥")148 print("Backup " + device['name'] + "(" + device['ipaddr'] + ")'s config failure")149 return 0150 try:151 tn.read_until(b"login:")152 tn.write(device['user'].encode('utf-8') + b"\n")153 tn.read_until(b"Password:")154 tn.write(device['passwd'].encode('utf-8') + b"\n")155 tn.read_until(b">")156 tn.write(b"su \n")157 tn.read_until(b"Password:")158 tn.write(device['supasswd'].encode('utf-8') + b"\n")159 tn.read_until(b">")160 tn.write(b"ftp " + ftp_server['ip'].encode('utf-8') + b"\n")161 tn.read_until(b"):")162 tn.write(ftp_server['user'].encode('utf-8') + b"\n")163 tn.read_until(b"Password:")164 tn.write(ftp_server['passwd'].encode('utf-8') + b"\n")165 tn.read_until(b"ftp>")166 tn.write(b"cd " + ftp_server['bak_dir'].encode('utf-8') + b"\n")167 tn.read_until(b"ftp>")168 tn.write(b"put startup.cfg " + device['hostname'].encode('utf-8') + b".cfg\n")169 tn.read_until(b"ftp>")170 tn.write(b"quit\n")171 tn.read_until(b">")172 tn.write(b"quit\n")173 tn.close()174 print("Backup " + device['name'] + "(" + device['ipaddr'] + ")'s config successful.")175 return 1176 except:177 print("Encounter error when backuping " + device['name'])178 print("Backup " + device['name'] + "(" + device['ipaddr'] + ")'s config failure")179 return 0180def bak_CSC_v12_cfg(device, ftp_server):181 # éç¨3800,2900182 print(sys._getframe().f_code.co_name)183 print("Begin to backup " + device['name'] + "(" + device['ipaddr'] + ")'s config ...")184 try:185 tn = telnetlib.Telnet(device['ipaddr'])186 tn.set_debuglevel(2)187 except:188 print(device['name'] + "(" + device['ipaddr'] + "ï¼æ æ³è¿æ¥")189 print("Backup " + device['name'] + "(" + device['ipaddr'] + ")'s config failure")190 return 0191 try:192 tn.read_until(b"Username:")193 tn.write(device['user'].encode('utf-8') + b"\n")194 tn.read_until(b"Password:")195 tn.write(device['passwd'].encode('utf-8') + b"\n")196 tn.read_until(b">")197 tn.write(b"en\n")198 tn.read_until(b"Password:")199 tn.write(device['supasswd'].encode('utf-8') + b"\n")200 tn.read_until(b"#")201 tn.write(b"copy start ftp\n")202 tn.read_until(b"]?")203 tn.write(ftp_server['ip'].encode('utf-8') + b"\n")204 tn.read_until(b"]?")205 tn.write(ftp_server['bak_dir'].encode('utf-8') +b"/" + device['hostname'].encode('utf-8') + b".cfg\n")206 tn.read_until(b"#")207 tn.write(b"exit\n")208 tn.close()209 print("Backup " + device['name'] + "(" + device['ipaddr'] + ")'s config successful.")210 return 1211 except:212 print("Encounter error when backuping " + device['name'])213 print("Backup " + device['name'] + "(" + device['ipaddr'] + ")'s config failure")214 return 0215if __name__ == '__main__':216 bat_no = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')217 print("***Begin to backup network device's config to FTP***")218 ftp_server = mk_bak_dir_on_ftp(FTP_SERVER, bat_no)219 FTP_SERVER['bak_dir'] = bat_no220 devices = get_devices(DB_SERVER)...
telnetlib_show.py
Source:telnetlib_show.py
...4username = "admin"5password = "access123"6enable_password = "access123"7cnx = telnetlib.Telnet(host="10.10.88.110")8cnx.read_until("Username:")9cnx.write(username + "\n")10cnx.read_until("Password:")11cnx.write(password + "\n")12cnx.read_until(">")13cnx.write("en" + "\n")14cnx.read_until("Password:")15cnx.write(enable_password + "\n")16cnx.read_until("#")17cnx.write("show ip int b" + "\n")18output = cnx.read_until("#")19cleaned_output = output.replace("show ip int b", "").replace("R1#", "")20print cleaned_output21exit()22# Ask users to enter the username and password23import telnetlib24import time25username = raw_input("please Enter your username:")26password = raw_input("please Enter your password:")27enable_password = raw_input("please Enter your enable password:")28cnx = telnetlib.Telnet(host="10.10.88.110")29# cnx.set_debuglevel(1000)30cnx.read_until("Username:")31cnx.write(username + "\n")32cnx.read_until("Password:")33cnx.write(password + "\n")34cnx.read_until(">")35cnx.write("en" + "\n")36cnx.read_until("Password:")37cnx.write(enable_password + "\n")38cnx.read_until("#")39cnx.write("show ip int b" + "\n")40time.sleep(5)41output = cnx.read_until("#")42print output43# Hide the password44import telnetlib45import time46import getpass47username = raw_input("please Enter your username:")48password = getpass.getpass("please Enter your password:")49enable_password = getpass.getpass("please Enter your enable password:")50cnx = telnetlib.Telnet(host="10.10.88.110")51# cnx.set_debuglevel(1000)52cnx.read_until("Username:")53cnx.write(username + "\n")54cnx.read_until("Password:")55cnx.write(password + "\n")56cnx.read_until(">")57cnx.write("en" + "\n")58cnx.read_until("Password:")59cnx.write(enable_password + "\n")60cnx.read_until("#")61cnx.write("show ip int b" + "\n")62time.sleep(5)63output = cnx.read_until("#")64print output65hosts = ["10.10.88.110", "10.10.88.111"]66username = raw_input("Please Enter your username:")67password = getpass.getpass("Please Enter your Password:")68enable_password = getpass.getpass("Please Enter your Enable Password:")69for ip_address in hosts:70 cnx = telnetlib.Telnet(ip_address)71 cnx.read_until("Username:")72 cnx.write(username + "\n")73 cnx.read_until("Password:")74 cnx.write(password + "\n")75 cnx.read_until(">")76 cnx.write("en" + "\n")77 cnx.read_until("Password:")78 cnx.write(enable_password + "\n")79 cnx.read_until("#")80 cnx.write("show ip int b" + "\n")81 # time.sleep(1)82 # cnx.read_until("#")83 output = cnx.read_until("#") # You need to print the variable84 print "\n#################### EXECUTING COMMAND ON {0} ###############################".format(ip_address)85 print output.replace("show ip int b", "")...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!