Unable to Connect to SFTP through paramiko
I am trying to establish a connection with an SFTP with paramiko. I was able to generate the known_hosts file in my local system by using
ssh my.domain.com
The resultant file has both the host and its IP in the first line of known_hosts, like
my.domain.com,xx.xx.xxx.xx ...
When I try to connect through paramiko,
host, port = 'my.domain.com,xx.xx.xxx.xx', 22
user, pwd = "xyz", "abc"
ssh = paramiko.SSHClient()
ssh.connect(host, port, username=user, password=pwd)
I get the error
socket.gaierror: [Errno 11001] getaddrinfo failed
After looking this up, the solutions were to not mention user in host or add port, etc. But I'm still not able to connect. I tried removing my.domain.com
from both the Python code and known_hosts file,
host, port = 'xx.xx.xxx.xx', 22
user, pwd = "xyz", "abc"
ssh = paramiko.SSHClient()
ssh.connect(host, port, username=user, password=pwd)
but that didn't work. I tried removing xx.xx.xxx.xx
from both the Python code and known_hosts file,
host, port = 'xx.xx.xxx.xx', 22
user, pwd = "xyz", "abc"
ssh = paramiko.SSHClient()
ssh.connect(host, port, username=user, password=pwd)
but that didn't work either.
How do I connect to my SFTP?
1 answer
-
answered 2022-05-07 08:59
python_amateur
as resolved by @tdalaney in the comments, I edited the known_hosts file to only specify the domain name and added ssh.load_system_host_keys() before the connect method.
do you know?
how many words do you know
See also questions close to this topic
-
Python File Tagging System does not retrieve nested dictionaries in dictionary
I am building a file tagging system using Python. The idea is simple. Given a directory of files (and files within subdirectories), I want to filter them out using a filter input and tag those files with a word or a phrase.
If I got the following contents in my current directory:
data/ budget.xls world_building_budget.txt a.txt b.exe hello_world.dat world_builder.spec
and I execute the following command in the shell:
py -3 tag_tool.py -filter=world -tag="World-Building Tool"
My output will be:
These files were tagged with "World-Building Tool": data/ world_building_budget.txt hello_world.dat world_builder.spec
My current output isn't exactly like this but basically, I am converting all files and files within subdirectories into a single dictionary like this:
def fs_tree_to_dict(path_): file_token = '' for root, dirs, files in os.walk(path_): tree = {d: fs_tree_to_dict(os.path.join(root, d)) for d in dirs} tree.update({f: file_token for f in files}) return tree
Right now, my dictionary looks like this:
key:''
.In the following function, I am turning the empty values
''
into empty lists (to hold my tags):def empty_str_to_list(d): for k,v in d.items(): if v == '': d[k] = [] elif isinstance(v, dict): empty_str_to_list(v)
When I run my entire code, this is my output:
hello_world.dat ['World-Building Tool'] world_builder.spec ['World-Building Tool']
But it does not see
data/world_building_budget.txt
. This is the full dictionary:{'data': {'world_building_budget.txt': []}, 'a.txt': [], 'hello_world.dat': [], 'b.exe': [], 'world_builder.spec': []}
This is my full code:
import os, argparse def fs_tree_to_dict(path_): file_token = '' for root, dirs, files in os.walk(path_): tree = {d: fs_tree_to_dict(os.path.join(root, d)) for d in dirs} tree.update({f: file_token for f in files}) return tree def empty_str_to_list(d): for k, v in d.items(): if v == '': d[k] = [] elif isinstance(v, dict): empty_str_to_list(v) parser = argparse.ArgumentParser(description="Just an example", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("--filter", action="store", help="keyword to filter files") parser.add_argument("--tag", action="store", help="a tag phrase to attach to a file") parser.add_argument("--get_tagged", action="store", help="retrieve files matching an existing tag") args = parser.parse_args() filter = args.filter tag = args.tag get_tagged = args.get_tagged current_dir = os.getcwd() files_dict = fs_tree_to_dict(current_dir) empty_str_to_list(files_dict) for k, v in files_dict.items(): if filter in k: if v == []: v.append(tag) print(k, v) elif isinstance(v, dict): empty_str_to_list(v) if get_tagged in v: print(k, v)
-
Actaully i am working on a project and in it, it is showing no module name pip_internal plz help me for the same. I am using pycharm(conda interpreter
File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\Scripts\pip.exe\__main__.py", line 4, in <module> File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\__init__.py", line 4, in <module> from pip_internal.utils import _log
I am using pycharm with conda interpreter.
-
Looping the function if the input is not string
I'm new to python (first of all) I have a homework to do a function about checking if an item exists in a dictionary or not.
inventory = {"apple" : 50, "orange" : 50, "pineapple" : 70, "strawberry" : 30} def check_item(): x = input("Enter the fruit's name: ") if not x.isalpha(): print("Error! You need to type the name of the fruit") elif x in inventory: print("Fruit found:", x) print("Inventory available:", inventory[x],"KG") else: print("Fruit not found") check_item()
I want the function to loop again only if the input written is not string. I've tried to type return Under print("Error! You need to type the name of the fruit") but didn't work. Help
-
Why doesn't systemd start my autossh tunnel on reboot?
I have a PC that I need to ssh into which only has a private IP (running Ubuntu 20.04 LTS). This is my first time working with autossh and systemd. I have autossh working and I can easily create a tunnel and ssh into the PC from my server (which has a public ip).
I have noticed that the ssh tunnel will randomly close despite having ServerAliveInterval 30 and ServerAliveCountMax 3 values. I have been fixing this my manually deleting the tunnel on both the PC and server, and then creating it all over again. But this is a temporary solution since ideally I would want the tunnel to come back by itself. I believe the tunnel closes to either the network dropping and coming back up, but I am not sure why. Here is the systemd service I created on the PC:
tunnel-up.service (192.168.1.111 is the fake public IP of the server)
[Unit] Wants=network-online.target After=network-online.target [Service] Type=oneshot ExecStart=autossh -M 0 -o "ExitOnForwardFailure=yes" -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -NT -o Tunnel=point-to-point -w 1:1 192.168.1.111 & ExecStart=/bin/bash /root/scripts/link-up.sh [Install] WantedBy=multi-user.target
link-up.sh
#!/bin/bash ip link set tun1 up && ip addr add 10.250.0.3/30 peer 10.250.0.4 dev tun1
I have done systemctl daemon-reload and systemctl start tunnel-up.service but when I reboot my computer the tunnel never gets created... I had the autossh command inside my link-up.sh script and when I executed the script it worked perfectly, however when it comes to running this on startup it never works. Any help would be appreciated.
Here is the output of journalctl -u tunnel-up.service
May 06 17:42:18 hmtest.ut systemd[1]: Starting tunnel-up.service... May 06 17:42:18 hmtest.ut autossh[1067]: port set to 0, monitoring disabled May 06 17:42:18 hmtest.ut autossh[1067]: starting ssh (count 1) May 06 17:42:18 hmtest.ut autossh[1067]: ssh child pid is 1077 May 06 17:42:18 hmtest.ut autossh[1077]: ssh: connect to host 192.168.1.111 port 22: Network is unreachable May 06 17:42:18 hmtest.ut autossh[1067]: ssh exited prematurely with status 255; autossh exiting May 06 17:42:18 hmtest.ut systemd[1]: tunnel-up.service: Main process exited, code=exited, status=1/FAILURE May 06 17:42:18 hmtest.ut systemd[1]: tunnel-up.service: Failed with result 'exit-code'. May 06 17:42:18 hmtest.ut systemd[1]: Failed to start tunnel-up.service.
-
Running a Python Flask server in closed Terminal session
I've made an Flask API Server that i want to run on a Server and close the Terminal session and still keep it running. (SSH) The API's makes a lot of Requests to other servers and uses Threading to make this Process faster.
I've tried the setsid command, and this works fine until i close the Terminal session. Because when its closed, i am only getting 500 errors.
-
Mathematics Mod Function
I would love if someone can try to explain the difference between the two equations below and why they equal different values but from what i can see are the same.
I am using MATLAB to try to compute the first equation shown, however, I am getting the result of the second.
d = mod(23^-1, 4169) RESULT: 0.0435 SHOULD BE: 3444
Any ideas? :)
-
How to securely implement a deadpool in a PoW blockchain?
Imagine you have a blockchain where the Proof of Work scheme is integer factorization. There is an opcode that takes two integers N,M where it returns true if M∉{0,1,N} and N mod M ≡ 0. Now, suppose we want a number factored, say Mg. It could be a Cunningham number, a brilliant number or any other number where there is an interest in knowing its factorization.
We can create a transaction where the locking script for the funds of the transaction is this opcode with Mg as the first input. This way the unlocking script is just any non-trivial factor of Mg. This would allow anyone on the blockchain to claim the reward for this factorization.
Two issues: miner-in-the-middle attack and reorganization attack.
Miner-in-the-Middle Attack: The miner who receives a transaction with the unlocking script will be able to see the factor in plain sight and replace that transaction with one where the funds are sent to their wallets instead of the solver's.
Reorganization Attack: If the value of the blockchain's coin is high there is also an incentive to attack the block in which a solver transaction was mined by re-solving that block with a replaced transaction accrediting themselves(the attacker) and attempting to create a longer chain from this new block.
So,the question becomes, how can solutions be submitted safely in this blockchain? Is it possible to do it in two transactions, or are three transactions required? Is there any blockchain that has solved this problem already?
Thank you.
Note 1: I choose tags that are the most related to this topic. It also seems like the bitcoin stack exchange is strictly for bitcoin.
Note 2: This question was posted on the cryptography stack exchange to get ideas on cryptographic mechanisms that might help implement this. It is also posted here because there is a blockchain tag and there may be implementation details and solutions which may be achieved with the current opcode set plus this new opcode. It seems this community stack exchange is the most suited for the latter.
-
Automate Azure Devops (FTP Upload) and Git to upload on Remote Server
The current setup is as below
- Version Control - Git
- Repos and Branch hosted on - Azure DevOps
- Codebase - External server
The dev team clones Azure Repo into local git project and any staged changes are committed via Git and pushed to specific branch of Azure DevOps. In this setup we would want to upload the changes to external FTP servers and avoid manual upload. Currently trying to use Azure Devops FTP Upload Task (https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/ftp-upload?view=azure-devops), however facing issues; yaml script as below
trigger: - main pool: vmImage: 'ubuntu-latest' variables: phpVersion: 7.4 webAppName: 'Test Project' buildConfiguration: 'Release' vmImageName: 'ubuntu-latest' steps: - publish: $(System.DefaultWorkingDirectory)/AzureRepoName artifact: Test Project Deploy - task: FtpUpload@2 displayName: 'FTP Upload' inputs: credentialsOption: inputs serverUrl: 'ftps://00.00.00.00:22' username: ftp-username password: ftp-password rootDirectory: '$(System.DefaultWorkingDirectory)/AzureRepoName' remoteDirectory: '/home/public_html' clean: false cleanContents: false preservePaths: true trustSSL: true
PROBLEM
Following errors occur when I commit (for test purposes) something.
Starting: PublishPipelineArtifact ============================================================================== Task : Publish Pipeline Artifacts Description : Publish (upload) a file or directory as a named artifact for the current run Version : 1.199.0 Author : Microsoft Corporation Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/publish-pipeline-artifact ============================================================================== Artifact name input: Test Project Deploy ##[error]Path does not exist: /home/vsts/work/1/s/AzureRepoName Finishing: PublishPipelineArtifact
I want to upload any staged change that is committed to main branch on Azure Devops to be automatically deploy on the remote FTP server
Thanks
-
Upstream relative path and sFTP upload with apache.commons.vsf2
I try to upload files on remote server with sFTP protocol, I use apache.commons.vsf2 library, and it's works well.
Here is my code :
public void uploadFile(byte[] bytesArray, String target, String fileName) throws IOException { this.initProperties(); // initializes username, password and host InputStream is = new ByteArrayInputStream(bytesArray); FileSystemManager manager = VFS.getManager(); FileSystemOptions opts = new FileSystemOptions(); FileObject remote = manager.resolveFile("sftp://" + username + ":" + password + "@" + host + "/" + fileName); try (OutputStream ostream = remote.getContent().getOutputStream()) { IOUtils.copy(is, ostream); logger.info("File {} uploaded", fileName); } remote.close(); }
But I want to change the default folder, and upload my files to an upstream folder, and I can't do it.
I tried with a relative path ("../"), but it seems that I don't have access to the parent directory, while I have access with mRemoteNg for example, with the same authentication (username / password).
For example, when I tried :
remote.getParent().getParent() // return null
An idea please ? Thanks
-
How to execute a Shell Script that is present on remote Unix server from local windows with paramiko in python script
I am trying to invoke a shell script that is present on remote server from my local windows but not able to do so. The script requires certain parameter as well to get executed. It is not being able to run ls command as well as shown.
My Code:
import paramiko import time client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect('apsrt3997', username='Lsaxena2', password='some pass') channel = client.invoke_shell() command = '(/p4products/nemis2/filehandlerU/bin/startFileHandler.sh encpr2 /p4products/nemis2/filehandlerU/encpr2/incoming/ AAP_ENC_UX_B.mi_no_mif_ph_ge &)' channel.send(command + '\n') time.sleep(1) print (channel.recv(1024)) client.close()
Can someone suggest how to execute the shell script from local windows? My actual command which I used to execute the shell script from putty is:
(/p4products/nemis2/filehandlerU/bin/startFileHandler.sh encpr2 /p4products/nemis2/filehandlerU/encpr2/incoming/ AAP_ENC_UX_B.mi_no_mif_ph_ge &)
My output is kinda scary but it is like:
b'Last login: Thu May 5 02:35:51 2022 from 10.190.51.178\r\r\n\r\n***************************************************** ***************\r\n* *\r\n* This is a private computer system containing confidential information. *\r\n* Any unauthorized attempt to access or use this computer system or any *\r\n* information on it by employees or other persons may result in termination *\r\n* of employment, civil fines, and criminal penalties. This system must be *\r\n* used for authorized business purposes only. *** THIS SYSTEM IS ONLY RUNNING QAS ***\r\n\r\n\r\nBuilt by an HPSA template on 2017-04- 25\r\n\r\nlsaxena2@apsrt3997.uhc.com:/home/lsaxena2\r\n$ (/p4products/nemis2/filehandlerU/bin/startFileHandler.sh encpr2 /p4products/nemis2/filehandlerU/e'
-
Error while using pysftp going into deadlock and raising EOFError()
I am developing a solution to download files from sftp but it is going into a deadlock and after a while throws errors
The situation is that randomly some of the files in the sftp are processing fine but random as well the process fails and get stuck for like a minute before it raise the exceptions
The sftp is the pysftp module.
def get(self, filename, destination): sftp = self.connection with sftp.cd(self.base_path): sftp.get(filename, destination) logger.info(f"File {filename} downloaded successfully.") return destination
And is throwing some errors after quite some time
Traceback (most recent call last): File "/opt/.local/lib/python3.8/site-packages/paramiko/sftp_client.py", line 843, in _read_response t, data = self._read_packet() File "/opt/.local/lib/python3.8/site-packages/paramiko/sftp.py", line 201, in _read_packet x = self._read_all(4) File "/opt/.local/lib/python3.8/site-packages/paramiko/sftp.py", line 188, in _read_all raise EOFError() EOFError During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/opt/.local/lib/python3.8/site-packages/pysftp/__init__.py", line 509, in cd yield File "/opt/project/downloader.py", line 77, in get with sftp.cd(self.base_path): File "/opt/.local/lib/python3.8/site-packages/pysftp/__init__.py", line 249, in get self._sftp.get(remotepath, localpath, callback=callback) File "/opt/.local/lib/python3.8/site-packages/paramiko/sftp_client.py", line 802, in get size = self.getfo(remotepath, fl, callback) File "/opt/.local/lib/python3.8/site-packages/paramiko/sftp_client.py", line 781, in getfo return self._transfer_with_callback( File "/opt/.local/lib/python3.8/site-packages/paramiko/sftp_client.py", line 678, in _transfer_with_callback data = reader.read(32768) File "/opt/.local/lib/python3.8/site-packages/paramiko/file.py", line 219, in read new_data = self._read(read_size) File "/opt/.local/lib/python3.8/site-packages/paramiko/sftp_file.py", line 182, in _read data = self._read_prefetch(size) File "/opt/.local/lib/python3.8/site-packages/paramiko/sftp_file.py", line 162, in _read_prefetch self.sftp._read_response() File "/opt/.local/lib/python3.8/site-packages/paramiko/sftp_client.py", line 845, in _read_response raise SSHException("Server connection dropped: {}".format(e)) paramiko.ssh_exception.SSHException: Server connection dropped: During handling of the above exception, another exception occurred: Traceback (most recent call last): File "manage.py", line 32, in <module> module.run() File "/opt/project/data_collector.py", line 118, in run integration.run() File "/opt/project/integrator.py", line 438, in run self.start_downloading() File "/usr/local/lib/python3.8/contextlib.py", line 75, in inner return func(*args, **kwds) File "/opt/project/integrator.py", line 302, in start_downloading self._download_multiple_branch_files() File "/opt/project/integrator.py", line 197, in _download_multiple_branch_files downloaded_file = downloader.get(file, destination) File "/opt/project/downloader.py", line 77, in get with sftp.cd(self.base_path): File "/usr/local/lib/python3.8/contextlib.py", line 131, in __exit__ self.gen.throw(type, value, traceback) File "/opt/.local/lib/python3.8/site-packages/pysftp/__init__.py", line 511, in cd self.cwd(original_path) File "/opt/.local/lib/python3.8/site-packages/pysftp/__init__.py", line 524, in chdir self._sftp.chdir(remotepath) File "/opt/.local/lib/python3.8/site-packages/paramiko/sftp_client.py", line 659, in chdir if not stat.S_ISDIR(self.stat(path).st_mode): File "/opt/.local/lib/python3.8/site-packages/paramiko/sftp_client.py", line 493, in stat t, msg = self._request(CMD_STAT, path) File "/opt/.local/lib/python3.8/site-packages/paramiko/sftp_client.py", line 812, in _request num = self._async_request(type(None), t, *arg) File "/opt/.local/lib/python3.8/site-packages/paramiko/sftp_client.py", line 837, in _async_request self._send_packet(t, msg) File "/opt/.local/lib/python3.8/site-packages/paramiko/sftp.py", line 198, in _send_packet self._write_all(out) File "/opt/.local/lib/python3.8/site-packages/paramiko/sftp.py", line 162, in _write_all n = self.sock.send(out) File "/opt/.local/lib/python3.8/site-packages/paramiko/channel.py", line 801, in send return self._send(s, m) File "/opt/.local/lib/python3.8/site-packages/paramiko/channel.py", line 1198, in _send raise socket.error("Socket is closed") OSError: Socket is closed