paramiko -SSH学习
生活随笔
收集整理的這篇文章主要介紹了
paramiko -SSH学习
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
SSHClient
用于連接遠(yuǎn)程服務(wù)器并執(zhí)行基本命令
1、SSH登陸到交換機(jī)執(zhí)行指令,并且將結(jié)果反饋到file1
import?paramiko
# 創(chuàng)建SSH對(duì)象 ssh?=?paramiko.SSHClient() # 允許連接不在know_hosts文件中的主機(jī) ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 連接服務(wù)器 ssh.connect(hostname='IP', port=22, username='admin', password='123') ?? # 執(zhí)行命令 stdin, stdout, stderr?=?ssh.exec_command('display interface brief') # 獲取命令結(jié)果 result?=?stdout.read() f = open("file1","wb") f.write(result) f.close() ssh.close() # 關(guān)閉連接 ssh.close() 另外一種方法,有登陸日志,不錯(cuò) # -*- coding: utf-8 -*-import paramiko
def sshclient_execmd(hostname, port, username, password, execmd):
paramiko.util.log_to_file("paramiko.log")
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=hostname, port=port, username=username, password=password)
stdin, stdout, stderr = ssh.exec_command(execmd)
stdin.write("Y") # Generally speaking, the first connection, need a simple interaction.
a = stdout.read()
file = open("log","wb")
file.write(a)
file.close()
ssh.close()
def main():
hostname = ''
port = 22
username = ''
password = ''
execmd = "display ip interface brief"
sshclient_execmd(hostname, port, username, password, execmd)
轉(zhuǎn)載于:https://www.cnblogs.com/hapen66/p/7307122.html
總結(jié)
以上是生活随笔為你收集整理的paramiko -SSH学习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 7_CentOS下安装和卸载AdobeR
- 下一篇: jqgrid 启用键盘操作bindKey