Alexey Kodanev | 18739ff | 2014-04-03 12:36:23 +0400 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # Copyright (c) 2014 Oracle and/or its affiliates. All Rights Reserved. |
| 3 | # |
| 4 | # This program is free software; you can redistribute it and/or |
| 5 | # modify it under the terms of the GNU General Public License as |
| 6 | # published by the Free Software Foundation; either version 2 of |
| 7 | # the License, or (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it would be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program; if not, write the Free Software Foundation, |
| 16 | # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | # |
| 18 | # Author: Alexey Kodanev <alexey.kodanev@oracle.com> |
| 19 | # |
| 20 | |
| 21 | [ -z "$TST_LIB_LOADED" ] && . test.sh |
| 22 | |
| 23 | # Run command on remote host. |
| 24 | # Options: |
| 25 | # -b run in background |
| 26 | # -s safe option, if something goes wrong, will exit with TBROK |
| 27 | # -c specify command to run |
| 28 | |
| 29 | tst_rhost_run() |
| 30 | { |
| 31 | # this is needed to run tools/apicmds on remote host |
| 32 | local pre_cmd= |
| 33 | local post_cmd= |
| 34 | local out= |
| 35 | local user="root" |
| 36 | local cmd= |
| 37 | local safe=0 |
| 38 | |
| 39 | OPTIND=0 |
| 40 | |
| 41 | while getopts :bsc:u: opt; do |
| 42 | case "$opt" in |
| 43 | b) |
| 44 | pre_cmd="nohup" |
| 45 | post_cmd=" > /dev/null 2>&1 &" |
| 46 | out="1> /dev/null" |
| 47 | ;; |
| 48 | s) safe=1 ;; |
| 49 | c) cmd=$OPTARG ;; |
| 50 | u) user=$OPTARG ;; |
| 51 | *) |
| 52 | tst_brkm TBROK "tst_rhost_run: unknown option: $opt" |
| 53 | ;; |
| 54 | esac |
| 55 | done |
| 56 | |
| 57 | OPTIND=0 |
| 58 | |
| 59 | [ -z "$cmd" ] && tst_brkm TBROK "command not defined" |
| 60 | |
| 61 | local output= |
| 62 | local ret= |
| 63 | if [ -n "$TST_USE_SSH" ]; then |
| 64 | output=`ssh -n -q $user@$RHOST "sh -c \ |
| 65 | '$pre_cmd $cmd $post_cmd'" $out 2> /dev/null` |
| 66 | else |
| 67 | output=`rsh -n -l $user $RHOST "sh -c \ |
| 68 | '$pre_cmd $cmd $post_cmd'" $out 2> /dev/null` |
| 69 | fi |
| 70 | ret=$? |
| 71 | [ "$ret" -ne 0 -a "$safe" -eq 1 ] && \ |
| 72 | tst_brkm TBROK "failed to run '$cmd' on '$RHOST'" |
| 73 | |
| 74 | [ -z "$out" -a -n "$output" ] && echo "$output" |
| 75 | |
| 76 | return $ret |
| 77 | } |