paramiko_su_root
#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的更多相关文章
随机推荐
- 经常遇到的js兼容问题大总结----最全总结
001.获取滚动条滚动的距离 var sTop = document.documentElement.scrollTop || document.body.scrollTop 002.获取非行间样式 ...
- caffe LOG LOG_IF
caffe使用了glog,在caffe的solver中输出都是用的LOG和LOG_IF LOG_IF是条件输出: LOG_IF(INFO, num_cookies > ) << &q ...
- _bbox_pred函数
fast中的_bbox_pred函数和faster中的bbox_transform_inv是一样的,是将框进行4个坐标变换得到新的框坐标.fast中是将selective search生成的框坐标进行 ...
- vue工程化之公有CSS、JS文件
1.关于公共的css 在src下面新建public.css,然后在main.js中引入进来 import '@/public.css',这样所有页面中都会使用这个样式了,如果只是部分页面需要,那还是不 ...
- zabbix4.2学习笔记--zabbix安装
环境 系统信息 发行版 版本 ip 关系 主机名 centos 7.5 192.168.181.135 服务端 server centos 7.5 192.168.181.136 客户端 client ...
- pycharm acejumpchar插件
1链接:https://plugins.jetbrains.com/plugin/7163-emacsideas 2将其pycharm路径下:\.PyCharm2017.1\config\plugin ...
- python Matplotlib 系列教程(三)——绘制直方图和条形图
在本章节我们将学习如何绘制条形图和直方图 条形图与直方图的区别:首先,条形图是用条形的长度表示各类别频数的多少,其宽度(表示类别)则是固定的: 直方图是用面积表示各组频数的多少,矩形的高度表示每一组的 ...
- Core Animation教程
http://dev.wo.com.cn/bbs/viewthread.jsp?tid=141767&page=1 http://blog.csdn.net/lvxiangan/article ...
- POJ 1149 PIGS 建图,最大流
题意: 你m个猪圈以及每个猪圈里原来有多少头猪,先后给你n个人,每个人能打开某一些猪圈并且他们最多想买Ki头猪,在每一个人买完后能将打开的猪圈中的猪顺意分配在这次打开猪圈里,在下一个人来之前 已打开的 ...
- 使用TensorRT加速yolo3
一.TensorRT支持的模型: TensorRT 直接支持的model有ONNX.Caffe.TensorFlow,其他常见model建议先转化成ONNX.总结如下: 1 ONNX(.onnx) 2 ...