Advertisement
Guest User

Untitled

a guest
Mar 1st, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. list = ["089"]
  2. sn = 0
  3.  
  4. ssh_new_conn = 'Are you sure you want to continue connecting'
  5.  
  6. class ThreadClass(threading.Thread):
  7. def __init__(self, index):
  8. super(ThreadClass, self).__init__()
  9. self.index = index
  10. def run(self):
  11.  
  12. sn = storelist[self.index]
  13.  
  14.  
  15. #easterndate = (currenttime + datetime.timedelta(0, 3600))
  16. #easterndate = easterndate
  17.  
  18. est = timezone('US/Eastern')
  19. cst = timezone('US/Central')
  20. #currenttime = (datetime.now())
  21. currenttime = cst.localize(datetime.now())
  22. #easterndate = (currenttime + timedelta(0, 3600))
  23. #easterndate = easterndate.strftime("%a %b %d %H:%M:%S %Z %Y")
  24. easterndate = currenttime.astimezone(est).strftime("%a %b %d %H:%M:%S %Z %Y")
  25. command1 = "/usr/bin/ssh %(username)s@%(hostname)s" % locals()
  26. command2 = " sudo date -s"%(easterndate)s"" % locals()
  27. command3 = " sudo date -s"%(currenttime)s"" % locals()
  28. now = datetime.now()
  29.  
  30. #central
  31. if sn == "073" or sn == "066" or sn == "016": #or sn == "022":
  32. p = pexpect.spawn((command1 + command3), timeout=360)
  33.  
  34.  
  35. #eastern
  36. else:
  37. print(command1 + command2)
  38. p = pexpect.spawn((command1 + command2), timeout=360)
  39.  
  40.  
  41. # Handles the 3 possible connection outcomes:
  42. # a) Ssh to the remote host for the first time, triggering 'Are you sure you want to continue connecting'
  43. # b) ask you for password
  44. # c) No password is needed at all, because you already have the key.
  45. i = p.expect([ssh_new_conn,'[pP]assword:',pexpect.EOF])
  46. print ' Initial pexpect command output: ', i
  47. if i == 0:
  48. # send 'yes'
  49. p.sendline('yes')
  50. i = p.expect(['[pP]assword:',pexpect.EOF])
  51. print 'sent yes. pexpect command output', i
  52. if i == 0:
  53. # send the password
  54. print "logging into box %(sn)s" % locals()
  55. p.sendline(password)
  56. print "login successful"
  57. print "Setting the time..."
  58.  
  59. elif i == 1:
  60. # send the password
  61. print "logging into box %(sn)s" % locals()
  62. p.sendline(password)
  63. print "login successful"
  64. print "Setting the time..."
  65. p.close()
  66.  
  67. elif i == 2:
  68. print "pexpect faced key or connection timeout"
  69. pass
  70.  
  71. print p.before
  72.  
  73. for i in range(len(list)):
  74. t = ThreadClass(i)
  75. t.start()
  76.  
  77. class ThreadClass(threading.Thread):
  78. def __init__(self, index):
  79. super(ThreadClass, self).__init__()
  80. self.index = index
  81. def run(self):
  82.  
  83. try:
  84. sn = storelist[self.index]
  85. username = raw_input('username: ')
  86. password = raw_input('password: ')
  87. hostname = "[hostname]"
  88. est = timezone('US/Eastern')
  89. cst = timezone('US/Central')
  90. #currenttime = (datetime.now())
  91. currenttime = cst.localize(datetime.now())
  92. #easterndate = (currenttime + timedelta(0, 3600))
  93. #easterndate = easterndate.strftime("%a %b %d %H:%M:%S %Z %Y")
  94. easterndate = currenttime.astimezone(est).strftime("%a %b %d %H:%M:%S %Z %Y")
  95. ssh = pxssh.pxssh()
  96.  
  97. print(hostname + " " + username + " " + password)
  98. ssh.login(hostname, username, password)
  99.  
  100. if sn == "073" or sn == "066" or sn == "016": #or sn == "022":
  101. ssh.sendline ('date') # run a command
  102. ssh.prompt() # match the prompt
  103. print(s.before) # print everything before the prompt.
  104. ssh.sendline ('sudo date -s"%(currenttime)s"' % locals()) # run a command
  105. ssh.expect('(?i)password.*:') # match password prompt for sudo
  106. ssh.sendline(password)
  107. ssh.prompt()
  108. print(s.before)
  109. ssh.logout()
  110. else:
  111. ssh.sendline ('date') # run a command
  112. ssh.prompt() # match the prompt
  113. print(s.before) # print everything before the prompt.
  114. ssh.sendline ('sudo date -s"%(easterndate)s"' % locals()) # run a command
  115. ssh.expect('(?i)password.*:') # match password prompt for sudo
  116. ssh.sendline(password)
  117. ssh.prompt()
  118. print(s.before)
  119. ssh.logout()
  120.  
  121. except pxssh.ExceptionPxssh as e:
  122. print(e)
  123.  
  124.  
  125. for i in range(len(storelist)):
  126. t = ThreadClass(i)
  127. t.start()
  128.  
  129. Traceback (most recent call last):
  130. File "./sshtest.py", line 8, in <module>
  131. s.login (hostname, username, password)
  132. File "/usr/lib/python2.6/dist-packages/pxssh.py", line 243, in login
  133. if not self.synch_original_prompt():
  134. File "/usr/lib/python2.6/dist-packages/pxssh.py", line 134, in synch_original_prompt
  135. self.read_nonblocking(size=10000,timeout=1) # GAS: Clear out the cache before getting the prompt
  136. File "/usr/lib/python2.6/dist-packages/pexpect.py", line 824, in read_nonblocking
  137. raise TIMEOUT ('Timeout exceeded in read_nonblocking().')
  138. pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().
  139.  
  140. self.sendline() #line 134
  141. time.sleep(0.5) #line 135
  142. self.read_nonblocking(size=10000,timeout=1) # GAS: Clear out the cache before getting the prompt
  143.  
  144. self.sendline() #line 134
  145. time.sleep(0.5) #line 135
  146. self.read_nonblocking(size=10000,timeout=1) # GAS: Clear out the cache before getting the prompt
  147.  
  148. import pxssh
  149. try:
  150. s = pxssh.pxssh()
  151. s.login (hostname, username, password)
  152. s.sendline ('date') # run a command
  153. s.prompt() # match the prompt
  154. print(s.before) # print everything before the prompt.
  155. s.sendline ('sudo date') # run a command
  156. s.expect('(?i)password.*:') # match password prompt for sudo
  157. s.sendline(password)
  158. s.prompt()
  159. print(s.before)
  160. s.logout()
  161. except pxssh.ExceptionPxssh as e:
  162. print(e)
  163.  
  164. # fabfile.py
  165. from fabric.api import run, sudo
  166.  
  167. def date():
  168. run('date')
  169. sudo('date')
  170.  
  171. $ fab -H localhost,user@host date
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement