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的更多相关文章
随机推荐
- Python学习-列表的其它主要操作
列表的其它主要操作 还记得之前使用del语句去清除一个列表中的所有内容,那么会因为把列表全部清空,所以输出会报错.可以使用clear() 完成 clear(self):可以将一个列表变成空列表 lis ...
- 11.Spring通过工厂方法配置Bean
通过工厂方法配置Bean暴扣静态工厂方法和实例工厂方法. 1.静态工厂方法 调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中,当客户端需要对象时,只需要简单的调用静态方法,而不去关心创建 ...
- tomcat idea 报权限错误
出现的错误提示如下: 下午9:11:27 All files are up-to-date下午9:11:27 All files are up-to-date下午9:11:27 Error runni ...
- STM32——通用定时器基本定时功能
STM32——————通用定时器基本定时功能 1. ...
- CSU1160
十进制-十六进制 Time Limit: 1 Sec Memory Limit: 128 MB Description 把十进制整数转换为十六进制,格式为0x开头,10~15由大写字母A~F表示. ...
- 集训第六周 O题
Description Gerald got a very curious hexagon for his birthday. The boy found out that all the angle ...
- 一种RC滤波电路的验证
在做电源的时候,在开关管的D极经常是出现不想看到的尖峰脉冲.以CCFL推挽式缓冲电路为准,我与一个同学杨进行了相应的验证. 其中的出来的现象和反思如下: 1,加上电阻和电容串联的滤波的确能将尖峰脉冲消 ...
- About SQL Server 2016 CPT2
SQL Server 2016 CTP2已经发布,可以从以下主页进行下载. http://www.microsoft.com/en-us/server-cloud/products/sql-serve ...
- vim学习之旅01-文本搜索并高亮显示
step 1:在linux终端新建一个test.txt文本文档:vim test.txt; 回车后打开编辑器: step 2:进入编辑状态(键盘"i")输入一段文本,退出编辑(键盘 ...
- java连接MySQL数据库并读取内容
package sqldemo; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSe ...