#coding=utf8
import paramiko
import time
import logging '''
if user root,can not login,must use user xx and then switch to root not root ,then run ''' def _conn_root(ip,port,username,passwd,cmd):
s = paramiko.SSHClient()
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
#此处写入一个用于登录的用户
s.connect(hostname=ip, port=int(port), username='xx', password='xx')
except:
logging.info('err: can not conn %s ,pls check %s and password of xx',(ip,ip)) if username == 'root' or 'xx':
ssh = s.invoke_shell()
time.sleep(1)
if username=='xx':
ssh.send('su - xx\n')
else:
ssh.send('su -\n')
buff = ''
while not buff.endswith('Password: '):
resp = ssh.recv(2048)
buff += resp
ssh.send(passwd)
ssh.send('\n')
buff = ''
while not buff.endswith('# '):
resp = ssh.recv(2048)
buff += resp
ssh.send(cmd)
ssh.send('\n')
buff = ''
time.sleep(1)
resp = ssh.recv(2048)
resp = ''
while not buff.endswith('# '):
resp = ssh.recv(2048)
print resp
buff += resp
s.close()
result = buff
else:
stdin, stdout, stderr = s.exec_command(cmd)
result = stdout.read()
s.close() return result def _conn_nomal(ip,port,username,passwd,cmd):
s = paramiko.SSHClient()
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
s.connect(hostname=host, port=int(port), username=username, password=password)
except:
logging.info('err: can not connect %s ,pls check %s and user && password',(ip,ip))
#'get result'
stdin, stdout, stderr = s.exec_command(cmd)
result = stdout.read()
s.close()
return result def Connect(ip,port=22,username='xx',passwd='xx',cmd='echo '): if username=='root' or 'xx':
return _conn_root(ip,port,username,passwd,cmd)
else:
return _conn_nomal(ip,port,username,passwd,cmd)

  

paramiko_su_root的更多相关文章

随机推荐

  1. dom监听事件class

    layui.use(['layer', 'form'], function(){ var layer = layui.layer ,form = layui.form; var $ = layui.j ...

  2. 谈谈如何来查看GC日志

    一.首先来看一下JVM中的GC有哪几种类型? 1.-XX:UseSerialGC 虚拟机运行在Client模式的默认值,打开此开关参数后,使用Serial+Serial Old收集器组合进行垃圾收集. ...

  3. webservice和一般处理程序

    一丶WebService 1.新建项目 2.选择Web窗体 3.添加新建项 二丶一般处理程序 前台访问: $.ajax({ type: "post", url: "Han ...

  4. Spring.Boot.1 -- 概览

    Spring Boot 是如何简化Java 开发的 SpringBoot的一些重要特征 长久以来,Spring 框架作为Java应用开发的框架地位稳固.最近在云计算.大数据.无结构数据持续化.函数式反 ...

  5. Spring对象类型——单例和多例

    由于看淘淘商城的项目,涉及到了项目中处理spring中bean对象的两种类型,分别是单例和多例,就在此记录一下,方便加深理解,写出更加健壮的代码. 一.单例和多例的概述 在Spring中,bean可以 ...

  6. c语言中的 strcpy和strncpy字符串函数使用介绍

    1.strcpy函数 函数原型:char *strcpy(char *dst,char const *src)            必须保证dst字符的空间足以保存src字符,否则多余的字符仍然被复 ...

  7. CSU 2018年12月月赛 H(2220): Godsend

    Description Leha somehow found an array consisting of n integers. Looking at it, he came up with a t ...

  8. ubuntu server 12.04.4安装配置

    这里讲

  9. Python之函数作业

    Python之函数作业 爬页面 #爬虫页面,send一次爬一次 from urllib.request import urlopen def get(): while True: url = yiel ...

  10. 第一节:python提取PDF文档中的图片

    由于项目需要将PDF文档当中的图片转换成图片,所以参考了这篇文章https://blog.csdn.net/qq_15969343/article/details/81673302后项目得以解决. 1 ...