BASH - 异常错误无法打开chrome远程桌面会话

时间:2013-11-28 09:17:42

标签: bash google-chrome ubuntu centos

如何使Google Chrome远程桌面自动化,以便可以远程初始化或从命令行初始化它,让会话始终运行(不涉及使用x11vnc,teamviewer,logmein)

为什么它会因抛出异常而失败:错误 - 无法打开chrome远程桌面会话?

我有这个BASH脚本打开chrome-browser并允许远程访问,但无法启动。

$ /var/tmp/chromote.sh:0.0 不推荐使用此标志。假设你的意思是--name(窗口名称)。 不推荐使用此标志。假设你的意思是--name(窗口名称)。 错误 - 无法打开chrome远程destop会话

#!/bin/bash
#
# chromote.sh - a simple bash script to start up chrome remote desktop
#              sharing of the linux desktop automatically from a text
#              terminal, and report back the access code.
#
# usage: bash> chromote.sh <X11_display>
#    where <X11_display> is the value of the DISPLAY environment variable
#    that you would use to send X11 commands to the desired screen.
#
# example: bash> chromote.sh :3
#
# author: richard.t.jones at uconn.edu
# version: july 20, 2013
#
# notes:
#
# 1. Besides a running X11 server, three other tools must be installed
#    in order for the script to run.  Both are available as standard
#    linux packages and can be installed using the local package manager.
#    Each of these are assumed to exist within the user's shell PATH.
#   a. chrome - basic browser with remote desktop extension installed
#   b. xdotool - general tool for simulating mouse and keyboard
#                    control events in a X11 environment.
#   c. xclip - general tool for reading from and writing into the
#                  paste buffer in a X11 environment.
#
# 2. The chromote script works by automatically performing the mouse clicks
#    that would be required to start up a chrome remote desktop session in
#    the standard fashion.  As such, it assumes a certain geometry of the
#    google remote desktop sharing page, as well as the way windows pop up
#    on the user's desktop.  To accommodate random changes in either of
#    these, the script supports a "calibration" mode.  To calibrate, try
#   bash> chromote.sh --calibrate <X11_display>

# these settings my be customized for each host
export DISPLAY=:0.0

chromeRD_URL="chrome-extension://gbchcmhmhahfdphkhkmpfmihenigjmpp/main.html"
urlWindowX=140
urlWindowY=80
okButtonX=339
okButtonY=386
shareButtonX=648
shareButtonY=366
codeLeftX=248
codeLeftY=424
codeRightX=449
codeRightY=423
copyItemX=483
copyItemY=436
minimizeWinX=632
minimizeWinY=40

# these are system preferences
chromeWinX=0
chromeWinY=0
chromeWinWidth=700
chromeWinHeight=900

function activate_browser() {
    chromewin=
    for winid in `xdotool search --title "Remote Desktop"`; do
        chromewin=$winid
    done
    if [[ -z $chromewin ]]; then
        chromium-browser >/dev/null 2>&1 &
        sleep 5 # wait for window to appear, adjust as needed
        chromewin=`xdotool getwindowfocus`
        xdotool windowmove $chromewin $chromeWinX $chromeWinY
        xdotool windowsize $chromewin $chromeWinWidth $chromeWinHeight
        xdotool mousemove $urlWindowX $urlWindowY
        xdotool click 1
        xdotool click 1
        xdotool click 1
        xdotool type "$chromeRD_URL
"
        sleep 5 # wait for page to load, adjust as needed
        chromewin=`xdotool search --title "Remote Desktop"`
        if [[ "$chromewin" = "" ]]; then
            echo "Error - unable to open chrome remote destop session" >&2
            exit 2
        fi
    fi
    resp=`xdotool windowactivate $chromewin 2>&1`
    [[ $resp = "" ]]
}

