使用 Python Paramiko 在远程服务器上执行的脚本不能 read/access 本地文件
Script executed on remote server using Python Paramiko cannot read/access local files
我正尝试从我的笔记本电脑(工作正常)在远程服务器 (VPS) 上 运行 本地 Python 脚本。
脚本无法从 VPS 我的笔记本电脑读取本地文件
输出:
我在 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)
当我 运行 来自 VPS 的脚本时,我遇到了这个问题
我无法 运行 直接从 VPS 脚本来检查问题,因为我使用的是本地文件,请查看屏幕截图:
当我删除本地路径和 运行 脚本(来自 pycharm 和 VPS)时,它工作正常。
您无法从服务器上的脚本 运行 神奇地访问本地文件。
I can't run the script directly from VPS to check the issue because i use local files, check img.
使用您最喜欢的 SSH 终端客户端运行远程 shell 中的脚本没有区别(我假设这就是您所说的 "运行直接从 VPS") 编写脚本,并使用 Paramiko 运行 在远程 shell 中设置脚本。它仍然 运行s 在远程 shell。
没有简单的方法可以从服务器访问客户端文件。那将是一场安全噩梦。
要么您的脚本必须将文件上传到服务器。
或者您需要 运行 在您的本地计算机上安装一个 (SFTP/FTP/whatever) 服务器以使您的本地文件可供全世界访问。
有关如何 运行 SFTP 服务器的示例,请参阅我的指南:
Installing SFTP/SSH server on Windows using OpenSSH
我正尝试从我的笔记本电脑(工作正常)在远程服务器 (VPS) 上 运行 本地 Python 脚本。
脚本无法从 VPS 我的笔记本电脑读取本地文件 输出:
我在 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)
当我 运行 来自 VPS 的脚本时,我遇到了这个问题
我无法 运行 直接从 VPS 脚本来检查问题,因为我使用的是本地文件,请查看屏幕截图:
当我删除本地路径和 运行 脚本(来自 pycharm 和 VPS)时,它工作正常。
您无法从服务器上的脚本 运行 神奇地访问本地文件。
I can't run the script directly from VPS to check the issue because i use local files, check img.
使用您最喜欢的 SSH 终端客户端运行远程 shell 中的脚本没有区别(我假设这就是您所说的 "运行直接从 VPS") 编写脚本,并使用 Paramiko 运行 在远程 shell 中设置脚本。它仍然 运行s 在远程 shell。
没有简单的方法可以从服务器访问客户端文件。那将是一场安全噩梦。
要么您的脚本必须将文件上传到服务器。
或者您需要 运行 在您的本地计算机上安装一个 (SFTP/FTP/whatever) 服务器以使您的本地文件可供全世界访问。
有关如何 运行 SFTP 服务器的示例,请参阅我的指南:
Installing SFTP/SSH server on Windows using OpenSSH