Last active
August 18, 2020 01:17
-
-
Save aberezin/cc668f423592c79f50e38070d4b409da to your computer and use it in GitHub Desktop.
sql-server-odbc-connect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Use python's argparse module in shell scripts | |
# | |
# The function `argparse` parses its arguments using | |
# argparse.ArgumentParser; the parser is defined in the function's | |
# stdin. | |
# | |
# Executing ``argparse.bash`` (as opposed to sourcing it) prints a | |
# script template. | |
# | |
# https://github.com/nhoffman/argparse-bash | |
# MIT License - Copyright (c) 2015 Noah Hoffman | |
argparse(){ | |
argparser=$(mktemp 2>/dev/null || mktemp -t argparser) | |
cat > "$argparser" <<EOF | |
from __future__ import print_function | |
import sys | |
import argparse | |
import os | |
class MyArgumentParser(argparse.ArgumentParser): | |
def print_help(self, file=None): | |
"""Print help and exit with error""" | |
super(MyArgumentParser, self).print_help(file=file) | |
sys.exit(1) | |
parser = MyArgumentParser(prog=os.path.basename("$0"), | |
description="""$ARGPARSE_DESCRIPTION""") | |
EOF | |
# stdin to this function should contain the parser definition | |
cat >> "$argparser" | |
cat >> "$argparser" <<EOF | |
args = parser.parse_args() | |
for arg in [a for a in dir(args) if not a.startswith('_')]: | |
key = arg.upper() | |
value = getattr(args, arg, None) | |
if isinstance(value, bool) or value is None: | |
print('{0}="{1}";'.format(key, 'yes' if value else '')) | |
elif isinstance(value, list): | |
print('{0}=({1});'.format(key, ' '.join('"{0}"'.format(s) for s in value))) | |
else: | |
print('{0}="{1}";'.format(key, value)) | |
EOF | |
# Define variables corresponding to the options if the args can be | |
# parsed without errors; otherwise, print the text of the error | |
# message. | |
if python "$argparser" "$@" &> /dev/null; then | |
eval $(python "$argparser" "$@") | |
retval=0 | |
else | |
python "$argparser" "$@" | |
retval=1 | |
fi | |
rm "$argparser" | |
return $retval | |
} | |
# print a script template when this script is executed | |
if [[ $0 == *argparse.bash ]]; then | |
cat <<FOO | |
#!/usr/bin/env bash | |
source \$(dirname \$0)/argparse.bash || exit 1 | |
argparse "\$@" <<EOF || exit 1 | |
parser.add_argument('infile') | |
parser.add_argument('-o', '--outfile') | |
EOF | |
echo "INFILE: \${INFILE}" | |
echo "OUTFILE: \${OUTFILE}" | |
FOO | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
__version__="20200716-1" | |
ARGPARSE_DESCRIPTION="Connect to sqlserver using the unixODBC driver. Passwords are stored in python keyring." | |
source $(dirname $0)/argparse.bash || ( echo "you must have argparse.bash. See https://github.com/nhoffman/argparse-bash" && exit 1 ) | |
argparse "$@" <<EOF || exit 1 | |
__version__ = "$__version" | |
#action = parser.add_mutually_exclusive_group(required=False) | |
parser.add_argument('-s', '--host', default='transttl-replica-1.acertuslabs.com', help='[default %(default)s]') | |
parser.add_argument('-n', '--dbname', default='transtitle', help='[default %(default)s]') | |
parser.add_argument('-u', '--username', default='aberezin', help='[default %(default)s]') | |
#parser.add_argument('-p', '--port', default=0, type=int, help='') | |
parser.add_argument('-c', '--no-password-cache', action='store_true', help='Ask for a pw and store it in the keyring with attempting to lookup first.') | |
parser.add_argument('-v', '--version', action='version', version="echo $__version__; exit", help='Print the version and exit') | |
EOF | |
SYSTEM=$HOST';db='$DBNAME | |
if [[ $NO_PASSWORD_CACHE != 'yes' ]]; then | |
PW=$(keyring get "$SYSTEM" $USERNAME) | |
fi | |
if [ -z $PW ]; then | |
#read -s -p "Password: " PW | |
keyring set "$SYSTEM" $USERNAME | |
PW=$(keyring get "$SYSTEM" $USERNAME) | |
fi | |
con_str='DRIVER={ODBC Driver 17 for SQL Server};SERVER='$HOST';DATABASE='$DBNAME';UID='$USERNAME';PWD='$PW | |
con_str_display='DRIVER={ODBC Driver 17 for SQL Server};SERVER='$HOST';DATABASE='$DBNAME';UID='$USERNAME';PWD=****' | |
echo "$con_str_display" | |
isql -k "$con_str" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment