python远程登录
import subprocess
import os
import time
import logging
import re
import paramiko
import string
import sys
import datetime
import threading
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 sftp_upload(self,local, remote):
sf = paramiko.Transport((self.ip, self.port))
sf.connect(username=self.username, password=self.password)
sftp = paramiko.SFTPClient.from_transport(sf)
try:
if os.path.isdir(local):
for f in os.listdir(local):
sftp.put(os.path.join(local + f), os.path.join(remote + f))
else:
sftp.put(local, remote)
except Exception, e:
print('upload exception:', e)
sf.close()
def sftp_get(self,remote,local):
sf = paramiko.Transport((self.ip, self.port))
sf.connect(username=self.username, password=self.password)
sftp = paramiko.SFTPClient.from_transport(sf)
sftp.get(remote,local)
sf.close()
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
if __name__ == "__main__":
hostname = sys.argv[1]
password=sys.argv[2]
monitor = RemoteMonitor(hostname,"nvidia",password)
monitor.SSHConnection()