Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import socket
- import paramiko
- import sys
- import time
- import pandas as pd
- def readCSV(filename):
- data = pd.read_csv(filename)
- return data.values
- def send_string_and_wait(command,wait_time,should_print):
- # Send the su command
- shell.send(command)
- # Wait a bit, if necessary
- time.sleep(wait_time)
- # Flush the receive buffer
- receive_buffer = shell.recv(1024)
- # Print the receive buffer, if necessary
- if should_print:
- print(receive_buffer)
- def send_string_and_wait_for_string(command, wait_string):
- # Send the su command
- shell.send(command)
- time.sleep(1)
- # Create a new receive buffer
- receive_buffer = shell.recv(1024)
- #if the command doesnt return desired response the connection will be closed and it will move on to next client.
- if not b'wait_string' in b'receive_buffer':
- # Flush the receive buffer
- client.close()
- print('connection closed as user is in AIX')
- # Print the receive buffer, if necessary
- else:
- print ('receive_buffer')
- data= readCSV("logins.csv")
- for d in data:
- # Get the command-line arguments
- if str(d[0]) !="nan":
- system_ip = str(d[0])
- system_username = str(d[1])
- system_ssh_password = str(d[2])
- system_su_password = str(d[5])
- por = int(d[3])
- root_command = "passwd -l %s\n" %d[4]
- root_command_result = "Locking password for user %s." %d[4]
- #print por
- # Create an SSH client
- client = paramiko.SSHClient()
- # Make sure that we add the remote server's SSH key automatically
- client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- print ("ip : " , system_ip)
- # Connect to the client
- try:
- client.connect(system_ip,port=por,username=system_username, password=system_ssh_password)
- print("Connected"),client
- # Create a raw shell
- shell = client.invoke_shell()
- # Send the su command
- send_string_and_wait("sudo su -\n", 1 , True)
- # Send the client's su password followed by a newline
- send_string_and_wait(system_su_password + "\n", 1, True)
- # Send the install command followed by a newline and wait for the done string
- send_string_and_wait_for_string(root_command, root_command_result)
- # Close the SSH connection
- client.close()
- #send_string_and_wait("passwd -u ctrls99\n", 1, True)
- except (paramiko.SSHException, socket.error):
- #the essage will be printed if the connection attempt fails.
- print('User has been already locked')
Add Comment
Please, Sign In to add comment