function calibrate() {
    echo "Calibration procedure for chromote.sh is starting."
    echo "If running this script in a terminal within your X11 display,"
    echo "please move it to the right so that it remains visible when"
    echo "the chrome browser window pops up.  DO NOT move the chrome"
    echo "browser window when it opens, because it needs to pop up in"
    echo "exactly the same place on the screen each time it opens."
    echo
    echo -n "Press <enter> when ready:"
    read resp
    scriptwin=`xdotool getwindowfocus`
    chromium-browser >/dev/null 2>&1 &

    sleep 3
    chromewin=`xdotool getwindowfocus`
    xdotool windowmove $chromewin $chromeWinX $chromeWinY
    xdotool windowsize $chromewin $chromeWinWidth $chromeWinHeight
    xdotool windowmove $scriptwin `expr $chromeWinX + $chromeWinWidth` \
                                        $chromeWinY
    xdotool windowactivate $scriptwin
    echo
    echo "A new chrome browser window should have appeared on the screen."
    echo "Go to the first tab and navigate to the chrome remote desktop"
    echo "web page.  If you have not installed it yet, you can find it"
    echo "in the Google Store.  Open a new tab if you need to.  When you"
    echo "reach the right page, there should be a green 'Share' button"
    echo "near the right side of the page."
    echo
    echo "Now click on the window running this script to return the focus"
    echo "here, then hover the mouse at a spot near the start of the url"
    echo "at the top of the browser window as if you were going to"
    echo "double-click it to type a new url.  Do not click anything yet,"
    echo "just position the mouse."
    echo
    echo -n "Press return to continue:"
    read resp
    loc=`xdotool getmouselocation`
    urlWindowX=`echo $loc | awk -F'[ :]' '{print $2}'`
    urlWindowY=`echo $loc | awk -F'[ :]' '{print $4}'`
    xdotool mousemove $urlWindowX $urlWindowY
    xdotool click 1
    xdotool click 1
    xdotool click 1
    echo
    echo "The url of the chrome remote desktop page should be highlighted"
    echo "now in the window.  If not, please hit Ctrl-C and start again."
    echo "If it is, right-click with the mouse and select 'Copy' to save"
    echo "the url in the paste buffer."
    echo
    echo -n "Select this window and press return to continue:"
    read resp
    chromeRD_URL=`xclip -o`
    echo
    echo "A button 'share' (maybe green) should be visible near the right"
    echo "side of the page.  Hover the mouse over the middle of the share,"
    echo "button, but do not click it."
    echo
    echo -n "Press <enter> when ready:"
    read resp
    loc=`xdotool getmouselocation`
    shareButtonX=`echo $loc | awk -F'[ :]' '{print $2}'`
    shareButtonY=`echo $loc | awk -F'[ :]' '{print $4}'`
    xdotool mousemove $shareButtonX $shareButtonY
    xdotool click 1
    echo "A popup window should have appeared, displaying in large"
    echo "letters an access code for you to use when making the remote"
    echo "connection.  Position the mouse to the left of the numbers,"
    echo "as if you were going to drag over them to highlight them, but"
    echo "do not click anything."
    xdotool windowactivate $scriptwin
    echo
    echo -n "Press <enter> when ready:"
    read resp
    loc=`xdotool getmouselocation`
    codeLeftX=`echo $loc | awk -F'[ :]' '{print $2}'`
    codeLeftY=`echo $loc | awk -F'[ :]' '{print $4}'`
    echo
    echo "Now move (not drag) the mouse over to the right side of the"
    echo "number code and position it in the white space past the last digit."
    echo
    echo -n "Press <enter> when ready:"
    read resp
    loc=`xdotool getmouselocation`
    codeRightX=`echo $loc | awk -F'[ :]' '{print $2}'`
    codeRightY=`echo $loc | awk -F'[ :]' '{print $4}'`
    xdotool mousemove $codeLeftX $codeLeftY
    xdotool mousedown 1
    sleep 1
    xdotool mousemove $codeRightX $codeRightY
    xdotool mouseup 1
    xdotool click 3
    echo
    echo "The number code should now be highlighted, and a popup copy/paste"
    echo "menu should be visible.  You now have 5 seconds to position the"
    echo "mouse over the 'Copy' option.  Just hold it there, do not click."
    echo
    sleep 5
    loc=`xdotool getmouselocation`
    copyItemX=`echo $loc | awk -F'[ :]' '{print $2}'`
    copyItemY=`echo $loc | awk -F'[ :]' '{print $4}'`
    xdotool mousemove $copyItemX $copyItemY
    xdotool click 1
    sleep 1
    xdotool click 1
    code=`xclip -o`
    echo
    echo "The number code should be \"$code\".  If this is not correct,"
    echo "hit Ctrl-C now and start again.  If everything worked so far,"
    echo "position the mouse over the 'minimize window' square at the"
    echo "top of the browser window."
    echo
    xdotool windowactivate $scriptwin
    echo -n "Press <enter> when ready:"
    read resp
    loc=`xdotool getmouselocation`
    minimizeWinX=`echo $loc | awk -F'[ :]' '{print $2}'`
    minimizeWinY=`echo $loc | awk -F'[ :]' '{print $4}'`
    echo
    echo "Now stop the desktop sharing session by clicking on the 'Cancel'"
    echo "button in the browser.  A popup should have appeared saying that"
    echo "sharing has stopped.  Position the mouse in the middle of the"
    echo "'ok' button in that popup, but do not click it."
    echo
    echo -n "Press <enter> when ready:"
    read resp
    loc=`xdotool getmouselocation`
    okButtonX=`echo $loc | awk -F'[ :]' '{print $2}'`
    okButtonY=`echo $loc | awk -F'[ :]' '{print $4}'`
    xdotool mousemove $minimizeWinX $minimizeWinY
    xdotool click 1
    echo
    echo "The desktop sharing window should have minimized itself to the"
    echo "taskbar.  If so, everything is correct, and you should copy/paste"
    echo "the settings below into the header of the chromote.sh script,"
    echo "replacing the defaults."
    echo 
    echo "chromeRD_URL=\"$chromeRD_URL\""
    echo "urlWindowX=$urlWindowX"
    echo "urlWindowY=$urlWindowY"
    echo "okButtonX=$okButtonX"
    echo "okButtonY=$okButtonY"
    echo "shareButtonX=$shareButtonX"
    echo "shareButtonY=$shareButtonY"
    echo "codeLeftX=$codeLeftX"
    echo "codeLeftY=$codeLeftY"
    echo "codeRightX=$codeRightX"
    echo "codeRightY=$codeRightY"
    echo "copyItemX=$copyItemX"
    echo "copyItemY=$copyItemY"
    echo "minimizeWinX=$minimizeWinX"
    echo "minimizeWinY=$minimizeWinY"
    exit 0
}

