PYTHON: Connection check with socket and VM - python

i have a local machine IP: 192.168.1.150 and a VM IP:192.168.1.152.
I'd like to check if the connection exists with my ubuntu server (by the way my ubuntu server is installed on the VM) or not:
My Code:
#!/usr/bin/env python3
import os
import sys
import socket
SERVER_IP = '192.168.152'
SERVER_PORT = 80
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((SERVER_IP, SERVER_PORT))
print('Connected')
s.close()
but sadly it doen't work and i get the ERROR:
Traceback (most recent call last):
File "./checkConnectionToServer.py", line 11, in <module>
s.bind((SERVER_IP, SERVER_PORT))
PermissionError: [Errno 13] Permission denied

Related

"[WinError 10061] No connection could be made because the target machine actively refused it" with python socket to Docker

I am running a Docker container on Windows which is basically made from Ubuntu Image with some add-ons(asterisk pbx). When I try to connect to it(I take IPv4 from ipconfig's "WSL") with python socket:
import socket
a = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
a.connect(('172.19.96.1', 5038))
I get
Traceback (most recent call last):
File "E:\Projects\project\test.py", line 43, in <module>
a.connect(('172.19.96.1', 5038))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
How do I connect to it? I am 100% sure my container is listening to 0.0.0.0:5038 as I can connect to it from inside of the container.
btw I tried to change '172.19.96.1' to '127.0.0.1' and 'localhost' both, but all the same I'm getting that error.

Getting error : ConnectionRefusedError: [Errno 111] Connection refused

I am trying to run this on Ubuntu
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("10.0.2.15", 5555)) #10.0.2.15 is the Kali IP
s.send("Connected!\n")
data_received = s.recv(1024)
print(data_received)
s.close()
While running
‐v ‐l ‐p 5555 this on kali
But i keep getting this error:
Traceback (most recent call last):
File "client.py", line 4, in <module>
s.connect(("10.0.2.15", 4444))
ConnectionRefusedError: [Errno 111] Connection refused

Why am I getting 'getaddrinfo failed' while connecting to remote server?

I am using the socket module in python to connect to a remote server at https://vedant-chatroom.glitch.me/chat.html?user=python. This is my code (test.py):
import socket
web_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
web_socket.connect(("https://vedant-chatroom.glitch.me/chat.html?user=python", 443))
After running I get:
Traceback (most recent call last):
File "test.py", line 4, in <module>
web_socket.connect(("https://vedant-chatroom.glitch.me/chat.html?user=python", 443))
socket.gaierror: [Errno 11001] getaddrinfo failed
The remote server is a Node based server which is using the sockets.io package and express.

python paramiko socket.error: [Errno 113] No route to host

I can't connect to my host with paramiko. It's no problem to connect throgh a shell though.
My code:
import socket
from paramiko import client
ssh=client.SSHClient()
ssh.load_system_host_keys()
host=socket.gethostbyname("rumo.fritz.box") # works -> host exists
print host
ssh.connect(host,2012,"sshad","MyPassword",timeout=10)
stdin,stdout,stderr=ssh.exec_command("ls -la")
ssh.close()
The error+output:
192.168.178.37
Traceback (most recent call last):
File "./rumo_suspend.py", line 20, in <module>
ssh.connect(host,2012,"sshad","MyPassword",timeout=10)
File "/usr/lib/python2.7/dist-packages/paramiko/client.py", line 296, in connect
sock.connect(addr)
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 113] No route to host
Connecting through a shell:
ssh -p 2012 sshad#rumo.fritz.box
sshad#rumo.fritz.box's password:
Welcome to Linux Mint 17.2 Rafaela (GNU/Linux 3.16.0-031600-generic x86_64)

Transfer a local file to a remote server(windows) using python

Hi I have this sample path "\10.81.67.162" which is a remote server (windows OS)
I want to be able to transfer files (local) to the remote server using paramiko in python.
I can make it work if the server is in linux.
This is my sample code
import paramiko
import base64
username = 'username'
password = 'password'
host = "10.81.67.162"
port = 22
transport = paramiko.Transport((host,port))
transport.connect(username = username, password = password)
stfp = paramiko.SFTPClient.from_transport(transport)
But having thhis error in windows:
Traceback (most recent call last):
File "ssh.py", line 9, in <module>
transport = paramiko.Transport((host,port))
File "build\bdist.win32\egg\paramiko\transport.py", line 289, in __init__
File "C:\Python27\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 10061] No connection could be made because the target machi
ne actively refused it
Python version 2.7
Paramiko version 1.7.5
Thanks!
There is no SSH server running on Windows. Thats why you are not able to connect.
Windows doesn't support SSH natively. You need to install third-party SSH server on your remote windows hosts like cygwin, bitwise ssh or freeSSHD.
Or if you are interested to run only commands on windows host then you can use winrm protocol which is natively supported by Windows. For this you need a python library pywinrm. I have used this and works fine:
https://github.com/diyan/pywinrm
If you're trying to connect to a network drive, you can use win_unc:
import os
from win_unc import UncCredentials, UncDirectory, UncDirectoryConnection
creds = UncCredentials('USERNAME', 'PASSWORD')
unc = UncDirectory(r'\\<computername>\c$', creds)
conn = UncDirectoryConnection(unc)
conn.connect()
print list(os.listdir(r'\\<computer_name>\c$\<folder>'))

Categories