how to test if redshift connection is successful in R shiny app? - shiny

I'm developing a R shiny app that can pull data from Redshift based on user inputs. I wanted to test if the Redshift connection was successful before proceeding. Is there an existing R function to test if the connection is successful?
We can see the connection in R console while in shiny app I wanted to test it and provide feedback to users. Is it possible to write some codes like following in Shinyserver
con <- DBI::dbConnect(odbc::odbc(),
Driver =driver,
Server= server,
Port = 5439,
Database = database,
UID = uid,
PWD = pwd,
sslmode = "allow")
# Ideally, a line of code like this line of mock codes to return error message
if (connectionSuccess = TRUE) {
stop("Please check your credentials!")
}
Thanks!

I think a workaround, with the same end effect, might be using dbi::dbCanConnect to check if the connection is possible, if so, then you try to connect to the host. You would end up with something similar to:
## Test if the connection is possible and if it authenticates successfully.
connection_check <- dbCanConnect(odbc::odbc(),
Driver =driver,
Server= server,
Port = 5439,
Database = database,
UID = uid,
PWD = pwd,
sslmode = "allow")
if(connection_check) {
## If it does authenticate...
con <- DBI::dbConnect(odbc::odbc(),
Driver =driver,
Server= server,
Port = 5439,
Database = database,
UID = uid,
PWD = pwd,
sslmode = "allow")
} else {
## If it fails...
stop("Please check your credentials!")
}

Related

Unable to query VMs on vcenter server appliance using pyvmomi

I am getting the following error when trying to use pyvmomi to get a list of VMs from the vcenter server appliance.
pyVmomi.VmomiSupport.vim.fault.NoPermission: (vim.fault.NoPermission) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
msg = 'Permission to perform this operation was denied.',
faultCause = <unset>,
faultMessage = (vmodl.LocalizableMessage) [],
object = 'vim.Folder:group-d1',
privilegeId = 'System.View',
missingPrivileges = (vim.fault.NoPermission.EntityPrivileges) [
(vim.fault.NoPermission.EntityPrivileges) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
entity = 'vim.Folder:group-d1',
privilegeIds = (str) [
'System.View'
]
}
]
}
This is my python code :
import atexit
import ssl
from pyVim import connect
from pyVmomi import vim
import pdb
def vconnect(hostIP,port=None):
if (True):
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE # disable our certificate checking for lab
else:
context = ssl.create_default_context()
context.options |= ssl.OP_NO_TLSv1_3
#cipher = 'DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-GCM-SHA256'
#context.set_ciphers(cipher)
pdb.set_trace()
if (port):
service_instance = connect.SmartConnect(host=str(hostIP), # build python connection to vSphere
user="root",
pwd="HagsLoff#1324",
port=port,
sslContext=context)
else:
service_instance = connect.SmartConnect(host=str(hostIP), # build python connection to vSphere
user="root",
pwd="HagsLoff#1324",
sslContext=context)
atexit.register(connect.Disconnect, service_instance) # build disconnect logic
content = service_instance.RetrieveContent()
container = content.rootFolder # starting point to look into
viewType = [vim.VirtualMachine] # object types to look for
recursive = True # whether we should look into it recursively
containerView = content.viewManager.CreateContainerView(container, viewType, recursive) # create container view
children = containerView.view
for child in children: # for each statement to iterate all names of VMs in the environment
summary = child.summary
print(summary.config.name)
# connecting to ESX host
vconnect("192.168.160.160")
# connecting to vcsa VM
vconnect("192.168.160.170", 443)
So I am using a nested ESX that runs on my workstation 16. I have deployed the vcsa on this ESX host via the windows CLI installer. Querying the ESX host works fine whereas querying the vcenter server appliance (vcsa) gives me the above error.
I looked at this discussion which talks about setting 'global permissions'; however on my vcenter server management VM, my 'administration' tab does not look anything like this:
What it instead looks like is this:
So apparently I have a 'vcenter server management' appliance and not what is referred to as the 'vsphere client'.
So with this context set, I have some questions:
Is the error above due to my trial license?
How is the 'vcenter server management (vcsa)' appliance different from the 'vsphere client'?
Is it possible to change 'global permissions' on the vcsa or do I need to get the 'vsphere client' to do that?
I tried adding the default port (443) as mentioned here to no avail. Keen to hear from you soon

