gpt4 book ai didi

java - VirtualBox Sdk Java - 从主机操作系统在 guest 操作系统内执行命令

转载 作者:行者123 更新时间:2023-12-02 05:01:01 25 4
gpt4 key购买 nike

我正在尝试从主机操作系统(Ubuntu 14.04)在 guest 操作系统(Ubuntu 服务器)内创建用户。

这是我的代码

IGuestProcess process = null;

ISession session = manager.getSessionObject(); //VirtualBoxManager

machine.lockMachine(session, LockType.Shared); //IMachine

IConsole console = session.getConsole();

IGuest guest = console.getGuest();

IGuestSession guestSession = guest.createSession("registerdUserWithRights", "hisPasswd", "", "");

Long time = 100000L;

GuestSessionWaitResult result = guestSession.waitFor(time, time);

if(result == GuestSessionWaitResult.Start)
process = guestSession.processCreate("adduser --disabled-password --gecos \"\" username", null, null, null, 0L);

ProcessWaitResult waitResult = process.waitFor(1L, time);

if(waitResult == ProcessWaitResult.Start)
System.out.println("started");

但是这在 ProcessWaitResult waitResult = process.waitFor(1L, time); 处失败。我不断得到

VBox error: VirtualBox error: rc=0x80bb0005 The specified file was not found on guest (0x80bb0005)

我哪里出错了?

最佳答案

我解决了。诀窍是发出命令的整个路径。我使用which <command>得到了它。另外,我必须在单独的数组列表中传递命令的参数。

IGuestProcess process = null;

ISession session = manager.getSessionObject(); //VirtualBoxManager

machine.lockMachine(session, LockType.Shared); //IMachine

IConsole console = session.getConsole();

IGuest guest = console.getGuest();

IGuestSession guestSession = guest.createSession("registerdUserWithRights", "hisPasswd", "", "");

Long time = 100000L;

GuestSessionWaitResult result = guestSession.waitFor(time, time);

List<String> argumentsForProcess = Arrays.asList("--disabled-password", "--gecos", "'" + name + "'", username);

if(result == GuestSessionWaitResult.Start)
createUser = guestSession.processCreate("/usr/sbin/adduser", argumentsForProcess, null, null, 0L);


ProcessWaitResult waitResult = process.waitFor(1L, time);

if(waitResult == ProcessWaitResult.Start)
System.out.println("started");
guestSession.close();
session.unlockMachine();

关于java - VirtualBox Sdk Java - 从主机操作系统在 guest 操作系统内执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28280777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com