Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/bin/bash | |
|
Will Drewry
2010/06/08 15:11:15
Make sure that you add bash to a dependency somewh
| |
| 2 # Copyright (c) 2009-2010 The Chromium OS Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 HELP=' | |
| 7 help | |
| 8 Display this help | |
| 9 | |
| 10 exit | |
| 11 Exit crosh | |
| 12 | |
| 13 ssh <user> <host> [<port>] | |
| 14 Open an ssh connection to the given <host>, as <user>. The <port> parameter | |
| 15 is optional and defaults to 22. | |
| 16 ' | |
| 17 | |
| 18 if [ "$1" == "--dev" -o -f "/root/.dev_mode" ]; then | |
|
Will Drewry
2010/06/08 15:11:15
can we _not_ encode /root/.dev_mode here too or do
| |
| 19 . "$(dirname "$0")/crosh-dev" | |
| 20 fi | |
| 21 | |
| 22 function cmd_help() { | |
| 23 echo "$HELP" | |
| 24 } | |
| 25 | |
| 26 function cmd_exit() { | |
| 27 exit | |
| 28 } | |
| 29 | |
| 30 function cmd_ssh() { | |
| 31 local user="$1" | |
| 32 local host="$2" | |
| 33 local port="${3:-22}" | |
| 34 | |
| 35 if [ -z "$user" ]; then | |
| 36 echo "Missing required parameter: user" | |
| 37 return | |
| 38 elif ! check_username "$user"; then | |
| 39 echo "Invalid user: $user" | |
| 40 return | |
| 41 fi | |
| 42 | |
| 43 if [ -z "$host" ]; then | |
| 44 echo "Missing required parameter: host" | |
| 45 return | |
| 46 elif ! check_hostname "$host"; then | |
| 47 echo "Invalid host: $host" | |
| 48 return | |
| 49 fi | |
| 50 | |
| 51 if [ ! -z "$port" ]; then | |
| 52 if ! check_digits "$port"; then | |
| 53 echo "Invalid port: $port" | |
| 54 return | |
| 55 fi | |
| 56 fi | |
| 57 | |
| 58 echo "open: host: $host, user: $user, port: $port" | |
| 59 ssh -p "$port" "$user@$host" | |
| 60 } | |
| 61 | |
| 62 function dispatch() { | |
| 63 local line="$1" | |
| 64 local command="" | |
| 65 local params="" | |
| 66 | |
| 67 local space_pos=$(expr index "$line" ' ') | |
| 68 | |
| 69 if [ $space_pos == 0 ]; then | |
| 70 command=$line | |
| 71 else | |
| 72 command=${line:0:$((space_pos - 1))} | |
| 73 params=${line:$space_pos} | |
| 74 fi | |
| 75 | |
| 76 if [ -z $(typeset -F "cmd_$command") ]; then | |
| 77 echo "Unknown command: '$command'" | |
| 78 else | |
| 79 command="cmd_$command" | |
| 80 $command $params | |
| 81 fi | |
| 82 } | |
| 83 | |
| 84 # Checks that a given string starts with an alphanumeric, and contains only | |
| 85 # alphanumeric, '.' or '-' characters | |
| 86 function check_hostname() { | |
| 87 expr "$1" : '^[[:alnum:]][[:alnum:].\-]*$' > /dev/null | |
| 88 } | |
| 89 | |
| 90 # Checks that a given string starts with an alphanumeric, and contains only | |
| 91 # alphanumeric, '.' or '-' characters | |
| 92 function check_hostname() { | |
|
Will Drewry
2010/06/08 15:11:15
two check_hostnames? :)
| |
| 93 expr "$1" : '^[[:alnum:]][[:alnum:].\-]*$' > /dev/null | |
| 94 } | |
| 95 | |
| 96 # Checks that a given string starts with an alphanumeric, and contains only | |
| 97 # alphanumeric and zero or more of ".~%$^\-" | |
| 98 function check_username() { | |
| 99 expr "$1" : '^[[:alnum:]][[:alnum:].~%$^\-]*$' > /dev/null | |
|
Will Drewry
2010/06/08 15:11:15
Are these characters just to cover the weird overl
| |
| 100 } | |
| 101 | |
| 102 function check_digits() { | |
| 103 expr "$1" : '^[[:digit:].]*$' > /dev/null | |
|
Will Drewry
2010/06/08 15:11:15
Why is the dot in there?
| |
| 104 } | |
| 105 | |
| 106 function repl() { | |
| 107 echo "Welcome to crosh, type 'help' for a list of commands" | |
| 108 echo | |
| 109 | |
| 110 while [[ 1 ]]; do | |
| 111 if read -p "crosh> " -e LINE_; then | |
| 112 if [ ! -z "$LINE_" ]; then | |
| 113 history -s "$LINE_" | |
| 114 dispatch "$LINE_" | |
| 115 fi | |
| 116 else | |
| 117 echo | |
| 118 return 1 | |
| 119 fi | |
| 120 done | |
| 121 } | |
| 122 | |
| 123 HISTFILE="$HOME/.ssh_history" | |
| 124 history -r $HISTFILE | |
| 125 | |
| 126 repl | |
| 127 | |
| 128 history -w $HISTFILE | |
| OLD | NEW |