Flask mqtt works fine in the web app console (pythonanywhere) but it doesn't work in the web app itself,

I use pythonanywhere for my IoT flask server. My MQTT code runs locally (visualstudio) but fails under pythonanywhere (code below copied from my similar question on pythonanywhere forum).
I configured MQTT credentials and set tls to false and 5 seconds keepalive then I instantiate mqtt=Mqtt(app). In #mqtt.onConnect() function I printed values when rc is zero but it doesn't print in any log file (even I used sys.stderr) meaning that means it doesn't go in this function, therefore publish and onMessage() doesn't work too.
However if I open python in the bash console and import MQTT instance from app file it connects to my broker and when I call the function that publish from the console it publish the message to my broker.
I tried to mqtt=Mqtt() then in main scope mqtt.init_app(app) and also tried in main mqtt.run() didn't work.
app1.config['MQTT_BROKER_URL'] = 'mybroker'
app1.config['MQTT_BROKER_PORT'] = 1883
app1.config['MQTT_USERNAME'] = ' '
app1.config['MQTT_PASSWORD'] = ' '
app1.config['MQTT_KEEPALIVE'] = 5
app1.config['MQTT_TLS_ENABLED'] = False
mqtt_client = Mqtt(app1)
import sys as syss
#mqtt_client.on_connect()
def handle_connect(client,userdata,flags,rc):
if rc == 0:
print('Connected successfully', file = syss.stderr)
mqtt_client.subscribe('esp/copra')
else:
print('Bad connection. Code:', rc , file=syss.stderr)
testmqtt="a"
#mqtt_client.on_message()
def handle_mqtt_message(client,userdata,message):
payload = message.payload.decode()
print("payload is " + payload)
#app1.route("/testmqtt")
def testmqtt():
print("test mqtt here")
mqtt_client.publish('esp/copra',"pythonanywhere")
return{"mqtt": "mqtt"}
if name == "main":
#app.config['SESSION_TYPE'] = 'filesystem'
mqtt_client.init_app(app1)
app1.run()

How can I get around Name too long for LOGINREC in pymssql without changing the username

