Created
November 20, 2015 02:02
-
-
Save anshumanb/683302a1216b89b850ef to your computer and use it in GitHub Desktop.
Auto-login for Windows 7 VirtualBox VM. Uses gnome-keyring to retrieve password.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
""" | |
This code is old. I would probably use secret-tool and | |
Python's subprocess to extract the password now. | |
""" | |
import sys | |
import time | |
import gnomekeyring as gk | |
from vboxapi import VirtualBoxManager | |
VM_NAME = 'windows_7' | |
USERNAME = 'myusername' | |
DOMAIN = 'windows_domain' | |
KEYRING_NAME = 'login' | |
SECRET_NAME = 'mysecret' | |
def startvm(vm_name, username, password, domain): | |
mgr = VirtualBoxManager(None, None) | |
vbox = mgr.vbox | |
machine = vbox.findMachine(vm_name) | |
session = mgr.mgr.getSessionObject(vbox) | |
progress = machine.launchVMProcess(session, "gui", "") | |
progress.waitForCompletion(-1) | |
# Wait for a bit before setting credentials in case it does not register. | |
# Though it should register; just being extra cautious. | |
time.sleep(10) | |
guest = session.console.guest | |
guest.setCredentials(username, password, domain, True) | |
mgr.closeMachineSession(session) | |
def get_password(keyring_name, secret_name): | |
secret = None | |
keys = gk.list_item_ids_sync(keyring_name) | |
for key in keys: | |
item_info = gk.item_get_info_sync(keyring_name, key) | |
if item_info.get_display_name() == secret_name: | |
secret = item_info.get_secret() | |
return secret | |
def main(): | |
if not gk.is_available(): | |
sys.exit('GNOME keyring not available') | |
password = get_password(KEYRING_NAME, SECRET_NAME) | |
if password is None: | |
sys.exit('Could not obtain password') | |
startvm(VM_NAME, USERNAME, password, DOMAIN) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment