【问题标题】:Python script not sending linux commands. i used Paramiko for remote SSH connectionPython 脚本不发送 linux 命令。我使用 Paramiko 进行远程 SSH 连接
【发布时间】:2017-02-14 15:08:04
【问题描述】:
import paramiko
import time
import os


ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('server',port=22,username='user',password='pass123')
print("connected to the linux machine from windows machine.")

channel=ssh.invoke_shell()

channel_data = str()

while True:
    if channel.recv_ready():
        channel_data += channel.recv(9999).decode(encoding='utf_8', errors='strict')
        os.system('cls')
        print("##### Device Output #####")
        print("\n",channel_data)
        print("\n #####################")
    else:
        continue

    time.sleep(5)

    if channel_data.endswith('[root@home ~]#'):
        #if block statements are not executed why
        print("Hi,why not executing this statement")
        ssh.send('cd /\n')
        #stdin,stdout,stderr=ssh.exec_command('pwd')
        #output1=stdout.readlines()
        print("My present working directory is")
    elif channel_data.endswith('[root@home /]#'):
        #Also elif block statements are not executed why
        ssh.send('mkdir BB444')
        #stdin,stdout,stderr=ssh.exec_command('mkdir /pn444')
        #output1=stdout.readlines()
        print("created pn444 directory")

我正在使用 paramiko 进行 ssh 连接。我能够登录到linux机器。然后我正在检查条件,即如果 channel_data.endswith('[root@home ~]#') 然后发送“cd /”命令,否则如果 channel_data.endswith('[root@home /]#') 然后发送'mkdir BB444 ' 命令,但此脚本不发送这些命令。调试后我看到这些发送命令语句没有被执行。请让我知道我在这里犯了什么错误。

我正在使用 python 3.6,paramiko 2.1.1

【问题讨论】:

  • 提示符通常以空格结尾。也许endswith('[root@home /]# ')?或'[root@home ~]# '.
  • 谢谢 Girisha。您的建议解决了问题。
  • 嗨 Girisha,我在其他情况下也遇到了类似的问题。在命令交互期间,当输出带有字符串 'Would you like to configure Network now? (yes/no) [default yes] ',脚本需要发送'yes'命令。下面是为此编写的代码。 channel.send('yes\n') 没有执行。 elif channel_data.endswith('现在要配置 Prime Network 吗?(yes/no) [default yes] '): channel.send('yes\n')

标签: linux ssh paramiko python-3.6


【解决方案1】:

问题可能不在于 python 脚本。我在从 python 脚本向 putty ssh 连接发送远程命令时遇到了类似的问题。如果您在公司网络中。一些管理员从参数中删除命令,例如我用于 putty 的 -m。因此您可以登录,但是当您尝试发送远程命令时。它没有到达服务器。如果您在公司网络上,请咨询您的管理员以了解 ssh 连接的远程命令。

【讨论】:

  • 感谢 Sivaprasath。实际上只有企业网络上的 Linux 服务器。我会与管理员核实您的建议,但目前我正在发送非常简单的命令,例如“cd /”或“pwd”或“mkdir pn444”
  • 您正在发送远程命令,这是安全问题。所以没有命令通过。如果您使用的是 CISCO 服务器或路由器,那么还有一件事是内置安全功能,可以将远程命令从 ssh 连接命令中剥离。
  • 我可以从脚本创建一个文件夹。这意味着我能够从脚本成功发送命令。下面是代码。 import paramiko ssh=paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('server',port=22,username='user',password='pass123') print("连接到linux windows机器上的机器。好吧basu") stdin,stdout,stderr=ssh.exec_command('mkdir BB444') output1=stdout.readlines() print('created a new directory is',output1)
  • 如果只执行第一个命令,那么可能是因为您的远程命令只需要一个命令。很少有其他 ssh 程序支持多行远程命令。但他们中的大多数只是执行第一个命令并退出环境。在下一个命令可以通过之前。再次检查 paramiko 的远程命令属性。是否支持多行远程命令
猜你喜欢
  • 2011-09-12
  • 2021-01-10
  • 2011-06-21
  • 1970-01-01
  • 2018-12-04
  • 2021-02-28
  • 2016-02-05
  • 2012-07-16
相关资源
最近更新 更多