Can someone recode this start.sh script based on CentOS?
Its now based on Ubuntu
#!/usr/bin/expect -f
lassign $argv pssword port mod map num hostname rconpassword slots
spawn-fcgi su - et -c cd et -c "./etded.x86 +set s_num=$num +set net_port $port +set dedicated 2 +set fs_game $mod +set logfile 0 +set sv_punkbuster 0 +set sv_hostname \'$hostname\' +set rconpassword $rcon +set g_password '$password' +set sv_maxclients $slots +set map $map"
> /dev/null 2>&1 &
expect "Password:" { send "$pssword\r" }
expect "# " { send "q" }
exit
Thanks!
Your expect is probably built on Tcl 8.4, so change
lassign $argv pssword port mod map num hostname rconpassword slots
to
foreach {pssword port mod map num hostname rconpassword slots} $argv {break}
Related
I asked the dark side and here's what it printed.....
#!/bin/bash
for interface in $(ip addr show | awk '/^[0-9]+:/ {print $2}' | tr -d :)
do
if ! ip addr show $interface | awk '/inet / {print $2}' | grep -q . ; then
echo $interface
fi
done
I want to add n+ variable directly so the output will be the interfaces that is not used by the system,
Done
I have these two scripts, one for Linux and one for Mac, I hope they serve you because I have tested them on Linux Ubuntu and Mac.
on linux
#!/bin/bash
# This script will output unused interfaces on Linux
# Get list of all interfaces
interfaces=$(ifconfig -a | grep -o '^[^ ]*:' | tr -d :)
# Loop through each interface
for interface in $interfaces; do
# Check if interface is up
if [[ $(ifconfig $interface | grep -c 'UP') -eq 0 ]]; then
# Output interface name
echo "$interface is unused"
fi
done
on mac
#!/bin/bash
# Get list of all network interfaces
interfaces=$(networksetup -listallnetworkservices | tail -n +2)
# Loop through each interface
for interface in $interfaces; do
# Get the IP address of the interface
ip=$(ipconfig getifaddr "$interface")
# If the IP address is empty, the interface is unused
if [ -z "$ip" ]; then
echo "$interface is unused"
fi
done
#!/bin/bash
count=0
for interface in $(ip addr show | awk '/^[0-9]+:/ {print $2}' | tr -d :)
do
if ! ip addr show $interface | awk '/inet / {print $2}' | grep -q . ; then
free_interfaces[$count]=$interface
count=$((count + 1))
fi
done
case $count in
0)
echo "No usable interface found."
exit 1
;;
1)
DEFIF=${free_interfaces[0]}
echo "The interface $DEFIF will be used."
;;
*)
echo "Available interfaces to select: "
PS3="Press a number to select an interface to use (1-$count): "
select interface in "${free_interfaces[#]}"; do
DEFIF=$interface
break
done
echo "The interface $DEFIF will be used."
;;
esac
https://dgraph.io/tour/intro/2/
asks for three docker commands being run in different terminals:
zero
alpha
ratel
I'd rather start things from a single script dgraph within Mac OSX 10.3.6 and other environments and use e.g. the terminal app to call the different scripts along the lines of Running a command in a new Mac OS X Terminal window
How can the script below adapted for other environments like Unix and Windows?
dgraph
#!/bin/bash
# WF 2020-08-05
# see https://dgraph.io/tour/intro/2/
version=v20.03.0
#ansi colors
#http://www.csc.uvic.ca/~sae/seng265/fall04/tips/s265s047-tips/bash-using-colors.html
blue='\033[0;34m'
red='\033[0;31m'
green='\033[0;32m' # '\e[1;32m' is too bright for white bg.
endColor='\033[0m'
#
# a colored message
# params:
# 1: l_color - the color of the message
# 2: l_msg - the message to display
#
color_msg() {
local l_color="$1"
local l_msg="$2"
echo -e "${l_color}$l_msg${endColor}"
}
#
# error
#
# show the given error message on stderr and exit
#
# params:
# 1: l_msg - the error message to display
#
error() {
local l_msg="$1"
# use ansi red for error
color_msg $red "Error:" 1>&2
color_msg $red "\t$l_msg" 1>&2
exit 1
}
# show usage
#
usage() {
echo "$0 [-h|--help|-k|--kill"
echo ""
echo "-b | --bash: start a bash terminal shell within the currently running container"
echo "-h | --help: show this usage"
echo "-k | --kill: stop the docker image"
exit 1
}
#
# stop the docker image
#
stopImage() {
color_msg $blue "stopping and removing dgraph image ..."
docker stop dgraph
docker rm dgraph
color_msg $green "...done"
}
#
# start a bash shell within the currently running container
#
bashInto() {
sudo docker exec -it dgraph bash
}
#
# dgraph zero
#
zero() {
docker run -it -p 5080:5080 -p 6080:6080 -p 8080:8080 \
-p 9080:9080 -p 8000:8000 -v ~/dgraph:/dgraph --name dgraph \
dgraph/dgraph:$version dgraph zero
}
#
# dgraph alpha
#
alpha() {
docker exec -it dgraph dgraph alpha --lru_mb 2048 --zero localhost:5080 --whitelist 0.0.0.0/0
}
#
# dgraph ratel
#
ratel() {
docker exec -it dgraph dgraph-ratel
}
me=$0
dir=$(dirname $0)
base=$(basename $0)
if [ $# -lt 1 ]
then
case $base in
dgraph)
# Run Dgraph zero
# And in another, run ratel (Dgraph UI)
# In another terminal, now run Dgraph alpha
for option in zero ratel alpha
do
#echo $dir $option
open -a terminal.app $dir/$option
# wait a bit
sleep 2
done
;;
alpha) alpha;;
ratel) ratel;;
zero) zero;;
esac
fi
# commandline option
while [ "$1" != "" ]
do
option="$1"
case $option in
alpha) alpha;;
ratel) ratel;;
zero) zero;;
-b|--bash) bashInto;;
-k|--kill) stopImage;;
-h|--help) usage;;
esac
shift
done
https://github.com/WolfgangFahl/DgraphAndWeaviateTest/blob/master/scripts/dgraph now has a script working on Linux (tested with travis/Ubuntu 18.04 LTS - bionic).
See also https://travis-ci.org/github/WolfgangFahl/DgraphAndWeaviateTest/jobs/715131236
usage
scripts/dgraph -h
scripts/dgraph [-b|--bash|-h|--help|-k|--kill|-p|--pull]
-b | --bash: start a bash terminal shell within the currently running container
-h | --help: show this usage
-k | --kill: stop the docker image
-p | --pull: pull the docker image
dgraph
#!/bin/bash
# WF 2020-08-05
#
# Starts zero alpha and ratel docker based with one script
#
# see https://dgraph.io/tour/intro/2/
# see https://stackoverflow.com/questions/63260073/starting-zero-alpha-and-ratel-in-a-single-command-e-g-in-macosx-and-other-envir
# see https://discuss.dgraph.io/t/dgraph-start-script/9231
version=v20.03.0
# interative tty option for docker
it="-it"
os=$(uname)
case $os in
Linux)
docker="docker"
it="";;
#docker="sudo docker";;
Darwin)
docker="docker";;
*)
docker="docker";;
esac
#ansi colors
#http://www.csc.uvic.ca/~sae/seng265/fall04/tips/s265s047-tips/bash-using-colors.html
blue='\033[0;34m'
red='\033[0;31m'
green='\033[0;32m' # '\e[1;32m' is too bright for white bg.
endColor='\033[0m'
#
# a colored message
# params:
# 1: l_color - the color of the message
# 2: l_msg - the message to display
#
color_msg() {
local l_color="$1"
local l_msg="$2"
echo -e "${l_color}$l_msg${endColor}"
}
#
# error
#
# show the given error message on stderr and exit
#
# params:
# 1: l_msg - the error message to display
#
error() {
local l_msg="$1"
# use ansi red for error
color_msg $red "Error:" 1>&2
color_msg $red "\t$l_msg" 1>&2
exit 1
}
# show usage
#
usage() {
echo "$0 [-h|--help|-k|--kill"
echo ""
echo "-b | --bash: start a bash terminal shell within the currently running container"
echo "-h | --help: show this usage"
echo "-k | --kill: stop the docker image"
echo "-p | --pull: pull the docker image"
exit 1
}
#
# stop the docker image
#
stopImage() {
color_msg $blue "stopping and removing dgraph image ..."
$docker stop dgraph
$docker rm dgraph
color_msg $green "...done"
}
#
# pull the docker image
#
pullImage() {
color_msg $blue "pulling dgraph image $version ..."
$docker pull dgraph/dgraph:$version
color_msg $green "...done"
}
#
# start a bash shell within the currently running container
#
bashInto() {
$docker exec -it dgraph bash
}
#
# dgraph zero
#
zero() {
# Let’s create a folder for storing Dgraph data outside of the container:
mkdir -p ~/dgraph
$docker run $it -p 5080:5080 -p 6080:6080 -p 8080:8080 \
-p 9080:9080 -p 8000:8000 -v ~/dgraph:/dgraph --name dgraph \
dgraph/dgraph:$version dgraph zero
}
#
# dgraph alpha
#
alpha() {
$docker exec $it dgraph dgraph alpha --lru_mb 2048 --zero localhost:5080 --whitelist 0.0.0.0/0
}
#
# dgraph ratel
#
ratel() {
$docker exec $it dgraph dgraph-ratel
}
me=$0
dir=$(dirname $0)
base=$(basename $0)
if [ $# -lt 1 ]
then
case $base in
dgraph)
# Run Dgraph zero
# And in another, run ratel (Dgraph UI)
# In another terminal, now run Dgraph alpha
for option in zero ratel alpha
do
# make sure linked versions of command are available
if [ ! -f $dir/$option ]
then
color_msg $blue "creating link $dir/$option"
ln $dir/dgraph $dir/$option
fi
#echo $dir $option
color_msg $blue "starting dgraph $option ..."
case $os in
Darwin)
open -a terminal.app $dir/$option
;;
# https://askubuntu.com/questions/46627/how-can-i-make-a-script-that-opens-terminal-windows-and-executes-commands-in-the
Linux)
#for terminal in gnome-terminal xterm konsole
#do
# which $terminal > /dev/null
# if [ $? -eq 0 ]
# then
# $terminal -e $dir/$option
# break
# fi
#done
nohup $dir/$option > /tmp/$option.log 2>&1 &
sleep 2
tail /tmp/$option.log
;;
*)
error "unsupported operating system $os"
esac
# wait a bit
sleep 2
done
;;
alpha) alpha;;
ratel) ratel;;
zero) zero;;
esac
fi
# commandline option
while [ "$1" != "" ]
do
option="$1"
case $option in
alpha) alpha;;
ratel) ratel;;
zero) zero;;
-p|--pull) pullImage;;
-b|--bash) bashInto;;
-k|--kill) stopImage;;
-h|--help) usage;;
esac
shift
done
In a shell script I need to know the value of a variable further down in the code, without running through it first.
This pings $IP which is extracted from $VAR below the while loop.
The $VAR is unknown at the time this is extracted (IP=$(echo $VAR | awk '{print $1}'))
Is it possible to read VAR in before the while loop runs?
The code:
#!/bin/sh
TIMEOUT=10
IP=$(echo $VAR | awk '{print $1}')
while [ $TIMEOUT -ne 0 ];do
ping -c 1 -W 1 "$IP" >/dev/null
rc=$?
if [ $rc -eq 0 ];then
TIMEOUT=0
else
TIMEOUT=$(($TIMEOUT - 1))
echo $TIMEOUT
sleep 1
fi
done
# rest of code to run after while loop
VAR="192.168.0.1 t,r 20,e"
I'm trying to configure tomcat init.d start script to work with multiple instances (at this time 2 instances)
I'm following below sample script to create init.d script
#!/bin/bash
#
# tomcat This shell script takes care of starting and stopping Tomcat
#
# chkconfig: - 80 20
#
### BEGIN INIT INFO
# Provides: tomcat
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start:
# Default-Stop:
# Short-Description: start and stop tomcat
### END INIT INFO
TOMCAT_USER=root
TOMCAT_HOME="/opt/tomcat7/node1"
SHUTDOWN_WAIT=45
tomcat_pid() {
echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'`
}
start() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Tomcat is already running (pid: $pid)"
else
# Start tomcat
echo "Starting tomcat service"
/bin/su - -c "cd $TOMCAT_HOME/bin && $TOMCAT_HOME/bin/startup.sh" $TOMCAT_USER
fi
return 0
}
stop() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Stoping Tomcat"
/bin/su - -c "cd $TOMCAT_HOME/bin && $TOMCAT_HOME/bin/shutdown.sh" $TOMCAT_USER
let kwait=$SHUTDOWN_WAIT
count=0
count_by=5
until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
do
echo "Waiting for processes to exit. Timeout before we kill the pid: ${count}/${kwait}"
sleep $count_by
let count=$count+$count_by;
done
if [ $count -gt $kwait ]; then
echo "Killing processes which didn't stop after $SHUTDOWN_WAIT seconds"
kill -9 $pid
fi
else
echo "Tomcat is not running"
fi
return 0
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Tomcat is running with pid: $pid"
else
echo "Tomcat is not running"
fi
;;
esac
exit 0
problem is tomcat_pid() method returns process ids of all tomcat instances, because of that, the second instance cannot be started. Is there a better method to handle this?
found a workaround, but expecting better solution
using netstat we can find process id via running port number
echo `netstat -tlnp | awk '/:80 */ {split($NF,a,"/"); print a[1]}'`
So i modified the function tomcat_pid() as below
tomcat_pid() {
echo `netstat -tlnp | awk '/:<port> */ {split($NF,a,"/"); print a[1]}'`
}
I have a bash script as follow on an AIX host, myscript.sh:
MODE="$1"
if [ "$MODE" == "start" ]; then
socat -T100 -lf $LOGF -d -d -d -x TCP4-LISTEN:$LISTENINGPORT,bind=$LISTENINGADDR,reuseaddr,fork EXEC:"$0 proxy" &
PID=$!
echo $PID > $PIDFILE
echo "$0 $MODE started (pid=$PID)"
elif [ "$MODE" == "proxy" ]; then
cat - > $TMPFILE
# process $TMPFILE before the SSL connection.
cat $TMPFILE | socat -T 100 -lf $LOGF -d - OPENSSL:$HOST
rm -f $TMPFILE
Everything is fine when I run:
$ cat somefile | myscript.sh proxy | xxd
The problem raise when I connect to the socat listener with a test script:
my $file = $ARGV[0];
my $fsize = -s $file;
my $socket = IO::Socket::INET->new("127.0.0.1:$port")
or die "Couldn't connect to remote host: $!";
$socket->autoflush(1);
binmode($socket);
open (FILE,$file);
binmode(FILE);
my $buffer ;
while(sysread(FILE, $buffer, $blocksize)) {
print $socket $buffer ;
}
print "sent\n" ;
close (FILE) ;
my $answer = <$socket>;
if (defined($answer)) {
print $answer; # never reached
print "...\n" ;
} else {
die "connection reset by peer\n";
}
In myscript.sh, it blocks on the line:
cat - > $TMPFILE
In the test script, it blocks on the line:
my $answer = <$socket>;
At this point, the data has been received by the socat listener (checked with tcpdump).
However, when I Ctrl+c the test script before the socat timeout, the data goes through the pipe (i.e., the SSL server is eventually contacted).
What am I doing wrong?
Update:
Thanks for the tips about cat and EOF. For the time being, I have worked around the problem like so:
timeout 0.2 cat -u - > $TMPFILE 2>>/dev/null
# process $TMPFILE before the SSL connection.
cat $TMPFILE | socat -T 100 -lf $LOGF -d - OPENSSL:$HOST
It's ugly, and a waste 0.2 seconds, I hope to find a better solution.
But it does the job for now. The 2>>/dev/null part is because AIX complains about an invalid counter (related to the timeout command).
My first thought is that there is no linefeed in the data you're trying to receive with cat - or <STDIN> . Both commands in their default behavior will return data once they have a linefeed or their buffers of the file-descriptor is full (4KB by default in Linux).