1

I need to access and open a file on a server with python. I have a Centos server which I can access from terminal with ssh [email protected] and enter the user password.

Question
If I have a file on the server in this path: /var/document.txt what do I write to access that path and open the document.txt on my local machine?

If the file was on my local machine, I could read it with this:

import glob

# for example if i want to see file in the folder
for f in glob.glob('/var/*.*'):
    print(f) # output --> document.txt
    # read file
    read = open(f, 'r')

How do I access the file if it is on the server? I do not want to download the file, edit it and upload it again.

6
  • Is python installed on the remote server? Commented Jul 11, 2020 at 14:42
  • You will have to download the file and process it if you want to process the file on the local machine Commented Jul 11, 2020 at 14:43
  • @bigbounty nope, on my local machine but i want to read file that on the server. Commented Jul 11, 2020 at 14:43
  • Try looking into sshfs, to either mount the remote file system on your local machine, or second option, download the file from server to your local machine, and then run the script here, or third option, upload the script to server and run the script there. Commented Jul 11, 2020 at 14:44
  • Might be helpful: pypi.org/project/scp Commented Jul 11, 2020 at 14:44

1 Answer 1

2
from contextlib     import closing
from fabric.network import connect
user = 'root' # your SSH user
host = '172.24.2.233' #IP of your server
port = '22' #SSH Port
remote_file = '/var/document.txt'
with closing(connect(user, host, port)) as ssh, \
     closing(ssh.open_sftp()) as sftp, \
     closing(sftp.open(remote_file)) as file:
    for line in file:
        print(line)
Sign up to request clarification or add additional context in comments.

7 Comments

thank you for response, i think this is what i looking for but now i have some error like this ModuleNotFoundError: No module named 'fabric.network' despite having already installed the fabric with this command pip install fabric and now my fabric version is fabric 2.5.0, so what is a problem?
Are you using a virtual enviroment (venv)? Maybe you did not install it in the venv but on the system wide installation. What python version are you using?
Python 3.8.2, and yes i did not install it in the venv.
but you are using a venv? If so, use the venv and install it there. Then it should work.
@Kaow, why don't you use directly scp ? Meaning from local machine instead of ssh , do scp [email protected]:/var/document.txt . and you'l have the file directly on you machine in the directory from which you launched the scp command (scp uses the SSH protocol to copy files across system by extending the syntax of cp.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.