python paramiko ssh

前端 未结 4 1802
广开言路
广开言路 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: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)
    

提交回复
热议问题