Guest User

Untitled

a guest
Sep 11th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. Executing a bash script in python
  2. #!/usr/bin/expect
  3. spawn ssh usr@myip
  4. expect "password:"
  5. send "mypasswordn";
  6. send "./mycommand1r"
  7. send "./mycommand2r"
  8. interact
  9.  
  10. import subprocess
  11. def runmyscript():
  12. subprocess.call("myscript.txt", executable="expect", shell=True)
  13. def main():
  14. run = runmyscript():
  15. if __name__ == '__main__': main()
  16.  
  17. WindowsError: [Error 2] The system cannot find the file specified
  18.  
  19. child = subprocess.Popen(['bash', '-c', './myscript.txt'], stdout = subprocess.PIPE)
  20.  
  21. import paramiko
  22.  
  23. user = "user"
  24. pass = "pass"
  25. host = "host"
  26.  
  27. client = paramiko.SSHClient()
  28. client.load_system_host_keys()
  29. client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  30. client.connect(host, port=22, username=user, password=pass)
  31. client.exec_command("./mycommand1")
  32. client.exec_command("./mycommand2")
  33. client.close()
  34.  
  35. import pexpect
  36.  
  37. ## Cleanly handle a variety of scenarios that can occur when ssh or scp-ing to an ip:port
  38. # amongst them are:
  39. #
  40. # (1) key has not been setup
  41. # (2) key has changed since last time
  42. # (3) command was executed (check exit status and output)
  43. #
  44. # @param cmdLine The "scp" or "ssh" command-line
  45. # @param mtimeout The millisecond timeout to wait for the child process to return
  46. # @param log The log to record events to if necessary
  47. def cleanlyHandleSecureCmd(cmdLine, mtimeout = None, log = None):
  48. status = -1
  49. output = None
  50.  
  51. if mtimeout == None:
  52. mtimeout = 60 * 1000
  53.  
  54. if cmdLine != None and ('scp' in cmdLine or 'ssh' in cmdLine):
  55. # Scenarios for ssh include: (1) key not setup (2) key changed (3) remote cmd was executed (check exit status)
  56. scenarios = ['Are you sure you want to continue connecting', '@@@@@@@@@@@@', EOF]
  57. child = spawn(cmdLine, timeout = mtimeout)
  58. scenario = child.expect(scenarios)
  59.  
  60. if scenario == 0:
  61. # (1) key not setup ==> say 'yes' and allow child process to continue
  62. child.sendline('yes')
  63.  
  64. scenario = child.expect(scenarios)
  65.  
  66. if scenario == 1:
  67. if log != None:
  68. # (2) key changed ==> warn the user in the log that this was encountered
  69. log.write('WARNING (' + cmdLine + '): ssh command encountered man-in-the-middle scenario! Please investigate.')
  70.  
  71. lines = child.readlines()
  72. scenario = child.expect([EOF])
  73.  
  74. child.close()
  75. else:
  76. # (3) remote cmd was executed ==> check the exit status and log any errors
  77. child.close()
  78.  
  79. status = child.exitstatus
  80. output = child.before
  81. output = sub('rn', 'n', output) # Do not be pedantic about end-of-line chars
  82. output = sub('n$', '', output) # Ignore any trailing newline that is present
  83.  
  84. if status == None:
  85. status = child.status
  86.  
  87. if status != 0 and log != None:
  88. log.error('Error executing command '' + str(cmdLine) + '' gave status of ' + str(status) + ' and output: ' + str(output))
  89. else:
  90. if log != None:
  91. log.error('Command-line must contain either ssh or scp: ' + str(cmdLine))
  92.  
  93. return (status, output)
Add Comment
Please, Sign In to add comment