明霞山资源网 Design By www.htccd.com
这篇文章主要介绍了python 采用paramiko 远程执行命令及报错解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
import sys
import paramiko
import config_reader
from check_utils import standout_print, parse_remainsize_response_lines, error_out_print
from time import time
class RemoteModel:
""" remote options model
execute remote command
"""
def __init__(self, host, port=22):
self.hostname = host
self.port = port
self.username, self.password = self.load_conf()
self.s = None
self.session = None
self.init_conn()
def load_conf(self):
"""
read config get the login info of remote host machine
:return:
login username and password of SSH login of this host
"""
if self.hostname.find("10.179.1.110") != -1:
error_out_print("Error : the remote machine of KOR can not provide. please know")
sys.exit(-1)
username, password = config_reader.read_login_config(self.hostname)
if not username or not password:
error_out_print(
'Error: can not find ssh login info in this host[%s]. check need ' % self.hostname)
sys.exit(-1)
return username, password
def init_conn(self):
"""
make a connection with the remote machine
:return:
"""
try:
paramiko.util.log_to_file("paramiko_log.log")
self.s = paramiko.SSHClient()
self.s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.s.connect(hostname=self.hostname, port=self.port, username=self.username, password=self.password)
standout_print('success connect the remote machine [host=%s]' % self.hostname)
except Exception, e:
standout_print(str(e))
standout_print(
'connect failed.in host[%s] user[%s] or pwd[%s] maybe wrong. ' % (
self.hostname, self.username, self.password))
sys.exit(-1)
def close(self):
"""
close
if close can not use this connection
:return:
"""
if self.s:
self.s.close()
self = None
def execute_command(self, command):
"""
:param command:
execute cmd
:return:
the response lines
"""
standout_print("Info: execute command [%s]" % command)
stdin, stdout, stderr = self.s.exec_command(command)
stdin.write("pwd"+"\n")
stdin.flush()
response_lines = stdout.readlines()
error_info = stderr.read()
if error_info and error_info.strip():
error_out_print(' remote command error info : %s' % stderr.read())
error_out_print(error_info)
return None
# info_arr = response_info.split('\n')
return response_lines
def remain_space_size(self, directory_path):
"""
:param directory_path:
:return:
free size of the directory
unit size : MB
"""
cmd = 'sudo df -m %s 1>&2' % directory_path # /usr/local/pgsql/data/ssd1
response_lines = self.execute_command(cmd)
# response_lines = self.execute_command_channel(cmd)
return parse_remainsize_response_lines(response_lines)
def execute(self, command, sudo=False):
feed_password = False
if sudo and self.username != "root":
command = "sudo %s" % command
feed_password = "pwd"
stdin, stdout, stderr = self.s.exec_command(command, get_pty=True)
if feed_password:
stdin.write(self.password + "\n")
stdin.flush()
return {'out': stdout.readlines(),
'err': stderr.readlines(),
'retval': stdout.channel.recv_exit_status()}
if __name__ == '__main__':
host = ""
hostname = ""
command = "sudo df -m /data/pgsql94/data"
rm = RemoteModel(host=hostname)
print rm.execute_command(command)
# print rm.execute("df -m /data/pgsql94/data 1>&2", True)
报错1:
remote command error info : sudo: sorry, you must have a tty to run sudo
是由于
self.s.exec_command(command, get_pty=True)
没有设置
get_pty=True
报错2:
会卡死在
stdout.readlines()
是由于 SSH在等待输入用户名的密码
stdin.write("pwd"+"\n")
stdin.flush()
该种方式进行交互,注意必须要换行"\n",和前面必须不能有空格等其他字符,确保密码正确
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
明霞山资源网 Design By www.htccd.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
明霞山资源网 Design By www.htccd.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。