#!/usr/bin/python
# -*- coding: UTF-8 -*-
#coding=utf8
''' 执行shell命令 '''
import paramiko
from paramiko.py3compat import u
import time
class SSHClient(object):
def close(self, ssh):
ssh.close()
def sshclient_execmd(self,hostname,port,username,password,execmd1):
paramiko.util.log_to_file("paramiko.log")
print "hostname %s,port %s, username %s, password %s, execmd %s" % (hostname, port, username, password, execmd1)
# 建立一个sshclient对象
ssh = paramiko.SSHClient()
# 允许将信任的主机自动加入到host_allow 列表,此方法必须放在connect方法的前面
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_system_host_keys()
# 调用connect方法连接服务器
ssh.connect(hostname=hostname, port=port, username=username, password=password)
try:
# 执行命令
stdin, stdout, stderr = ssh.exec_command(execmd1)
# 结果放到stdout中,如果有错误将放到stderr中
stdin.write("Y") # Generally speaking, the first connection, need a simple interaction.
# print(stdout.read().decode())
print stdout.read()
ssh.close()
except :
# 关闭连接
ssh.close()
print ("BUG")#
# if __name__ == '__main__':
# pass
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货