#!/bin/bash # # nodessh.sh # # Convenience script for running commands over ssh to BCPC nodes when # their cobbler root passwd is available in the chef databags. # # Parameters: # $1 is the name of chef environment file, without the .json file extension # $2 is the IP address or name of the node on which to execute the specified command # $3 is the command to execute (use "-" for an interactive shell) # $4 (optional) if 'sudo' is specified, the command will be executed using sudo # if [[ -z "$1" || -z "$2" || -z "$3" ]] { global NAME := $[basename $0] if [[ "$NAME" = nodescp ]] { echo "Usage: $0 'environment' 'nodename|IP address' 'from' 'to'" } else { echo "Usage: $0 'environment' 'nodename|IP address' 'command' (sudo)" } exit } if [[ -z `which sshpass` ]] { echo "Error: sshpass required for this tool. You should be able to 'sudo apt-get install sshpass' to get it" exit } global ENVIRONMENT := $1 global IP := $2 global COMMAND := $3 # verify we can access the data bag for this environment global KNIFESTAT := $[knife data bag show configs $ENVIRONMENT !2 > !1 | grep ERROR] if [[ ! -z "$KNIFESTAT" ]] { echo "knife error $KNIFESTAT when showing the config" exit } # get the cobbler root passwd from the data bag global PASSWD := $[knife data bag show configs $ENVIRONMENT | grep "cobbler-root-password:" | awk ' {print $2}] if [[ -z "$PASSWD" ]] { echo "Failed to retrieve 'cobbler-root-password'" exit } # check if the specified host is responding #UP=`ping -c 1 $IP | grep ttl` #if [[ -z "$UP" ]]; then # echo "Node $NODEFQDN($IP) doesn't appear to be on-line" # exit #fi if [[ $(basename "$0") == nssh ]] { global SSH1COMMON := ''"" } else { global SSHCOMMON := '"-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o VerifyHostKeyDNS=no'" } proc apply_command { @Argv global RES := $Status if [[ "$RES" -ne 0 ]] { echo "Executing '$ifsjoin(Argv)' failed" exit $RES } } if [[ $(basename "$0") == nodescp ]] { global SCPCMD := ""scp $SSHCOMMON"" apply_command sshpass -p $PASSWD $SCPCMD -p $3 "ubuntu@$2:$4" } else { # finally ... run the specified command # the -t creates a pty which ensures we see errors if the command fails global SSHCMD := ""ssh $SSHCOMMON"" if [[ "$4" == sudo ]] { # if we need to sudo, pipe the passwd to that too apply_command sshpass -p $PASSWD $SSHCMD -t ubuntu@$IP "echo $PASSWD | sudo -S $COMMAND" } else { # not sudo, do it the normal way if [[ "$COMMAND" == - ]] { echo "You might need this : cobbler_root = $PASSWD" apply_command sshpass -p $PASSWD $SSHCMD -t ubuntu@$IP } else { apply_command sshpass -p $PASSWD $SSHCMD -t ubuntu@$IP $COMMAND } } } (CommandList children: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (LogicalOr left: (BoolUnary op_id:BoolUnary_z child:{(DQ ($ VSub_Number "$1"))}) right: (LogicalOr left: (BoolUnary op_id:BoolUnary_z child:{(DQ ($ VSub_Number "$2"))}) right: (BoolUnary op_id:BoolUnary_z child:{(DQ ($ VSub_Number "$3"))}) ) ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:NAME) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [(C {(basename)} {(DQ ($ VSub_Number "$0"))})] ) left_token: spids: [72 78] ) } spids: [71] ) ] spids: [71] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobEqual left: {(DQ ($ VSub_Name "$NAME"))} right: {(nodescp)} ) ) terminator: ) ] action: [ (C {(echo)} { (DQ ("Usage: ") ($ VSub_Number "$0") (" 'environment' 'nodename|IP address' 'from' 'to'") ) } ) ] spids: [-1 96] ) ] else_action: [ (C {(echo)} { (DQ ("Usage: ") ($ VSub_Number "$0") (" 'environment' 'nodename|IP address' 'command' (sudo)") ) } ) ] spids: [109 121] ) (C {(exit)}) ] spids: [-1 68] ) ] spids: [-1 126] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolUnary op_id: BoolUnary_z child: { (CommandSubPart command_list: (CommandList children:[(C {(which)} {(sshpass)})]) left_token: spids: [135 139] ) } ) ) terminator: ) ] action: [ (C {(echo)} { (DQ ( "Error: sshpass required for this tool. You should be able to 'sudo apt-get install sshpass' to get it" ) ) } ) (C {(exit)}) ] spids: [-1 144] ) ] spids: [-1 156] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:ENVIRONMENT) op: Equal rhs: {($ VSub_Number "$1")} spids: [159] ) ] spids: [159] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:IP) op:Equal rhs:{($ VSub_Number "$2")} spids:[162])] spids: [162] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:COMMAND) op: Equal rhs: {($ VSub_Number "$3")} spids: [165] ) ] spids: [165] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:KNIFESTAT) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (SimpleCommand words: [ {(knife)} {(data)} {(bag)} {(show)} {(configs)} {($ VSub_Name "$ENVIRONMENT")} ] redirects: [(Redir op_id:Redir_GreatAnd fd:2 arg_word:{(1)} spids:[186])] ) (C {(grep)} {(ERROR)}) ] negated: False ) ] ) left_token: spids: [173 194] ) } spids: [172] ) ] spids: [172] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (LogicalNot child: (BoolUnary op_id:BoolUnary_z child:{(DQ ($ VSub_Name "$KNIFESTAT"))}) ) ) terminator: ) ] action: [ (C {(echo)} {(DQ ("knife error ") ($ VSub_Name "$KNIFESTAT") (" when showing the config"))}) (C {(exit)}) ] spids: [-1 211] ) ] spids: [-1 225] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:PASSWD) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(knife)} {(data)} {(bag)} {(show)} {(configs)} {($ VSub_Name "$ENVIRONMENT")}) (C {(grep)} {(DQ ("cobbler-root-password:"))}) (C {(awk)} {(SQ <" {print $2}">)}) ] negated: False ) ] ) left_token: spids: [232 260] ) } spids: [231] ) ] spids: [231] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr:(BoolUnary op_id:BoolUnary_z child:{(DQ ($ VSub_Name "$PASSWD"))})) terminator: ) ] action: [(C {(echo)} {(DQ ("Failed to retrieve 'cobbler-root-password'"))}) (C {(exit)})] spids: [-1 275] ) ] spids: [-1 287] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: { (CommandSubPart command_list: (CommandList children: [(C {(basename)} {(DQ ($ VSub_Number "$0"))})] ) left_token: spids: [314 320] ) } right: {(nssh)} ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:SSH1COMMON) op:Equal rhs:{(DQ )} spids:[332])] spids: [332] ) ] spids: [-1 329] ) ] else_action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:SSHCOMMON) op: Equal rhs: { (DQ ( "-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o VerifyHostKeyDNS=no" ) ) } spids: [339] ) ] spids: [339] ) ] spids: [336 344] ) (FuncDef name: apply_command body: (BraceGroup children: [ (C {(DQ ($ VSub_At "$@"))}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:RES) op: Equal rhs: {($ VSub_QMark "$?")} spids: [359] ) ] spids: [359] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_ne left: {(DQ ($ VSub_Name "$RES"))} right: {(0)} ) ) terminator: ) ] action: [ (C {(echo)} {(DQ ("Executing '") ($ VSub_At "$@") ("' failed"))}) (C {(exit)} {($ VSub_Name "$RES")}) ] spids: [-1 378] ) ] spids: [-1 395] ) ] spids: [351] ) spids: [347 350] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: { (CommandSubPart command_list: (CommandList children: [(C {(basename)} {(DQ ($ VSub_Number "$0"))})] ) left_token: spids: [404 410] ) } right: {(nodescp)} ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:SCPCMD) op: Equal rhs: {(DQ ("scp ") ($ VSub_Name "$SSHCOMMON"))} spids: [422] ) ] spids: [422] ) (C {(apply_command)} {(sshpass)} {(-p)} {($ VSub_Name "$PASSWD")} {($ VSub_Name "$SCPCMD")} {(-p)} {(DQ ($ VSub_Number "$3"))} {(DQ ("ubuntu@") ($ VSub_Number "$2") (":") ($ VSub_Number "$4"))} ) ] spids: [-1 419] ) ] else_action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:SSHCMD) op: Equal rhs: {(DQ ("ssh ") ($ VSub_Name "$SSHCOMMON"))} spids: [464] ) ] spids: [464] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {(DQ ($ VSub_Number "$4"))} right: {(sudo)} ) ) terminator: ) ] action: [ (C {(apply_command)} {(sshpass)} {(-p)} {($ VSub_Name "$PASSWD")} {($ VSub_Name "$SSHCMD")} {(-t)} {(ubuntu) (Lit_Other "@") ($ VSub_Name "$IP")} {(DQ ("echo ") ($ VSub_Name "$PASSWD") (" | sudo -S ") ($ VSub_Name "$COMMAND"))} ) ] spids: [-1 487] ) ] else_action: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {(DQ ($ VSub_Name "$COMMAND"))} right: {(-)} ) ) terminator: ) ] action: [ (C {(echo)} {(DQ ("You might need this : cobbler_root = ") ($ VSub_Name "$PASSWD"))}) (C {(apply_command)} {(sshpass)} {(-p)} {($ VSub_Name "$PASSWD")} {($ VSub_Name "$SSHCMD")} {(-t)} {(ubuntu) (Lit_Other "@") ($ VSub_Name "$IP")} ) ] spids: [-1 541] ) ] else_action: [ (C {(apply_command)} {(sshpass)} {(-p)} {($ VSub_Name "$PASSWD")} {($ VSub_Name "$SSHCMD")} {(-t)} {(ubuntu) (Lit_Other "@") ($ VSub_Name "$IP")} {(DQ ($ VSub_Name "$COMMAND"))} ) ] spids: [569 593] ) ] spids: [518 596] ) ] spids: [452 598] ) ] )