Error when connecting to SFTP server using Python Pysftp - python

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.

Related

Cannot copy/move file from remote SFTP server to local machine by Paramiko code running on remote SSH server

I want to copy a file from my SFTP server to local computer. However, when I run my code, it didn't show any error while I still cannot find my file on local computer. My code like that:
import paramiko
host_name ='10.110.100.8'
user_name = 'abc'
password ='xyz'
port = 22
remote_dir_name ='/data/.../PMC1087887_00003.jpg'
local_dir_name = 'D:\..\pred.jpg'
t = paramiko.Transport((host_name, port))
t.connect(username=user_name, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.get(remote_dir_name,local_dir_name)
I have found the main problem. If I run my code in local in VS Code, it works. But when I login in my server by SSH in VS Code, and run my code on server, I found that my file appeared in current code folder (for example /home/.../D:\..\pred.jpg) and its name is D:\..\pred.jpg. How to solve this problem if I want to run code on server and download file to local?
If you call SFTPClient.get on the server, it will, as any other file manipulation API, work with files on the server.
There's no way to make remote Python script directly work with files on your local machine.
You would have to use some API to push the files to your local machine. But for that, your local machine would have to implement the API. For example, you can run an SFTP server on the local machine and "upload" the files to it.

How to create directory in remote system using python script

I want to create a directory in my remote system using python script or by socket programming. I have remote system's Username, password and IP address. I am able to do this in my local machine but not in remote. Please help!
Download Putty then connect to remote system) and in terminal write mkdir foldername
To create a directory on a remote machine, you will have to first connect to it.Telnet and SSH and SSH is used to connect to remote machines. Obviously TELNET or SSH service should be running on the remote machine, otherwise you won't be able to connect.Since in case of Telnet,data is transfered in plain text, it's better to use SSH protocol.
Once connected to the remote machine using SSH, you will be able to execute commands on the remote machine.
Now since you want to do everything in Python, you will have to write a complete SSH client in Python. Which is greate for learning, because you will learn about socket programming and cryptography.
If you are in a hurry, you can use a good SSH library.
If you are getting network connection error, please check whether SSH is installed in the remote machine or not. If yes, then check firewall settings.

Script executed on remote server using Python Paramiko cannot read/access local files

I'm trying to run a local Python script from my laptop (which works fine) on remote server (VPS).
The script can't read local files from my laptop from VPS
Output:
My script on pycharm.
import sys
import time
import paramiko
# Connect to remote host
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('myip', port=22, username='root', password='mypassword')
# Setup sftp connection and transmit this script
sftp = client.open_sftp()
sftp.put(r'/myscript.py', '/myscript.py')
sftp.close()
# till now everything's good. I check my VPS files i find my script uploaded there
# Run the transmitted script remotely without args and show its output.
# SSHClient.exec_command() returns the tuple (stdin,stdout,stderr)
stdout = client.exec_command('python3 /myscript.py')[1]
for line in stdout:
# Process each line in the remote output
print(line)
client.close()
sys.exit(0)
When I run script from VPS I got this issue
I can't run the script directly from VPS to check the issue because I use local files, check the screenshot:
When I remove local paths and run the script (both from pycharm and VPS), it works fine.
You cannot magically access local files from script run on a server.
I can't run the script directly from VPS to check the issue because i use local files, check img.
There's no difference between running the script in remote shell using your favourite SSH terminal client (I assume that's what you mean by "run the script directly from VPS") and running the script in remote shell using Paramiko. It still runs in the remote shell.
There's no easy way to make the client files accessible from the server. That would be a security nightmare.
Either your script has to upload the files to the server.
Or you need to run a (SFTP/FTP/whatever) server on your local machine to make your local file accessible to the world.
For an example how to run an SFTP server, see my guide:
Installing SFTP/SSH server on Windows using OpenSSH

JumpSSh in Python get_cmd_output

I am trying to connect to a remote server from a jump server. It connected to the remote server perfectly, but when I try to run a python script on the remote server, it says no directory found. Please help
gateway_session = SSHSession('host',
'unman', password='password').open()
# from jump server, establish connection with a remote server
remote_session = gateway_session.get_remote_session('host',
'username',password='password')
print(gateway_session.get_cmd_output('python /Folder/test.py'))
Try first to check where you are:
print(gateway_session.get_cmd_output('pwd;ls -alrth; ls -alrth /'))
That way, you know if there is indeed a Folder at /
The OP adds in the comments:
all I needed was to was separate the commands with a semi colon and run them using the same get output command.
The OP adds:
I just sshpass to a jump server from their, I sshpass to the remote on the same command in a shell script.

python script that connects with vsftpd

I have the following issue... i need to connect to sftp(vsftpd) through python script, upload a file and download it. So far i have created ftp server with vsftpd but i cannot connect. If i use the terminal(ftp localhost) i can log in without any problems. Please advise how i should proceed.
Here is my python script:
http://codepaste.net/omysxu
and here is the config file of vsftpd:
http://codepaste.net/1qrrdf
The thing that i am trying to do is.. set up a ftp server, then i should have 2 scripts(or 1) which will have to upload file via ssh then download it in another dir.
vsftpd is a FTP server and can not be accessed with the SFTP protocol. FTP, SFTP and FTPS are often confused:
FTP = file transfer protocol (RFC959) - supported by vsftpd
FTPS = extension for using TLS with FTP (RFC4217) - supported by vsftpd
SFTP = file transfer using the SSH protocol - not supported by vsftpd, you need SSH for this.

Categories

Resources