Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SCP.py 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import tempfile, pexpect
  2. def scp(src_name, dest_name, host, user, password, timeout=30, bg_run=False):
  3. """SSH'es to a host using the supplied credentials and executes a command.
  4. Throws an exception if the command doesn't return 0.
  5. bgrun: run command in the background"""
  6. fname = tempfile.mktemp()
  7. fout = open(fname, 'w')
  8. options = '-q -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -oPubkeyAuthentication=no'
  9. if bg_run:
  10. options += ' -f'
  11. scp_cmd = 'scp %s %s@%s:%s' % (src_name, user, host, dest_name)
  12. child = pexpect.spawn(scp_cmd, timeout=timeout, encoding='UTF-8')
  13. child.expect(['password: '])
  14. child.sendline(password)
  15. child.logfile = fout
  16. child.expect(pexpect.EOF)
  17. # We expect any of these three patterns...
  18. #i = child.expect (['Permission denied', 'Terminal type', '[#\$] '])
  19. #if i==0:
  20. # print('Permission denied on host. Can\'t login')
  21. # child.kill(0)
  22. #elif i==1:
  23. # print('Login OK... need to send terminal type.')
  24. # child.sendline('vt100')
  25. # child.expect('[#\$] ')
  26. #elif i==2:
  27. # print('Login OK.')
  28. # print('Shell command prompt', child.after)
  29. child.close()
  30. fout.close()
  31. fin = open(fname, 'r')
  32. stdout = fin.read()
  33. fin.close()
  34. if 0 != child.exitstatus:
  35. raise Exception(stdout)
  36. return stdout