I need to add an agent of Net-SNMP daemon on a OpenWrt router. Because a script involved by extend directive only can return a string, so I have to use pass directive to involve my script and implement it.
If I return a single line string in the script, everything is OK, but I need to return a table such as route table or online user list of OpenVPN.
Is there a example script that implementing such a agent of Net-SNMP?
This is my script on halfway:
#!/bin/sh
opt=$1
oid=$2
type=$3
value=$4
touch /var/log/snmp_pass.log
echo "passing $opt $oid $type $value" >> /var/log/snmp_pass.log
if [ "$oid" = ".1.3.6.1.4.1.12345" ]; then
prefix=$oid
sub_id=0
else
prefix=${oid%.*}
sub_id=${oid##*.}
fi
if [ "$opt" = "-s" ]; then
# echo $oid
# echo string
# echo "Finished"
exit 0
fi
if [ "$opt" = "-n" ]; then
sub_id=`expr $sub_id + 1`
fi
# if [ "$opt" = "-g" ]; then
# fi
echo "${prefix}.${sub_id}"
echo string
case $sub_id in
[0-8])
echo "Hello$sub_id"
echo "second line"
exit 0
;;
9)
cat /var/log/openvpn-server-status
exit 0
;;
esac
exit 1
Team,
I am not able to figure out what is the issue with my indentation or syntax. can any one hint? I tried it on linux and i get below error:
error: output
line 23: syntax error near unexpected token `['
line 23: ` if [ $CLUSTER_NAME == prod.$test_environment ]; then'
#!/bin/bash
sops_ops() {
sops --version
if [ "$?" -eq "0" ]; then
echo "proceed sops ops"
else
echo "check sops binary"
fi
read -p 'Enter cluster_NAME: ' CLUSTER_NAME
test_environment="test.nvaa.com"
test1_environment="test1.nvaa.com"
case "${$CLUSTER_NAME}" in
prod.$test_environment) ;;
dev.$test1_environment) ;;
*) echo "Invalid option: ${CLUSTER_NAME}" 1>&2 && exit 1 ;;
if [ $CLUSTER_NAME == prod.$test_environment ]; then
printf "got test cluster $CLUSTER_NAME"
elif [ $CLUSTER_NAME == dev.$test1_environment ];then
printf "got test1 cluster $NAME"
echo "not found cluster"
else
echo "Environment not available"
fi
}
sops_ops
You're are missing esac. Add it after the following snippet :
case "${$CLUSTER_NAME}" in
prod.$test_environment) ;;
dev.$test1_environment) ;;
*) echo "Invalid option: ${CLUSTER_NAME}" 1>&2 && exit 1 ;;
Problem:
I need to make this bash script to choose based on the user inputs. Example how can i add choices? such that user just select from 1 to 3 and that is set in variable CLUSTER_NAME.
choices are test.com, try.com and me.com
Script
#!/bin/bash
sops_ops() {
sops --version
if [ "$?" -eq "0" ]; then
echo "proceed sops ops"
else
echo "check sops binary"
fi
read -p 'Enter cluster_NAME: = ' CLUSTER_NAME
test_environment="test.com"
test1_environment="test1.com"
test2_environment="test2.com"
case "${$CLUSTER_NAME}" in
prod.$test_environment) ;;
dev.$test1_environment) ;;
stage.$test2_environment) ;;
test.$test_environment) ;;
*) echo "Invalid option: ${CLUSTER_NAME}" 1>&2 && exit 1 ;;
if [ $CLUSTER_NAME = test.$test_env ];then
printf "got cluster $CLUSTER_NAME"
elif [ $CLUSTER_NAME = "test.test.com" ];then
printf "got dev cluster $CLUSTER_NAME"
echo "not found cluster"
else
echo "Environment not available"
fi
}
sops_ops
Question:
How do I do that?
Any help is appreciated!
I'm trying to add my own custom check to nagios.
I have successfully created a bash script, that is executable from nagios user.
The problem is that this script works fine from linux command line, but it's return the unknown status if runned by nagios ( returned in web gui ).
#!/bin/sh
while getopts ":q:c:w:h:u:p" optname
do
case "$optname" in "q") query=$OPTARG
;;
"c") CIRT=$OPTARG
;;
"w") WARN=$OPTARG
;;
"u") user=$OPTARG
;;
"p") pswd=$OPTARG
;;
"h") echo "Useage: check_SQLplus_query -u user -p password -w warning value -c cirtical value"
exit
;;
"?") echo "Unknown option $OPTARG"
exit
;;
":") echo "No argument value for option $OPTARG"
exit
;;
*) # Should not occur
echo "Unknown error while processing options"
exit
;;
esac
done
RETVAL=`sqlplus -s USER/PASSWORD#HOST:1521/DBNAME<<EOF
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF
$query;
EXIT;
EOF`
if [ "$RETVAL" -le "$CIRT" ]
then
echo "OK - $RETVAL"
exit 0
elif [ "$RETVAL" -gt "$CIRT" ] && [ "$RETVAL" -le "$WARN" ]
then
echo "WARNING - $RETVAL"
exit 1
elif [ "$RETVAL" -gt "$WARN" ]
then
echo "CRITICAL - $RETVAL"
exit 2
else
echo "UNKNOWN - $RETVAL"
exit 3
fi
Actually the user, password are "coded" inside script, even if I've already set the getopt to make this script more flexible.
The idea is simple, use sqlplus to get a simple query that return only a number ( like the number of the row like my case ).
Bash line to start the script:
/usr/lib64/nagios/plugins/check_SQLplus_queryPrimavera.sh -q "select count(*) from ADMUSER_PM.REFRDEL" -w 6000000 -c 8000000
I'm trying to create a simple script for logging into various servers via ssh, all keys have been installed and are working but i simply cannot get this script to work for me. Basically there is an option as to which server the user wishes to login to but it keeps throwing up the following error:
': not a valid identifier `INPUT
login.sh: line 24: syntax error near unexpected token `elif'
'ogin.sh: line 24: `elif [ $INPUT -eq 2 ] ; then
The script layout can be found below with dummy info:
#!/bin/bash
echo "What Server would you like to login to?"
echo ""
echo ""
echo "1. Server 1"
echo "2. Server 2"
echo "3. Server 3"
echo "4. Server 4"
echo "5. Exit"
read INPUT
if [ $INPUT -eq 1 ] ; then
echo"Logging in"
echo"..."
ssh root#1.2.3.4 -p 5678
elif [ $INPUT -eq 2 ] ; then
echo"Logging in"
echo"..."
ssh root#1.2.3.4 -p 5678
elif [ $INPUT -eq 3 ] ; then
echo"Logging in"
echo"..."
ssh root#1.2.3.4 -p 5678
elif [ $INPUT -eq 4 ] ; then
echo"Logging in"
echo"..."
ssh root#1.2.3.4 -p 5678
elif [ $INPUT -eq 5 ] ; then
exit 0
else
echo "invalid choice"
return
fi
Any help would be greatly appreciated, relatively new to using bash and this is just annoying me now!
looks like you created this file on windows.
try, using dos2unix like:
dos2unix <your_script>
This answer is really just a comment, but comments are not suitable for code. You're script could be greatly simplified. Consider something like:
#!/bin/bash
servers=( host1 host2 host3 )
ips=( 192.168.1.1 192.168.1.2 192.168.1.3 )
ports=( 123 22 33 )
select server in ${servers[#]}; do
echo "Logging into $server..."
ssh root#${ips[$REPLY]} -p ${ports[$REPLY]}
break
done
(Although it's not at all clear why you would want to specify the IP addresses rather than using the hostname!)
Tried your script by copy pasting, it worked. I modified a little more on it to get the invalid choice option to work.
Errm... sorry, but it works for me?
#!/bin/bash
while true ; do
echo "What Server would you like to login to?"
echo ""
echo ""
echo "1. Server 1"
echo "2. Server 2"
echo "3. Server 3"
echo "4. Server 4"
echo "5. Exit"
read INPUT
if [ $INPUT -eq 1 ] ; then
echo "Logging in 1"
echo "..."
ssh root#1.2.3.4 -p 5678
elif [ $INPUT -eq 2 ] ; then
echo "Logging in 2"
echo "..."
ssh root#1.2.3.4 -p 5678
elif [ $INPUT -eq 3 ] ; then
echo "Logging in 3"
echo "..."
ssh root#1.2.3.4 -p 5678
elif [ $INPUT -eq 4 ] ; then
echo "Logging in 4"
echo "..."
ssh root#1.2.3.4 -p 5678
elif [ $INPUT -eq 5 ] ; then
exit 0
else
echo "invalid choice"
fi
done
I'll throw myself in front of the SO bus by answering the question you didn't ask on this one. Why not use select?
echo 'Which server would you like to log into?'
select server in 192.168.0.1 192.168.0.2 192.168.0.3 192.168.0.4; do
printf 'Logging in to server %s...\n' "$server"
ssh "$server" -p "$port"
done
If you don't like select, then why not at least use case?
read -p 'Which server do you want? ' input
case "$input" in
whatever)
printf 'Logging in to server %s...\n' "$server"
ssh "$server" -p "$port"
;;
*)
echo 'Invalid option'
;;
esac
I realize this doesn't answer your question, but you said you were new to bash. I'd suggest using the case statement instead of a mess of ifs and I also added a prompt to your read statement (with the -p option). You might also look into the select statement instead of the case.
#!/bin/bash
echo "What Server would you like to login to?"
echo ""
echo ""
echo "1. Server 1"
echo "2. Server 2"
echo "3. Server 3"
echo "4. Server 4"
echo "5. Exit"
read -p "cmd> " INPUT
case $INPUT in
1)
echo "Logging in"
echo "..."
echo ssh root#1.2.3.4 -p 5678
;;
2)
echo "Logging in"
echo "..."
echo ssh root#1.2.3.4 -p 5678
;;
3)
echo "Logging in"
echo "..."
echo ssh root#1.2.3.4 -p 5678
;;
4)
echo "Logging in"
echo "..."
echo ssh root#1.2.3.4 -p 5678
;;
5)
echo "exiting"
exit 0
;;
*)
echo "invalid choice"
return
;;
esac