ssh host1 ssh host2 “cmd1; cmd2”

This simple automation script demonstrates how to ssh 2 layers into a machine to run a command.

Obviously you need to set up authorized_keys.

#!/bin/bash
date=0710
tgz=nx$date.tgz
set -x
ssh -q uidev1 ssh -q bxbrdr2 "tar cfvz $tgz /data/mnt/captures/tp5/lfeeds/nysemkt/nysemkt-primary.2017${date}_03*"
ssh -q uidev1 ssh -q bxbrdr2 "hostname; ls -l ~/$tgz"
ssh -q uidev1 scp -pq bxbrdr2:$tgz .
set +x
ssh -q uidev1 "hostname; ls -l ~/$tgz"
scp -pq uidev1:$tgz .
ls -l $tgz

perl q(..) range operator

there are 2 largely unrelated operators:

  • list range operator,
  • scalar range operator

#1 most common usage (list context) iterates over a range of integers or strings, similar to python xrange(). [1] has clear examples.

#2 (possibly more powerful) usage is during text file line-by-line processing. The operators selects a continuous chunk of lines.

Overall, [1] has too many technicalities to obscure the key, useful features.

Unintuitively, this is considered scalar context [1] has useful example, but need to understand the basics first.

[1] http://perldoc.perl.org/perlop.html#Range-Operators

