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