Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python脚本不发送linux命令。我使用Paramiko进行远程SSH连接_Linux_Ssh_Paramiko_Python 3.6 - Fatal编程技术网

Python脚本不发送linux命令。我使用Paramiko进行远程SSH连接

Python脚本不发送linux命令。我使用Paramiko进行远程SSH连接,linux,ssh,paramiko,python-3.6,Linux,Ssh,Paramiko,Python 3.6,我正在使用paramiko进行ssh连接。我能够登录到linux机器。然后我检查条件,即如果信道_data.endswith('[root@home~]#')然后发送“cd/”命令,如果通道_data.endswith('[root@home/]#')然后发送“mkdir BB444”命令,但此脚本不发送这些命令。调试后,我看到这些发送命令语句没有执行。请让我知道我在这里犯了什么错误 我使用的是python 3.6、paramiko 2.1.1,问题可能不是python脚本。在从python脚本

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


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

谢谢Sivapasath。实际上,Linux服务器只在公司网络上运行。我将与管理员核实您的建议,但目前我正在发送非常简单的命令,如“cd/”或“pwd”或“mkdir pn444”。您正在发送远程命令,这是安全问题。因此没有命令通过。还有一件事,如果您使用的是CISCO服务器或路由器,那么它是一种内置的安全机制,可以从ssh连接命令中删除远程命令。这意味着我能够成功地从脚本发送命令。下面是代码。导入paramiko 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.ok basu')stdin,stdout,stderr=ssh.exec_命令('mkdir BB444')output1=stdout.readlines()print('created a new directory is',output1)如果只执行第一个命令,则可能是因为您的远程命令只执行一个命令。很少有其他ssh程序支持多行远程命令。但大多数程序只执行第一个命令并退出环境。在传递下一个命令之前。再次检查paramiko的远程命令属性。是否可以支持多行远程命令通常提示以空格结尾。可能
endswith('[root@home/]#')
?或
'[root@home ~]#“
。谢谢Girisha。您的建议解决了这个问题。Hi Girisha,我在其他情况下遇到了类似的问题。在命令交互期间,当输出带有字符串“您想现在配置网络吗?(是/否)[默认是]”,脚本需要发送“是”命令。下面是为此.channel.send编写的代码('yes\n')未执行.elif channel_data.endswith('是否立即配置主网络?(是/否)[默认是]):channel.send('是\n')
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")