繁体   English   中英

在Paramiko中运行交互式命令

[英]Running interactive commands in Paramiko

我正在尝试通过paramiko运行交互式命令。 该cmd执行尝试提示输入密码,但是我不知道如何通过paramiko的exec_command提供密码,并且执行挂起。 如果cmd执行期望交互输入,是否可以将值发送到终端?

ssh = paramiko.SSHClient()
ssh.connect(server, username=username, password=password)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("psql -U factory -d factory -f /tmp/data.sql")

有谁知道这可以解决吗? 谢谢。

完整的paramiko发行版附带了很多很好的演示

在demos子目录中, demo.pyinteractive.py具有完整的交互式TTY示例,这可能对您的情况有些过分。

在上面的示例中, ssh_stdin行为类似于标准的Python文件对象,因此,只要通道仍处于打开状态, ssh_stdin.write应该起作用。

我从不需要写stdin,但是文档建议在命令退出后立即关闭通道,因此使用标准的stdin.write方法发送密码可能无效。 通道本身上有一些较低级别的paramiko命令,可以为您提供更多控制权-有关所有详细信息,请参见SSHClient.exec_command方法的实现方式。

我在尝试使用ssh (Paramiko的一个分支)进行交互式ssh会话时遇到了相同的问题。

我四处寻找,发现了这篇文章:

更新的链接 (链接生成404之前的最新版本): http ://web.archive.org/web/20170912043432/http: //jessenoller.com/2009/02/05/ssh-programming-with-paramiko-completely -不同/

要继续您的示例,您可以做

ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("psql -U factory -d factory -f /tmp/data.sql")
ssh_stdin.write('password\n')
ssh_stdin.flush()
output = ssh_stdout.read()

本文更加深入,描述了围绕exec_command的完全交互式shell。 我发现这比源代码中的示例更易于使用。

原始链接http : //jessenoller.com/2009/02/05/ssh-programming-with-paramiko-completely-different/

您需要Pexpect才能兼得两者(expect和ssh包装器)。

我不熟悉paramiko,但这可能有效:

ssh_stdin.write('input value')
ssh_stdin.flush()

有关stdin的信息:

http://docs.python.org/library/sys.html?highlight=stdin#sys.stdin

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(server_IP,22,username, password)


stdin, stdout, stderr = ssh.exec_command('/Users/lteue/Downloads/uecontrol-CXC_173_6456-R32A01/uecontrol.sh -host localhost ')
alldata = ""
while not stdout.channel.exit_status_ready():
   solo_line = ""        
   # Print stdout data when available
   if stdout.channel.recv_ready():
      # Retrieve the first 1024 bytes
      solo_line = stdout.channel.recv(1024) 
      alldata += solo_line
   if(cmp(solo_line,'uec> ') ==0 ):    #Change Conditionals to your code here  
     if num_of_input == 0 :
      data_buffer = ""    
      for cmd in commandList :
       #print cmd
       stdin.channel.send(cmd)        # send input commmand 1
      num_of_input += 1
     if num_of_input == 1 :
      stdin.channel.send('q \n')      # send input commmand 2 , in my code is exit the interactive session, the connect will close.
      num_of_input += 1 
print alldata
ssh.close()              

如果在不检查stdout.channel.recv_ready()的情况下正确使用,为什么stdout.read()会挂起,而在stdout.channel.exit_status_ready()中:

就我而言,在远程服务器上运行命令后,会话正在等待用户输入,输入“ q”后,它将关闭连接。 但是在输入'q'之前,stdout.read()将等待EOF,如果buffer较大,此方法似乎不起作用。

  • 我在while尝试过stdout.read(1),它有效
    我在while尝试过stdout.readline(),它也可以工作。
    stdin,stdout,stderr = ssh.exec_command('/ Users / lteue / Downloads / uecontrol')
    stdout.read()将挂起

看一个例子,以类似的方式做

(源自http://jessenoller.com/2009/02/05/ssh-programming-with-paramiko-completely-different/ ):

    ssh.connect('127.0.0.1', username='jesse', 
        password='lol')
    stdin, stdout, stderr = ssh.exec_command(
        "sudo dmesg")
    stdin.write('lol\n')
    stdin.flush()
    data = stdout.read.splitlines()
    for line in data:
        if line.split(':')[0] == 'AirPort':
            print line

您可以使用此方法发送所需的任何确认消息,例如“确定”或密码。 这是我的解决方案示例:

def SpecialConfirmation(command, message, reply):
    net_connect.config_mode()    # To enter config mode
    net_connect.remote_conn.sendall(str(command)+'\n' )
    time.sleep(3)
    output = net_connect.remote_conn.recv(65535).decode('utf-8')
    ReplyAppend=''
    if str(message) in output:
        for i in range(0,(len(reply))):
            ReplyAppend+=str(reply[i])+'\n'
        net_connect.remote_conn.sendall(ReplyAppend)
        output = net_connect.remote_conn.recv(65535).decode('utf-8') 
    print (output)
    return output

CryptoPkiEnroll=['','','no','no','yes']

output=SpecialConfirmation ('crypto pki enroll TCA','Password' , CryptoPkiEnroll )
print (output)

暂无
暂无

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

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