python paramiko ssh

前端 未结 4 1792
广开言路
广开言路 2020-12-01 06:01

i\'m new on python. i wrote a script to connect to a host and execute one command

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAdd         


        
相关标签:
4条回答
  • 2020-12-01 06:07

    There is extensive paramiko API documentation you can find at: http://docs.paramiko.org/en/stable/index.html

    I use the following method to execute commands on a password protected client:

    import paramiko
    
    nbytes = 4096
    hostname = 'hostname'
    port = 22
    username = 'username' 
    password = 'password'
    command = 'ls'
    
    client = paramiko.Transport((hostname, port))
    client.connect(username=username, password=password)
    
    stdout_data = []
    stderr_data = []
    session = client.open_channel(kind='session')
    session.exec_command(command)
    while True:
        if session.recv_ready():
            stdout_data.append(session.recv(nbytes))
        if session.recv_stderr_ready():
            stderr_data.append(session.recv_stderr(nbytes))
        if session.exit_status_ready():
            break
    
    print 'exit status: ', session.recv_exit_status()
    print ''.join(stdout_data)
    print ''.join(stderr_data)
    
    session.close()
    client.close()
    
    0 讨论(0)
  • 2020-12-01 06:12
    ###### Use paramiko to connect to LINUX platform############
    import paramiko
    
    ip='server ip'
    port=22
    username='username'
    password='password'
    ssh=paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(ip,port,username,password)
    
    --------Connection Established-----------------------------
    
    ######To run shell commands on remote connection###########
    import paramiko
    
    ip='server ip'
    port=22
    username='username'
    password='password'
    ssh=paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(ip,port,username,password)
    
    
    stdin,stdout,stderr=ssh.exec_command(cmd)
    outlines=stdout.readlines()
    resp=''.join(outlines)
    print(resp) # Output 
    
    0 讨论(0)
  • 2020-12-01 06:13

    There is something wrong with the accepted answer, it sometimes (randomly) brings a clipped response from server. I do not know why, I did not investigate the faulty cause of the accepted answer because this code worked perfectly for me:

    import paramiko
    
    ip='server ip'
    port=22
    username='username'
    password='password'
    
    cmd='some useful command' 
    
    ssh=paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(ip,port,username,password)
    
    stdin,stdout,stderr=ssh.exec_command(cmd)
    outlines=stdout.readlines()
    resp=''.join(outlines)
    print(resp)
    
    stdin,stdout,stderr=ssh.exec_command('some really useful command')
    outlines=stdout.readlines()
    resp=''.join(outlines)
    print(resp)
    
    0 讨论(0)
  • 2020-12-01 06:19

    The code of @ThePracticalOne is great for showing the usage except for one thing: Somtimes the output would be incomplete.(session.recv_ready() turns true after the if session.recv_ready(): while session.recv_stderr_ready() and session.exit_status_ready() turned true before entering next loop)

    so my thinking is to retrieving the data when it is ready to exit the session.

    while True:
    if session.exit_status_ready():
    while True:
        while True:
            print "try to recv stdout..."
            ret = session.recv(nbytes)
            if len(ret) == 0:
                break
            stdout_data.append(ret)
    
        while True:
            print "try to recv stderr..."
            ret = session.recv_stderr(nbytes)
            if len(ret) == 0:
                break
            stderr_data.append(ret)
        break
    
    0 讨论(0)
提交回复
热议问题