linux远程登录设备后下载文件
import subprocess
import os
import time
import logging
import re
import paramiko
import string
import sys
import datetime
from ftplib import FTP
import getpass
from scp import SCPClient
class RemoteMonitor:
ip = ''
port = 22
username = ''
password = ''
def __init__(self,ip,username,password):
self.ip = ip
self.port = 22
self.username = username
self.password = password
def SSHConnection(self):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(self.ip,22,self.username,self.password)
self.ssh = ssh
def __ExecCommand(self,cmd):
stdin, stdout, stderr = self.ssh.exec_command(cmd)
result = stdout.readlines()
# print (stderr,result,len(result))
return result
def ExecCommand(self,cmd):
stdin, stdout, stderr = self.ssh.exec_command(cmd)
result = stdout.readlines()
# print (stderr,result,len(result))
return result
def scp_file(self,remotepath,localpath1):
# self.ssh.exec_command('scp'+' '+file_name+' '+'test@192.168.199.177:~/')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(self.ip,22,self.username,self.password)
scpclient = SCPClient(ssh.get_transport(), socket_timeout=15.0)
scpclient.get(remotepath, localpath1)
ssh.close()
if __name__ == "__main__":
hostname = sys.argv[1]
password=sys.argv[2]
monitor = RemoteMonitor(hostname,"nvidia",password)
monitor.SSHConnection()
logging.basicConfig(level=logging.INFO,
format="[%(asctime)s] %(name)s:%(levelname)s: %(message)s",
datefmt='%a, %d %b %Y %H:%M:%S',
filename='test_005.log',
filemode='w')
monitor.scp_file(remotepath,localpath1)