Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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 我想通过ssh的一个通道执行多个命令,使用paramiko库实现。_Python_Ssh_Command_Paramiko - Fatal编程技术网

Python 我想通过ssh的一个通道执行多个命令,使用paramiko库实现。

Python 我想通过ssh的一个通道执行多个命令,使用paramiko库实现。,python,ssh,command,paramiko,Python,Ssh,Command,Paramiko,我需要在存储网络中交换机的同一个已建立通道中执行多个命令(以特定的顺序) 但是每次我使用exec\u命令(command)时,一个新的通道被打开,命令被发送到交换机 由于命令必须按顺序执行,因此不会执行任何操作 所以,我的疑问是,如何通过单个ssh通道发送多个顺序命令,使用paramiko库实现 如果您的目标是在同一主机中发送多个命令并接收这些命令的结果,那么我已经编写了一个运行良好的脚本(在本例中是ping几个元素): 通过每次打开一个新连接,可以发送多个命令。希望有帮助。我想你的答案是。 #

我需要在存储网络中交换机的同一个已建立通道中执行多个命令(以特定的顺序)

但是每次我使用
exec\u命令(command)
时,一个新的通道被打开,命令被发送到交换机

由于命令必须按顺序执行,因此不会执行任何操作


所以,我的疑问是,如何通过单个ssh通道发送多个顺序命令,使用
paramiko
库实现

如果您的目标是在同一主机中发送多个命令并接收这些命令的结果,那么我已经编写了一个运行良好的脚本(在本例中是ping几个元素):

通过每次打开一个新连接,可以发送多个命令。希望有帮助。

我想你的答案是。
# -*- coding: utf-8 -*-
"""
Created on Thu May  5 10:56:10 2016

@author: Me
"""

import paramiko as pk

class cii(Exception):
    pass
num = int(input('number : '))

while True :
    try:
        if len(str(num)) != 5 :
            raise cii

    except cii:
        print ('error message') 
        break
    else:   
        with open (r'path.txt') as search :

            try:
                for line in search :

                    line = line.rstrip()
                    line1 = line.split(",")        
                    IP = line1[2]
                    line2 = line1[2].split('.')
                    line3 = (int(line2[3])+1)
                    del line2[3] 
                    line2.append(str(line3))
                    CA = str(".".join(line2))
                    Machine = line1[1]
                    Command = 'ping -n 1'

                    if str(num) in line and yy == 'xx' :

                        ssh = pk.SSHClient()
                        ssh.set_missing_host_key_policy(
                            pk.AutoAddPolicy())
                        ssh.connect('{}'.format(IP), port=xxxx, username='xxxxxx', 
                            password='xxxx')
                        stdin, stdout, stderr = \
                        ssh.exec_command('ping -n 1 xxx.xxx.xxx.xxx\n')
                        print('Ping xx: \n', stdout.readlines())

                        ssh = pk.SSHClient()
                        ssh.set_missing_host_key_policy(
                            pk.AutoAddPolicy())
                        ssh.connect('{}'.format(IP), port=xxxx, username='xxxxxx', 
                            password='xxxx')
                        stdin, stdout, stderr = \
                        ssh.exec_command('ping -n 1 xxx.xxx.xxx.xxx\n')
                        print('Ping xx: \n', stdout.readlines())

                        ssh = pk.SSHClient()
                        ssh.set_missing_host_key_policy(
                            pk.AutoAddPolicy())
                        ssh.connect('{}'.format(IP), port=xxxx, username='xxxxxx', 
                            password='xxxx')
                        stdin, stdout, stderr = \
                        ssh.exec_command('ping -n 1 xxx.xxx.xxx.xxx\n')
                        print('Ping xx: \n', stdout.readlines())

                        ssh = pk.SSHClient()
                        ssh.set_missing_host_key_policy(
                            pk.AutoAddPolicy())
                        ssh.connect('{}'.format(xx), port=xxxx, username='xxxxxxx', 
                            password='xxxxxxx')
                        stdin, stdout, stderr = \
                        ssh.exec_command('{} {}'.format(Command, CA).encode('ascii') + b"\n")
                        print('Ping CA: \n', stdout.readlines())
                break
            except TimeoutError:
                print ('error message')
            break
input ('\n keep windows open and then Enter to exit \n')