code122

Untitled

Jul 27th, 2017
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import socket
  3. import paramiko
  4. import sys
  5. import time
  6. import pandas as pd
  7.  
  8.  
  9. def readCSV(filename):
  10. data = pd.read_csv(filename)
  11. return data.values
  12.  
  13. def send_string_and_wait(command,wait_time,should_print):
  14. # Send the su command
  15. shell.send(command)
  16.  
  17. # Wait a bit, if necessary
  18. time.sleep(wait_time)
  19.  
  20. # Flush the receive buffer
  21. receive_buffer = shell.recv(1024)
  22.  
  23. # Print the receive buffer, if necessary
  24. if should_print:
  25. print(receive_buffer)
  26.  
  27. def send_string_and_wait_for_string(command, wait_string):
  28. # Send the su command
  29. shell.send(command)
  30. time.sleep(1)
  31. # Create a new receive buffer
  32. receive_buffer = shell.recv(1024)
  33. #if the command doesnt return desired response the connection will be closed and it will move on to next client.
  34. if not b'wait_string' in b'receive_buffer':
  35. # Flush the receive buffer
  36. client.close()
  37. print('connection closed as user is in AIX')
  38. # Print the receive buffer, if necessary
  39. else:
  40. print ('receive_buffer')
  41.  
  42. data= readCSV("logins.csv")
  43. for d in data:
  44.  
  45. # Get the command-line arguments
  46. if str(d[0]) !="nan":
  47. system_ip = str(d[0])
  48. system_username = str(d[1])
  49. system_ssh_password = str(d[2])
  50. system_su_password = str(d[5])
  51. por = int(d[3])
  52. root_command = "passwd -l %s\n" %d[4]
  53. root_command_result = "Locking password for user %s." %d[4]
  54. #print por
  55. # Create an SSH client
  56. client = paramiko.SSHClient()
  57.  
  58. # Make sure that we add the remote server's SSH key automatically
  59. client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  60. print ("ip : " , system_ip)
  61. # Connect to the client
  62. try:
  63. client.connect(system_ip,port=por,username=system_username, password=system_ssh_password)
  64. print("Connected"),client
  65. # Create a raw shell
  66. shell = client.invoke_shell()
  67.  
  68. # Send the su command
  69. send_string_and_wait("sudo su -\n", 1 , True)
  70.  
  71. # Send the client's su password followed by a newline
  72. send_string_and_wait(system_su_password + "\n", 1, True)
  73.  
  74. # Send the install command followed by a newline and wait for the done string
  75. send_string_and_wait_for_string(root_command, root_command_result)
  76.  
  77. # Close the SSH connection
  78. client.close()
  79.  
  80. #send_string_and_wait("passwd -u ctrls99\n", 1, True)
  81. except (paramiko.SSHException, socket.error):
  82. #the essage will be printed if the connection attempt fails.
  83. print('User has been already locked')
Add Comment
Please, Sign In to add comment