I am attempting to use pymssql to connect to a client's database, but have thus far been unable to succeed. I believe that the root of the problem is this entry in the stack trace of my TDSDUMP:
dblib.c:761:dbsetlname(0x1ac3e10, <username>#<servername>.database.windows.net, 2)
dblib.c:7929:dbperror((nil), 20042, 0)
dblib.c:7981:20042: "Name too long for LOGINREC field"
One problem is that <servername>.database.windows.net already exceeds the string limit (which seems to be 30 according to this: How to use PHP's dblib PDO driver with long usernames? / SQLSTATE[HY000] Name too long for LOGINREC field (severity 2)).
I have also attempted to exclude the #<servername>.database.windows.net portion in the username entry only to receive the following error:
msgno 40532: "Cannot open server "1433D" requested by the login. The login failed."
According to https://github.com/pymssql/pymssql/issues/330 the #<servername> portion is requested by the server.
So at this point, I attempted to do the following:
user = username + "#{}".format(server)
user = user[:30]
And I received the following information (which was a slight improvement but still not ideal, given that I could still not establish the connection):
"Cannot open server "<server_name minus the last 6 characters>" requested by the login. The login failed."
If possible, I would prefer passing some parameter to pymssql's connect method that would override this character limit or do something to append the server to the username on the backend after calling dbsetlname on just <username> without the #<servername> portion. Does anyone have any recommendations (again the preference is to not ask the client to change our username, but we may have to resort to that or trying to use pyodbc if there's no other option).
These are a few of the connection methods I have tried:
conn = pymssql.connect(
host=host,
database=database,
user=username,
password=password,
port=int(port) # this is 1433
)
conn = pymssql.connect(
host=host,
database=database,
user=username + '#{}'.format(servername),
password=password,
port=int(port) # this is 1433
)
conn = pymssql.connect(
server=<servername>.database.windows.net,
database=database,
user=username,
password=password,
port=int(port) # this is 1433
)
Thanks in advance for any help/advice that you may be able to offer!
You can probably use an alternate connecting string method.
If you're using SQL Server Auth, try this:
conn = pymssql.connect(
server="<servername>.database.windows.net",
port=1433,
user="username",
password="password",
database="dbname"
)
If you're using Windows Auth:
conn = pymssql.connect(
server="<servername>.database.windows.net",
port=1433,
user="DOMAIN\USERNAME",
password="password",
database="dbname"
)
Have you tried either of these connect methods?

Accessing Hive from remote server through Python

I have installed following necessary packages on the remote server to access Hive through Python.
Python 2.7.6,
Python development tools,
pyhs2,
sasl-0.1.3,
thrift-0.9.1,
PyHive-0.1.0
Here is the Python script to access Hive.
#!/usr/bin/env python
import pyhs2 as hive
import getpass
DEFAULT_DB = 'camp'
DEFAULT_SERVER = '10.25.xx.xx'
DEFAULT_PORT = 10000
DEFAULT_DOMAIN = 'xxx.xxxxxx.com'
# Get the username and password
u = raw_input('Enter PAM username: ')
s = getpass.getpass()
# Build the Hive Connection
connection = hive.connect(host=DEFAULT_SERVER, port=DEFAULT_PORT, authMechanism='LDAP', user=u + '#' + DEFAULT_DOMAIN, password=s)
# Hive query statement
statement = "select * from camp.test"
cur = connection.cursor()
# Runs a Hive query and returns the result as a list of list
cur.execute(statement)
df = cur.fetchall()
Here is the output I got:
File "build/bdist.linux-x86_64/egg/pyhs2/__init__.py", line 7, in connect
File "build/bdist.linux-x86_64/egg/pyhs2/connections.py", line 46, in __init__
File "build/bdist.linux-x86_64/egg/pyhs2/cloudera/thrift_sasl.py", line 74, in open
File "build/bdist.linux-x86_64/egg/pyhs2/cloudera/thrift_sasl.py", line 92, in _recv_sasl_message
File "build/bdist.linux-x86_64/egg/thrift/transport/TTransport.py", line 58, in readAll
File "build/bdist.linux-x86_64/egg/thrift/transport/TSocket.py", line 118, in read
thrift.transport.TTransport.TTransportException: TSocket read 0 bytes
I don't see any error in the output after executing the script, however I don't see any query results on the screen. I'm not sure why it's not displaying any query results, Hive server IP, port, user and password are correct. I also verified connectivity between hive server and remote server, no issues with connectivity.
Try using this code:
import pyhs2
with pyhs2.connect(host='localhost',
port=10000,
authMechanism="PLAIN",
user='root',
password='test',
database='default') as conn:
with conn.cursor() as cur:
#Show databases
print cur.getDatabases()
#Execute query
cur.execute("select * from table")
#Return column info from query
print cur.getSchema()
#Fetch table results
for i in cur.fetch():
print i
I've managed to get access by using the following
from pyhive import presto
DEFAULT_DB = 'XXXXX'
DEFAULT_SERVER = 'server.name.blah'
DEFAULT_PORT = 8000
# Username
u = "user"
# Build the Hive Connection
connection = presto.connect(host=DEFAULT_SERVER, port=DEFAULT_PORT, username=u)
# Hive query statement
statement = "select * from public.dudebro limit 5"
cur = connection.cursor()
# Runs a Hive query and returns the result as a list of list
cur.execute(statement)
df = cur.fetchall()
print df

Trouble connecting via paramiko + kerberos

Currently when I want to connect to a node I simply do: ssh username#node and everything works fine. (thanks Kerberos :-))
Now I'm trying to develop a simple python script that connect to a specified host but I cannot connect to it using that script.The following my script:
import paramiko
import gssapi
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname = 'node_name', username = 'my_uname', gss_auth = True, gss_kex = True)
But I received this error:
paramiko.ssh_exception.AuthenticationException: Authentication failed.
I tried also
ssh.connect(hostname = 'node_name', username = 'my_uname', gss_auth = True, gss_kex = True, gss_deleg_creds=True)
and the error I received changed a bit:
gssapi.error.GSSException: (131072) An invalid name was supplied. Minor code: (100001) Success. Target: node_name
Any suggestions?
Thanks!