【问题标题】:I want to execute multiple commands through a single channel of ssh, implemented using paramiko library.我想通过单个 ssh 通道执行多个命令,使用 paramiko 库实现。
【发布时间】:2013-06-24 14:57:30
【问题描述】:

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

但每次我使用exec_command(command) 时都会打开一个新通道并将命令发送到交换机。

由于命令必须按顺序执行,所以什么都不会执行。

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

【问题讨论】:

  • 我想你的答案是here

标签: python ssh command paramiko


【解决方案1】:

在 ssh 的单个通道中执行多个命令。您需要通过

调用交互式shell
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, port, username, password)
channel = ssh.invoke_shell()

然后通过

向终端发送命令
channel.send(command)

并接收命令输出

channel.recv(9999)

打印输出

channel.recv(9999).decode()

【讨论】:

    【解决方案2】:

    如果您的目标是在同一主机中发送多个命令并接收这些命令的结果,我编写了一个运行良好的脚本(在本例中是 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')
    

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

    【讨论】:

      最近更新 更多