Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 2 | # |
Han Shen | 0d39863 | 2016-02-01 16:18:19 -0800 | [diff] [blame] | 3 | # Copyright 2010~2015 Google Inc. All Rights Reserved. |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 4 | """Script to get past the login screen of ChromeOS.""" |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 5 | |
Han Shen | 0d39863 | 2016-02-01 16:18:19 -0800 | [diff] [blame] | 6 | from __future__ import print_function |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 7 | |
Han Shen | 0d39863 | 2016-02-01 16:18:19 -0800 | [diff] [blame] | 8 | import argparse |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 9 | import os |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 10 | import sys |
asharif | 54cf678 | 2013-02-16 02:11:03 +0000 | [diff] [blame] | 11 | import tempfile |
Han Shen | 0d39863 | 2016-02-01 16:18:19 -0800 | [diff] [blame] | 12 | |
| 13 | from cros_utils import command_executer |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 14 | |
| 15 | LOGIN_PROMPT_VISIBLE_MAGIC_FILE = '/tmp/uptime-login-prompt-visible' |
| 16 | LOGGED_IN_MAGIC_FILE = '/var/run/state/logged-in' |
| 17 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 18 | script_header = """ |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 19 | import os |
| 20 | import autox |
| 21 | import time |
asharif | d2ced68 | 2013-02-16 01:05:17 +0000 | [diff] [blame] | 22 | """ |
| 23 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 24 | wait_for_login_screen = """ |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 25 | |
| 26 | while True: |
| 27 | print 'Waiting for login screen to appear...' |
| 28 | if os.path.isfile('%s'): |
| 29 | break |
| 30 | time.sleep(1) |
| 31 | print 'Done' |
| 32 | |
| 33 | time.sleep(20) |
asharif | d2ced68 | 2013-02-16 01:05:17 +0000 | [diff] [blame] | 34 | """ % LOGIN_PROMPT_VISIBLE_MAGIC_FILE |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 35 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 36 | do_login = """ |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 37 | xauth_filename = '/home/chronos/.Xauthority' |
| 38 | os.environ.setdefault('XAUTHORITY', xauth_filename) |
| 39 | os.environ.setdefault('DISPLAY', ':0.0') |
| 40 | |
| 41 | print 'Now sending the hotkeys for logging in.' |
| 42 | ax = autox.AutoX() |
| 43 | # navigate to login screen |
| 44 | ax.send_hotkey('Ctrl+Shift+q') |
| 45 | ax.send_hotkey('Ctrl+Alt+l') |
| 46 | # escape out of any login screen menus (e.g., the network select menu) |
| 47 | time.sleep(2) |
| 48 | ax.send_hotkey('Escape') |
| 49 | time.sleep(2) |
| 50 | ax.send_hotkey('Tab') |
| 51 | time.sleep(0.5) |
| 52 | ax.send_hotkey('Tab') |
| 53 | time.sleep(0.5) |
| 54 | ax.send_hotkey('Tab') |
| 55 | time.sleep(0.5) |
| 56 | ax.send_hotkey('Tab') |
| 57 | time.sleep(0.5) |
| 58 | ax.send_hotkey('Return') |
| 59 | print 'Waiting for Chrome to appear...' |
| 60 | while True: |
| 61 | if os.path.isfile('%s'): |
| 62 | break |
| 63 | time.sleep(1) |
| 64 | print 'Done' |
asharif | d2ced68 | 2013-02-16 01:05:17 +0000 | [diff] [blame] | 65 | """ % LOGGED_IN_MAGIC_FILE |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 66 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 67 | |
asharif | d2ced68 | 2013-02-16 01:05:17 +0000 | [diff] [blame] | 68 | def RestartUI(remote, chromeos_root, login=True): |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 69 | chromeos_root = os.path.expanduser(chromeos_root) |
| 70 | ce = command_executer.GetCommandExecuter() |
| 71 | # First, restart ui. |
asharif | d2ced68 | 2013-02-16 01:05:17 +0000 | [diff] [blame] | 72 | command = 'rm -rf %s && restart ui' % LOGIN_PROMPT_VISIBLE_MAGIC_FILE |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 73 | ce.CrosRunCommand(command, machine=remote, chromeos_root=chromeos_root) |
asharif | 54cf678 | 2013-02-16 02:11:03 +0000 | [diff] [blame] | 74 | host_login_script = tempfile.mktemp() |
| 75 | device_login_script = '/tmp/login.py' |
asharif | d2ced68 | 2013-02-16 01:05:17 +0000 | [diff] [blame] | 76 | login_script_list = [script_header, wait_for_login_screen] |
| 77 | if login: |
| 78 | login_script_list.append(do_login) |
| 79 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 80 | full_login_script_contents = '\n'.join(login_script_list) |
asharif | d2ced68 | 2013-02-16 01:05:17 +0000 | [diff] [blame] | 81 | |
asharif | 54cf678 | 2013-02-16 02:11:03 +0000 | [diff] [blame] | 82 | with open(host_login_script, 'w') as f: |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 83 | f.write(full_login_script_contents) |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 84 | ce.CopyFiles( |
| 85 | host_login_script, |
| 86 | device_login_script, |
| 87 | dest_machine=remote, |
| 88 | chromeos_root=chromeos_root, |
| 89 | recursive=False, |
| 90 | dest_cros=True) |
| 91 | ret = ce.CrosRunCommand( |
| 92 | 'python %s' % device_login_script, |
| 93 | chromeos_root=chromeos_root, |
| 94 | machine=remote) |
asharif | 54cf678 | 2013-02-16 02:11:03 +0000 | [diff] [blame] | 95 | if os.path.exists(host_login_script): |
| 96 | os.remove(host_login_script) |
| 97 | return ret |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 98 | |
| 99 | |
| 100 | def Main(argv): |
| 101 | """The main function.""" |
Han Shen | 0d39863 | 2016-02-01 16:18:19 -0800 | [diff] [blame] | 102 | parser = argparse.ArgumentParser() |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 103 | parser.add_argument( |
| 104 | '-r', '--remote', dest='remote', help='The remote ChromeOS box.') |
| 105 | parser.add_argument( |
| 106 | '-c', '--chromeos_root', dest='chromeos_root', help='The ChromeOS root.') |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 107 | |
Han Shen | 0d39863 | 2016-02-01 16:18:19 -0800 | [diff] [blame] | 108 | options = parser.parse_args(argv) |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 109 | |
asharif | 54cf678 | 2013-02-16 02:11:03 +0000 | [diff] [blame] | 110 | return RestartUI(options.remote, options.chromeos_root) |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 111 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 112 | |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 113 | if __name__ == '__main__': |
Han Shen | 0d39863 | 2016-02-01 16:18:19 -0800 | [diff] [blame] | 114 | retval = Main(sys.argv[1:]) |
asharif | 5ab1c7d | 2013-02-15 23:43:58 +0000 | [diff] [blame] | 115 | sys.exit(retval) |