NoValidConnectionError on Paramiko in Python - python

ssh_client =paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.load_system_host_keys()
ssh_client.set_missing_host_key_policy(paramiko.WarningPolicy)
ssh_client.connect(hostname='abc',username='admin',password='admin')
with SCPClient(ssh_client.get_transport()) as scp:
scp.put(local_path='example.txt',remote_path='/abc')
I am trying to connect to a Windows system and send a file example.txt to it. But if I run the script, it gives the following error when sending a file to a windows system
paramiko.ssh_exception.NoValidConnectionsError: [Errno None] Unable to connect to port 22 on 'IP Address'
Help would be appreciated.
Edit: The Windows system that I am sending the file to runs a Windows 2008 server.

If you want to connect with Paramiko SSH/SFTP/SCP library to a machine, the machine must be running SSH/SFTP/SCP server.
Windows does not come with SSH/SFTP/SCP server running by default.
See Windows SSH Servers? question on Super User.

Related

How to establish remote desktop connection between windows machine using Paramiko, Python

I am trying establish remote desktop connection from a windows machine to other windows machine and have tried below scenarios -
import paramiko
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('IP',port = 3389, username='un',password='pwd')
print ("Connected to %s" % 'IP')
stdin, stdout, stderr = ssh.exec_command('ls -1 /root|head -n 5')
print ("STDOUT:\n%s\n\nSTDERR:\n%s\n" %( stdout.read(), stderr.read() ))
Upon running above code i observed below error message
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner[WinError 10054] An existing connection was forcibly closed by the remote host
Looked at this quetsion - python connecting to ssh An existing connection was forcibly closed by the remote host
But, I haven't closed the connection in my code and still face the exception.
I looked at some forums and understood that for windows machine 3389 is the port for remote desktop connection and so used 3389 as port in my code.
I have also used port=22, ran the code and observed this exception:
paramiko.ssh_exception.NoValidConnectionsError: [Errno None] Unable to connect to port 22 on IP
I have also tried few - How to connect to a remote Windows machine to execute commands?
procedures stated here to make remote desktop connection. But couldn't establish the connection.
versions:
paramiko: 2.4.0
python: 3.6.4
You need SSH server on the server to connect with SSH.
There's no SSH server by default in Windows, so you cannot connect to port 22, unless you install some. See https://serverfault.com/q/648855/168875
You absolutely cannot connect to Remote desktop port 2289 with SSH.
Also, even if you install SSH server, it won't allow you to execute *nix command like ls on Windows. Neither it would magically create some root folder on Windows.
For that you additionally need to install some *nix-emulation on Windows, like Windows Subsystem for Linux or Cygwin.
Overall, your question like a conceptual misunderstanding.

Copy File from Windows Host to Linux [Python]

What are the different modules/ways to copy file from a windows computer to a linux server available in python
I tried using ftplib api to connect to the windows server but i m unable to do with the error - socket.error: [Errno 111] Connection refused
What are the other modules that i can connect to a windows computer to copy or list the files under a directory
If you have access to linux server, and the file generated on windows automatically, you can do the folowing:
Generate ssh-key on your windows maching
Add it to authorized_hosts of the linux machine
Install simple console scp tool on windows
Write simple cmd-script to copy file with help of scp, something like:
scp c:\path\to\file.txt user#linuxhost.local:/home/user/file.txt
Run this script automatically every time, then the file is generated on windows host.

Error when connecting to SFTP server using Python Pysftp

I am executing a Python script on my Linux server that uses pysftp to connect to another server in order to read files that are sitting in a directory of that remote server. When I run the script, it fails out while connecting to the remote server and creates a text file with the title: 'This service allows sftp connections only.'
This file is created inside my project directory. Below is the part of my code that is failing:
def sftp_get_file(sftp_host, sftp_username):
with pysftp.Connection(sftp_host, sftp_username) as sftp:
# transfer file from remote to local
sftp.get(remote_file, local_file)
Code is very simple and works when I've tested it using my local server as the remote server. When I tested it in the new environment by actually depending on SFTP, then it failed. Any suggestions? Is pysftp using SSH at some point when it should be using only SFTP?
Turns out the problem was due to me performing sftp.execute('ls') a couple lines down in the script. The server I was remoting onto only supported sftp commands and that command was forbidden.

Can't connect to remote django server port 80

I'm having an issue running and connecting to my python django server on a windows 2012 server. To run the server I use command: python manage.py 0.0.0.0:80. This results in an error below
[Error 10013]: an attempt was made to access a socket in a way forbidden by its access permissions
I've tried running the command prompt as an administrator with no change. For reference, I am able to run the server on port 8000 but then I cannot connect to the port remotely. I have turned off firewalls as well so that is probably not the issue.
While it is preferable to run the django on port 80, I am trying to get this working on any port.
Port 80 (and many other ports) is reserved by Windows Server. Checkout this https://serverfault.com/questions/633179/cant-make-confluence-run-on-port-80-with-windows-server-2012-r2,
You may want to google "Windows Server Reserved port" for more info and a way to "unreserve" it.

Connecting to Python XML RPC from the Mac

I wrote an XML RPC server in python and a simple Test Client for it in python. The Server runs on a linux box. I tested it by running the python client on the same linux machine and it works.
I then tried to run the python client on a Mac and i get the following error
socket.error: (61, 'Connection Refused')
I can ping and ssh into the linux machine from the Mac. So i dont think its a configuration or firewall error.
Does anyone have any idea what could be going wrong?
The code for the client is as below:
import xmlrpclib
s = xmlrpclib.ServerProxy('http://143.252.249.141:8000')
print s.GetUsers()
print s.system.listMethods()
"Connection Refused" means the connection was REFUSED - the machine 143.252.249.141 is up, and in the network, but is not accepting connections on port 8000 - it is actively refusing them.
So maybe the server software isn't running on the server? Or is running in another port? Or is bound to a different IP address?

Categories

Resources