python 链接交换机并执行相关命令


原文地址 https://blog.csdn.net/u010897775/article/details/80311786?utm_source=blogxgwz0

# 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