# make sure all necessary tools are available
for tool in chromium-browser xdotool xclip; do
    if [[ ! -x `which $tool` ]]; then
        echo "Required tool $tool not found in the user PATH" >&2
        exit 1
    fi
done

# parse command arguments
if [[ "$1" = "--calibrate" ]]; then
    shift
    export DISPLAY=$1
    calibrate
elif [[ $# != 1 ]]; then
    echo "Usage: chromote.sh [--calibrate] <X11_display>" >&2
    exit 1
else
    export DISPLAY=$1
fi

# look for an existing chrome browser open to the RD page,
# if none exists then start a new browser window
activate_browser
if [[ $? != 0 ]]; then
    echo -n "Chrome Remote Desktop sharing session already connected" 
    echo " on display $1"
    echo "Cancel the existing one before trying to create a new one!"
    exit 1
fi

# click the OK button, in case an old session-closed message is pending
xdotool mousemove $okButtonX $okButtonY
xdotool click 1
sleep 1 # wait for the old session popup to go away, if any

# find the "SHARE" button and press it
xdotool mousemove $shareButtonX $shareButtonY
xdotool click 1
sleep 1 # wait for the new session popup to appear, adjust as needed

# highlight the access code string with the mouse
xdotool mousemove $codeLeftX $codeLeftY
xdotool mousedown 1
sleep 1 # pause for the drag motion to initiate, adjust as needed
xdotool mousemove $codeRightX $codeRightY
xdotool mouseup 1

# copy the highlighted string into the paste buffer
xdotool click 3
sleep 1 # wait for the copy/paste menu to appear, adjust as needed
xdotool mousemove $copyItemX $copyItemY
xdotool click 1
sleep 1 # wait for the copy/paste menu to go away, adjust as needed

# remove the highlighting by clicking in some white space
xdotool click 1

# shrink the chrome window to the taskbar
xdotool mousemove $minimizeWinX $minimizeWinY
xdotool click 1

# report the result and exit
echo the share code is `xclip -o`
exit 0

1 个答案:

答案 0 :(得分:1)

有两种方法可以通过Google Chrome远程桌面实现这一目标,避免浪费我们的时间:VNC,x11vnc背后的nat,防火墙问题。

方法1: 在问题中询问

方法2:

第1步:

sudo apt-get install git subversion
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
# depot_tools are some Google tools such as ninja etc
export PATH="$PATH":`pwd`/depot_tools
mkdir chromium && cd ~/chromium
gclient config https://src.chromium.org/chrome/trunk/src https://chromium-status.appspot.com/lkgr
gclient sync --nohooks
./src/build/install-build-deps.sh
export GYP_GENERATORS='ninja'
export GYP_DEFINES=werror= 
gclient sync --force
# this ninja is not ninja package like seen in ubuntu apt-get
# its in depot_tools
ninja -C src/out/Release chrome

# run it it will fail because of sandbox 
./src/out/Release/chrome

[3803:3803:1128/164845:FATAL:browser_main_loop.cc(167)] Running without the SUID sandbox! See https://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment for more information on developing with the sandbox on.
Aborted

# install the sandbox with depot tools ninja
../../depot_tools/ninja -C out/Debug chrome_sandbox
ninja: Entering directory `out/Debug'
[4/4] LINK chrome_sandbox

# finally run it now
./src/out/Release/chrome
提示:你也可以在没有沙箱的情况下运行,如下所示,但它对攻击不安全等

./src/out/Release/chrome --no-sandbox

第2步:

转到https://cloud.google.com/console?redirected=true#/project 单击创建项目(验证SMS确认等) 激活api如下:

enter image description here

下载json文件以查找客户端ID和密码:

enter image description here

第3步:

A)应用客户端ID和秘密

$ grep "#define GOOGLE_CLIENT_" /home/sun/chromium/src/google_apis/google_api_keys.cc 
#define GOOGLE_CLIENT_ID_MAIN DUMMY_API_TOKEN
#define GOOGLE_CLIENT_SECRET_MAIN DUMMY_API_TOKEN
#define GOOGLE_CLIENT_ID_CLOUD_PRINT DUMMY_API_TOKEN
#define GOOGLE_CLIENT_SECRET_CLOUD_PRINT DUMMY_API_TOKEN
#define GOOGLE_CLIENT_ID_REMOTING DUMMY_API_TOKEN
#define GOOGLE_CLIENT_SECRET_REMOTING DUMMY_API_TOKEN
#define GOOGLE_CLIENT_ID_REMOTING_HOST DUMMY_API_TOKEN
#define GOOGLE_CLIENT_SECRET_REMOTING_HOST DUMMY_API_TOKEN

将其替换为

#define GOOGLE_CLIENT_ID_REMOTING "client id from step 2"
#define GOOGLE_CLIENT_SECRET_REMOTING "client secret from step 2"

B)首先构建自定义Chrome远程桌面Web应用程序

/home/sun/chromium/src/build/gyp_chromium
/home/sun/depot_tools/ninja -C /home/sun/chromium/src/out/Release/ remoting_me2me_host remoting_start_host remoting_webapp

第4步:

这些是由步骤3创建的,现在可以使用

$ ll /home/sun/chromium/src/out/Release/remoting-webap*
-rw-r--r-- 1 root root 8054274 Nov 28 17:50 /home/sun/chromium/src/out/Release/remoting-webapp.v2.zip
-rw-r--r-- 1 root root 8217034 Nov 28 17:50 /home/sun/chromium/src/out/Release/remoting-webapp.zip

$ unzip out/Release/remoting-webapp.zip -d /tmp/
$ wget -P /tmp/ http://download-chromium.appspot.com/dl/Linux_x64
$ unzip /tmp/Linux_x64 -d /tmp/
$ /tmp/chrome-linux/chrome --no-sandbox

第5步:

# Open a tab to “chrome://extensions”
# Toggle on “Developer mode” checkbox
# Click on “Load unpacked extension...” button 
# Select /tmp/remoting-webapp/ folder
# Click on “Add” button 

# Open a new tab
# Click on the Chromoting App icon 
# Follow instructions to authorise the App