python paramiko ssh登錄交換機執行命令


 

# encoding=utf-8
import paramiko
import time
client = paramiko.SSHClient()
client.load_system_host_keys()
 
# connect to client
client.connect('192.168.254.141',22,'test','test',allow_agent=False,look_for_keys=False)
 
# get shell
ssh_shell = client.invoke_shell()
# ready when line endswith '>' or other character
while True:
    line = ssh_shell.recv(1024)
    #print line
    if line and line.endswith('>'):
        break;
 
# send command
ssh_shell.sendall( 'ping 192.168.254.142' + '\n')
 
# get result lines
lines = []
while True:
    line = ssh_shell.recv(1024)
    if line and line.endswith('>'):
        break;
    lines.append(line)
result = ''.join(lines)
 
# print result
print result

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM