You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I made a bash script to act as command in terminal host to do several work on the session:
I added this script as /usr/bin/ccmd file with execution permissions, so it can be used to:
# executes command directly on node:
$ ccmd <node_name><command># connects to node terminal:
$ ccmd <node_name># prints core socket dir:
$ ccmd -p socketdir
# prints a list of node names of running session:
$ ccmd -p nodes
# prints a list of "Node|Ifacename|Proto|IP" of running sesssion:
$ ccmd -p ifaces
# updates /etc/hosts file with all nodenames, interfaces and IPs of running session to provide command autocomplete and more, its useful to add to runtime hook:
$ ccmd -p hostsupdate
# removes data added by hostsupdate, its useful to add to showdown hook:
$ ccmd -p hostsremove
Also I used it to add autocomplete as well, it can be added to .bashrc:
# Bash completition of core nodes to ccmd commandcomplete -W '$(ccmd -p nodes)' ccmd
Here is the script, I can make a PR with this to be added on bin folder.
#!/bin/bash####### Generated by vagrant provisioning ########## Core command shortcut ###
POSITIONAL_ARGS=()
while [[ $#-gt 0 ]];docase$1in
-p|--print)
PRINT="$2"shift# past argumentshift# past value
;;
-h|--help)
echo"Usage: $0 [-h|-p <string>|<node> [<command>]]"echo"Print:"echo" -p <string> Prints information of core emulation"echo" <string> can be:"echo" - socketdir"echo" - nodes"echo" - ifaces"echo" - hostsupdate"echo" - hostsremove"echo"Node Command:"echo" <node> can be one of emulation nodes"echo" [<command>] to run on node or use bash if not specified"echo"Help:"echo" -h, --help Print this help"exit 0
;;
*)
POSITIONAL_ARGS+=("$1") # save positional argshift# past argument
;;
esacdonecsocketdir(){
# core-gui uses another process names than core-gui-legacy but this is not a problem hereecho$(ps aux | grep -oP "/tmp/pycore.[0-99999999999].+?(?=/)"| head -n1 | awk '{print $1}'| cut -d/ -f 1-3)
}
cnodenames(){
echo$(ls $(csocketdir)| grep conf | sed -e "s/.conf$//")
}
runcmd() {
# new core-gui has many process, separated by spaces
SOCKETS_DIR=$(csocketdir)
[ -d"${SOCKETS_DIR}" ] || { echo"Core socket dir not found. Is Core running?";return 1; }
[ -d"${SOCKETS_DIR}/${1}.conf" ] || { echo"Core node directory not found. Does it exists?";return 1; }
vcmd -c $SOCKETS_DIR/$1 -- env TERM=xterm-256color /bin/bash -c "${2:-"bash"}"
}
cifaces() {
fornin$(cnodenames);do
runcmd $n"ip -o addr ls"|whileread -r line;do
iface=$(echo $line| awk '{print $2}')
proto=$(echo $line| awk '{print $3}')
nodeip=$(echo $line| awk '{print $4}'| sed -e "s/\/.*//")echo"$n$iface$proto$nodeip"donedone
}
chostsremovehookdata() {
[ -w /etc/hosts ] || { echo"No permission to write /etc/hosts";return 1; }
sed '/^### CORE HOOK ###/,/^\$/{/^### CORE HOOK ###/!{/^\$/!d}}' /etc/hosts > /tmp/temp_hosts && mv /tmp/temp_hosts /etc/hosts
grep -v "### CORE HOOK ###" /etc/hosts > /tmp/temp_hosts && mv /tmp/temp_hosts /etc/hosts
}
chostsupdatehookdata() {
[ -w /etc/hosts ] || { echo"No permission to write /etc/hosts";return 1; }
chostsremovehookdata
echo"### CORE HOOK ### START - Do not touch this">> /etc/hosts
echo"# Nodes with first IP address will be used as the hostname">> /etc/hosts
echo$"`cifaces | grep " inet "| grep -v " lo "| sort | sort -u -k1,1 | awk '{print $4 " " $1}'`">> /etc/hosts
echo"# Nodes with interface name">> /etc/hosts
echo$"`cifaces | grep " inet "| grep -v " lo "| sort | awk '{print $4 " " $1 "_" $2}'`">> /etc/hosts
echo"### CORE HOOK ### END - Do not touch this">> /etc/hosts
}
set -- "${POSITIONAL_ARGS[@]}"# restore positional parametersif [ -z${PRINT} ];then
runcmd $@elseif [ "$PRINT"=="socketdir" ];then
csocketdir
fiif [ "$PRINT"=="nodes" ];then
cnodenames
fiif [ "$PRINT"=="ifaces" ];then
cifaces
fiif [ "$PRINT"=="hostsupdate" ];then
chostsupdatehookdata
fiif [ "$PRINT"=="hostsremove" ];then
chostsremovehookdata
fifiexit$?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi, I made a bash script to act as command in terminal host to do several work on the session:
I added this script as /usr/bin/ccmd file with execution permissions, so it can be used to:
Also I used it to add autocomplete as well, it can be added to .bashrc:
Here is the script, I can make a PR with this to be added on bin folder.
Beta Was this translation helpful? Give feedback.
All reactions