Login To Multiple Servers Using One Of The Two Passwords Used For Authentication Using Python
I am new to Python. I am using Paramiko module to login to the Linux servers. However I have 2 different passwords for authentication and I want the server to be logged in using either of them. In case both fails, I am raising the exception for it. I am facing problem when I have to use the second password. Here is sample for the same. server.txt is having the list of servers
file1 = 'D:\Linux\server.txt' with open(file1) as f: switch_ip = f.readlines() switch_ip = [x.strip() for x in switch_ip] username = "user1" password1 = "abcd2" password2 = "efcdrf2" def simple_out(cmd): try: ssh=paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) x= ssh.connect(IP,port =22,username = username, password = password1 or password2) pass time.sleep(2) stdin, stdout, stderr = ssh.exec_command(cmd) line1 = stdout.readline() #read line by line while line1: split_words = line1.split() #List # split_words.insert(0,IP) print split_words str1 = ' '.join(split_words) #String x.append(str1) line1=stdout.readline() return [x] except (paramiko.BadHostKeyException, paramiko.AuthenticationException, paramiko.SSHException, socket.error) as e: time.sleep(2) buf = StringIO.StringIO(e) line3 = buf.read() y=[line3] return [y] for IP in switch_ip: output = simple_out("df -h") # will call function and execute command out1 = output #t for items in out1: book = xlrd.open_workbook('D:\\Linux\\xlwt example.xls') sheet2 = book.sheet_by_index(0) row_count = sheet2.nrows column_count = 1 sheet1.write(row_count, 0, IP) sheet1.write(row_count, column_count, items) wb.save('D:\\Linux\\xlwt example.xls') time.sleep(2)
I want to login to the servers using either of the 2 passwords
you can use try catch block for each password and continue in your program see below example:
try: ssh.connect(IP,port =22,username = username, password = password1) except paramiko.AuthenticationException as e: #catch other exceptions as well ssh.connect(IP,port =22,username = username, password = password2)
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module