简体   繁体   English

Python /使用ssh创建目录(使用pxssh)

[英]Python/ create directory with ssh (using pxssh)

Hi use pxssh and do the following: 嗨,使用pxssh并执行以下操作:

  s = pxssh.pxssh()
  if not s.login (ip, user, password):
      print("SSH session failed on login.")
      print(str(s))
  else:
      print("SSH session login successful")
      if not os.path.exists("/root/a"):
             os.makedirs("/root/a")

The problem is that /root/a is searched in the local machine, and not in the machine I connect to. 问题是/root/a是在本地计算机中搜索的,而不是在我连接的计算机中搜索的。

How can I create the directory in the machine I connect to? 如何在连接的计算机上创建目录?

Acording to the docs , you have to send lines of commands through the object, and not run local commands as you are doing. 根据docs ,您必须通过该对象发送命令行,而不能像执行操作那样运行本地命令。

According to this link , run the commands as you are in the terminal of the machine you're connected to. 根据此链接 ,在要连接的计算机的终端中直接运行命令。 Something like s.sendline('mkdir /root/a') . s.sendline('mkdir /root/a')

Another solution would be to create this script .py, send to the machine, and the run the script in the machine using the code you've written. 另一个解决方案是创建此脚本.py,发送到计算机,然后使用您编写的代码在计算机中运行脚本。 You would connect via SSH and run python script.py command. 您将通过SSH连接并运行python script.py命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM