Chambers
-- -- --

Struggling to connect to server with Python

Anonymous in /c/coding_help

221
I have a Red Hat Linux Server and I am trying to connect to it with Python but I kept getting "ConnectionRefusedError: [Errno 61] Connection refused". I have tried to connect to it with simply connecting to the ip address and port number but I am getting the same thing. I have even tried connecting to it with a socket but still I get the same thing. I am currently using the paramiko library to connect to it but still I am getting the same thing. I am using a rsa key to connect to it but I am still getting the same error. I am simply trying to connect to the server because I want to run a batch file on it simply by sending a command from my python code. I have used the pxssh library but it is giving me "PasswordAuthentication is false for this host" which I don't understand because all my parameters are set to true. Any help would be appreciated. This is my code using paramiko to connect to it:<br><br>```python<br>import paramiko<br>import socket<br><br>def connect_to_server(ip_address, port_number, rsa_key, rsa_password, username):<br> try:<br> ssh_client = paramiko.SSHClient()<br> ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())<br> ssh_client.connect(ip_address,<br> port_number,<br> username=username,<br> key_filename=rsa_key,<br> passphrase=rsa_password,<br> look_for_keys=True)<br> print("Connected to the server")<br> except paramiko.AuthenticationException as e:<br> print(f"Authentication failed: {e}")<br> return False<br> except paramiko.SSHException as e:<br> print(f"Connection failed: {e}")<br> return False<br> except paramiko.BadAuthenticationType as e:<br> print(f"Bad authentication type: {e}")<br> return False<br> except Exception as e:<br> print(f"Any other exception: {e}")<br> return False<br> return ssh_client<br><br>ip_address = "192.168.2.29"<br>port_number = 8080<br>rsa_key = "/Users/yourusername/.ssh/your_private_key"<br>rsa_password = "your_key_password"<br>username = "your_username"<br><br>ssh_client = connect_to_server(ip_address, port_number, rsa_key, rsa_password, username)<br>if ssh_client:<br> stdin, stdout, stderr = ssh_client.exec_command('ls -l', get_pty=True)<br> for line in stdout:<br> print('... ' + line.strip())<br> ssh_client.close()<br>```<br><br>This is my code using the pxssh library:<br><br>```python<br>import pxssh<br><br>def connect_to_server(ip_address, username, password):<br> try:<br> ssh_client = pxssh.pxssh()<br> ssh_client.login(ip_address, username, password, auto_prompt=True)<br> return ssh_client<br> except pxssh.ExceptionPxssh as e:<br> print(e)<br> return False<br><br>ip_address = "192.168.2.29"<br>username = "your_username"<br>password = "your_password"<br><br>ssh_client = connect_to_server(ip_address, username, password)<br>if ssh_client:<br> ssh_client.sendline('ls -l')<br> ssh_client.prompt()<br> print(ssh_client.before)<br> ssh_client.logout()<br>```<br><br>This is my code using socket:<br><br>```python<br>import socket<br><br>def connect_to_server(ip_address, port_number):<br> try:<br> ssh_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br> ssh_client.connect((ip_address, port_number))<br> return ssh_client<br> except socket.error as e:<br> print(f"Connection failed: {e}")<br> return False<br><br>ip_address = "192.168.2.29"<br>port_number = 8080<br><br>ssh_client = connect_to_server(ip_address, port_number)<br>if ssh_client:<br> ssh_client.sendall(b'ls -l')<br> data = ssh_client.recv(1024)<br> print(data.decode())<br> ssh_client.close()<br>```<br><br>This is my code using socket with rsa key:<br><br>```python<br>import socket<br>import paramiko<br><br>def connect_to_server(ip_address, port_number, rsa_key, rsa_password, username):<br> try:<br> ssh_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br> ssh_client.connect((ip_address, port_number))<br> except socket.error as e:<br> print(f"Connection failed: {e}")<br> return False<br> try:<br> ssh_client = paramiko.SSHClient()<br> ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())<br> ssh_client.connect(ip_address,<br> port_number,<br> username=username,<br> key_filename=rsa_key,<br> passphrase=rsa_password,<br> look_for_keys=True)<br> print("Connected to the server")<br> except paramiko.AuthenticationException as e:<br> print(f"Authentication failed: {e}")<br> return False<br> except paramiko.SSHException as e:<br> print(f"Connection failed: {e}")<br> return False<br> except paramiko.BadAuthenticationType as e:<br> print(f"Bad authentication type: {e}")<br> return False<br> except Exception as e:<br> print(f"Any other exception: {e}")<br> return False<br> return ssh_client<br><br>ip_address = "192.168.2.29"<br>port_number = 8080<br>rsa_key = "/Users/yourusername/.ssh/your_private_key"<br>rsa_password = "your_key_password"<br>username = "your_username"<br><br>ssh_client = connect_to_server(ip_address, port_number, rsa_key, rsa_password, username)<br>if ssh_client:<br> stdin, stdout, stderr = ssh_client.exec_command('ls -l', get_pty=True)<br> for line in stdout:<br> print('... ' + line.strip())<br> ssh_client.close()<br>```<br><br>This is my code using socket with rsa key and pxssh:<br><br>```python<br>import socket<br>import pxssh<br>import paramiko<br><br>def connect_to_server(ip_address, port_number, rsa_key, rsa_password, username):<br> try:<br> ssh_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br> ssh_client.connect((ip_address, port_number))<br> ssh_client = paramiko.SSHClient()<br> ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())<br> except socket.error as e:<br> print(f"Connection failed: {e}")<br> return False<br> try:<br> ssh_client.connect(ip_address,<br> port_number,<br> username=username,<br> key_filename=rsa_key,<br> passphrase=rsa_password,<br> look_for_keys=True)<br> print("Connected to the server")<br> except paramiko.AuthenticationException as e:<br> print(f"Authentication failed: {e}")<br> return False<br> except paramiko.SSHException as e:<br> print(f"Connection failed: {e}")<br> return False<br> except paramiko.BadAuthenticationType as e:<br> print(f"Bad authentication type: {e}")<br> return False<br> except Exception as e:<br> print(f"Any other exception: {e}")<br> return False<br> ssh_client = pxssh.pxssh()<br> ssh_client.login(ip_address, username, rsa_password, auto_prompt=True)<br> return ssh_client<br><br>ip_address = "192.168.2.29"<br>port_number = 8080<br>rsa_key = "/Users/yourusername/.ssh/your_private_key"<br>rsa_password = "your_key_password"<br>username = "your_username"<br><br>ssh_client = connect_to_server(ip_address, port_number, rsa_key, rsa_password, username)<br>if ssh_client:<br> ssh_client.sendline('ls -l')<br> ssh_client.prompt()<br> print(ssh_client.before)<br> ssh_client.logout()<br>```

Comments (8) 9156 👁️