bash script show`trap,read,waiting for gdb-attach

#1 valuable feature is the wait for gdb to attach, before unleashing the data producer.

#2 signal trap. I don’t have to remember to kill off background processes.

# Better source this script. One known benefit -- q(jobs) command would now work

sigtrap(){
  echo Interrupted
  kill %1 %2 %3 %4 # q(jobs) can show the process %1 %2 etc
  set -x
  trap - INT
  trap # show active signal traps
  sleep 1
  jobs
  set +x
}

set +x
ps4 # my alias to show relevant processes
echo -e "\njobs:"
jobs
echo -en "\nContinue? [y/any_other_key] "
unset REPLY; read $REPLY
[ "$REPLY" = "y" ] || return

trap "sigtrap" INT # not sure what would happen to the current cmd and to the shell

base=/home/vtan//repo/tp/plugins/xtap/nysemkt_integrated_parser/
pushd $base
make NO_COMPILE=1 || return
echo '---------------------'
popd
/bin/rm --verbose $base/*_vtan.*log /home/vtan/nx_parser/working/CSMIParser_StaticDataMap.dat

set -x

#If our parser is a client to rebus server, then run s2o as a fake rebus server:
s2o 40490|tee $base/2rebus_vtan.bin.log | decript2 ctf -c $base/etc/cdd.cfg > $base/2rebus_vtan.txt.log 2>&1 &

#if our parser is a server outputing rtsd to VAP, then run c2o as a fake client:
c2o localhost 40492|tee $base/rtsd_vtan.bin.log | decript2 ctf -c $base/etc/cdd.cfg > $base/rtsd_vtan.txt.log 2>&1 &

# run a local xtap process:
$base/shared/tp_xtap/bin/xtap -c $base/etc/test_replay.cfg > $base/xtap_vtan.txt.log 2>&1 &
#sleep 3; echo -en "\n\n\nDebugger ready? Start pbflow? [y/any_other_key] "
#unset REPLY; read $REPLY; [ "$REPLY" = "y" ] || return

# playback some historical data, on a multicast port:
pbflow -r999 ~/captured/ax/arcabookxdp1-primary 224.0.0.7:40491 &

set +x
jobs
trap

append.bashrc.txt

 

<< ______________end_of_comment_1________________
modified [28 Apr 2017]
______________end_of_comment_1________________
[ -z "$(echo $- | grep i)" ] && return # this same line should be on top of append.bashrc_citi.txt
# # # # # # # # # # # # # # # # # #
#
# << var assignment >> should be first
#
# # # # # # # # # # # # # # # # # # # # # #
export PS1='\n\s!\! \u@\h [ \t \d ] \w/ \n\$ '
export EDITOR=vi # primarily for sqlplus
export VIMINIT=":se nu | :map v :w" 
export EXINIT=$VIMINIT # ignored if .vimrc found!
LESS="--ignore-case"
export TMOUT=987654; [ $root ] && export TMOUT=100600
HISTSIZE=900
set -o emacs
set +o nounset
export PATH=/usr/xpg4/bin:$PATH; [ $(id -u) = 0 ] && root=yesur 
set +o noclobber; [ $root ] && set -o noclobber
sav=$LOGNAME
# # # # # # # # # # # # # # # # # # # # # #
#
# PAGER and TERM:
#
# # # # # # # # # # # # # # # # # # # # # #
export PAGER=more
export PAGERE=more
if [ -z "$(which less |grep '^no less')" ]; then
 export PAGER="less" # breaking some x terminals but ok in "console"
 export PAGERE="less -E" # -E needed for dir-viewing, file-viewing but affects man 
fi
export TERM=vt100 # 'linux' breaks vi in gnome-terminal; 'linux' imperfect for solaris
# # # # # # # # # # # # # # # # # # # # # #
#
# << aliases >> ... sortable
#
# # # # # # # # # # # # # # # # # # # # # #
alias %=cd_l # typing trainer
alias ..='cd_l ..'
alias cp='cp -i' # defuse
alias egi='export |grep -i'
alias h=history
alias hgi="history |grep -i"
alias j1="fg %1"
alias j2="fg %2"
alias killthem="perl -pe 's/^\S+(\s+\d+).*/\1/ or s/.*//'|xargs -pt kill" # p str1 | killthem
alias m=$PAGER
alias mv='mv -i' # defuse
alias p=ps_grep
alias path=" echo \$PATH |perl -pe 's/:/\n/g' |sort -u|$PAGERE "
alias rm=myrm
alias s=' source $HOME/.bashrc '
alias t='l -t'
# # # # # # # # # # # # # # # # # # # # # #
#
# << functions >> ... difficult to sort
#
# # # # # # # # # # # # # # # # # # # # # #
cd_l(){
 [ $# -eq 0 ] && l && return
 [ -n "$( file $* | grep directory )" ] && cd $* && l && return
 [ -n "$( file $* | perl -ne 'print if /text|script/' )" ] && m $* &&
/bin/echo -e "\n\n" && l $*
}
d(){
 [ $# -ne 0 ] && cd $*
 [ `pwd` = '/' ] && target=/ && echo I am in /
 echo "In MB :"
 eval du -ks * |sort -n|perl -pe 's|^(\d+)|$1/1000|e'
}
notinuse_g(){ # bug with g -v
 pattern=$1; shift
 cmd=" grep -i \"$pattern\" $* "
 /usr/bin/printf '%s\n' "cmd=__$cmd __"
 eval $cmd |$PAGERE
}
l(){ # can't be completely replaced by 'cd_l' or ']', because "cd_l -tr dir1" is confusing and should be avoided
 /bin/ls -alFs $* |$PAGERE
}
myrm(){
 cmd="mv -i $* /var/tmp/$sav/ "
 /usr/bin/printf '%s\n' "cmd=__$cmd __"
 eval $cmd
}
ps_grep(){
 ## cmd1='/bin/ps -ef' # truncation risk
 # ps auxwww : inconsistent for root vs non-root
 cmd1='/usr/ucb/ps auxwww' # |grep -v grep -- no cos some real commands contain grep
 for f in $*; do
 cmd1="$cmd1 | g $f"
 done
 eval $cmd1
}
sav(){ # proven since 2007
 suffix=$RANDOM$(date +"%H%d%b") # to avoid misleading readers,make the suffix ambiguous
 for f in $*; do
 f=$(echo $f|perl -pe 's|/$||') # sav dir/
 b=`basename $f`
 (
 cd `dirname $f` ; 
 tar cf - $b | (cd /tmp; tar xpf -)
 /bin/mv -i /tmp/$b $b.b4${sav}edit$suffix
 [ -d $f ] && opt=' -d' 
 eval ls -lt $opt $b*
 )
 done
}
# # # # # # # # # # # # # # # # # # #
#
# << exa group of companies >> ... an exercise in grouping
#
# # # # # # # # # # # # # # # # # # # # # #
exa(){ #testing: exa -t ss ~/.ssh
 local ops
 while is_opt=`echo $1 |perl -ne 'print if /^-/' ` && [ "$is_opt" ]; do
 ops="$ops $1"; shift
 done

 if [ $# -eq 1 ]; then
 fullpath=$1
 shortname=`echo $1|perl -pe 's|/$||;s|^.*/||' `
 else
 fullpath=$2
 shortname=$1
 fi

 [ -x $fullpath ] || return
 # export "$shortname"="$fullpath"
 set $shortname="$fullpath" # not working as of 2017
 eval $shortname="$fullpath"
 prj="$fullpath"
 alias "$shortname"="cd $fullpath;l $ops"
 alias prj=$shortname # USE MORE
}
exa /tmp/
# # # # # # # # # # # # # # # # # # #
#
# << path stuff, potentially dangerous >>
#
# # # # # # # # # # # # # # # # # # # # # #
add_path(){ 
 [ -r $1 ] || return
 [ "$(echo $PATH|grep :$1)" ] && return
 # check world write perm, esp for root
 PATH=$PATH:$1
}
set_manpath(){ # very slow on some systems. Run this when u need it.
 for d in `ls -d /[uo]*/*/man /[uo]*/*/*/man /[uo]*/*/*/*/man`; do
 export MANPATH=$MANPATH:$d
 done
}
set_path(){
 add_path /usr/sbin
 add_path /usr/bin
 add_path /sbin
 add_path /usr/local/bin
 add_path /usr/cluster/bin
 add_path /usr/openwin/bin/
 add_path /usr/ucb
 add_path $ORACLE_HOME/bin
 add_path /usr/sfw/bin # pre-installed
 add_path /opt/csw/bin
 [ $root ] && return
 add_path . 
 add_path $HOME/bin
}
set_path