0

Using Parmiko trying to login to the remote machine and executing the cqlsh commands, but it is not working

I tried using paramiko to execute cqlsh command on remote machine , but not getting any result. I tried with dse.cluster also, but getting AuthenticationFailed with bad credentials. Credentials are correct only, all out lab machines are file-based authenticated.

import paramiko

hostname = '10.XX.XX.XX'
username = 'root'
gSSHkey = 'D:\\LoginKeys\\login-id_rsa-key.ppk'
#cmds = ["cqlsh -u casadmin -pmotive  10.XX.XX.XX 9042 -k casadmin", "select count(*) from dia_scs_config ;"]

sshcon = paramiko.SSHClient()  # will create the object
sshcon.set_missing_host_key_policy(paramiko.AutoAddPolicy())  # no known_hosts error
sshcon.connect(hostname=hostname, username=username, key_filename=gSSHkey)  # no passwd needed
for i in range(len(cmds)):
    stdin, stdout, stderr = sshcon.exec_command(cmds[i])
    output = stdout.readlines()
    print(output)
    for line in output:
        print(line)
sshcon.close()

++++++++++++++++++++++++++++++++++++++++++++===

from dse.cluster import Cluster
from dse.auth import PlainTextAuthProvider
auth_provider = PlainTextAuthProvider(
        username='casadmin', password='casadmin')
cluster = Cluster(contact_points=['10.XX.XX.XX'],
port=9042, auth_provider=auth_provider)
session = cluster.connect('casadmin')
print ("connected")
print (session.execute("select count(*) from dia_scs_config")[0])

trying to connect to Cassandra VM which is having key-based authentication is preset. Any sample working to connect to Cassandra vm which is having key-based authentication will be much helpfull.

saiteja
  • 1
  • 4

2 Answers2

0

Not sure if this will work for you but here's what works for me. I'm only providing the code from client creation to the connection. Also, make sure root is allowed to connect remotely.

sshcon = paramiko.SSHClient()  # will create the object
sshcon.set_missing_host_key_policy(paramiko.AutoAddPolicy())  # no known_hosts error
key = paramiko.RSAKey.from_private_key_file(gSSHkey) 
sshcon.connect(hostname, username=username, pkey=key, look_for_keys=False)
Jason
  • 475
  • 2
  • 4
0

If you get no output on stdout, it is usually because the command fails to even start.

Read stderr (stderr.readlines()) to check for any errors.


Quite often the error is "<command> not found". For that see
Some Unix commands fail with "<command> not found", when executed using Python Paramiko exec_command

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992