Python 官方文档:入门教程 => 点击学习
下载并安装pexpect与ptyprocess模块并结合python2.7实现如下操作tar -zxvf pexpect-4.0.1.tar.gz cd pexpect-4.0.1Python setup.py installtar -zx
下载并安装pexpect与ptyprocess模块并结合python2.7实现如下操作
tar -zxvf pexpect-4.0.1.tar.gz
cd pexpect-4.0.1
Python setup.py install
tar -zxvf ptyprocess-0.5.tar.gz
cd /ptyprocess-0.5
python setup.py install
Py脚本
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pexpect
#from getpass import getpass
#passwd = getpass()
def ssh_cmd(user,ip, cmd,passwd):
ssh = pexpect.spawn('ssh %s@%s "%s"' % (user,ip,cmd))
try:
i = ssh.expect(['passWord:', 'continue connecting (yes/no)?'], timeout=5)
if i == 0 :
ssh.sendline(passwd)
elif i == 1:
ssh.sendline('yes')
ssh.expect('password: ')
ssh.sendline(passwd)
except pexpect.EOF:
print "EOF"
except pexpect.TIMEOUT:
print "TIMEOUT"
else:
r = ssh.read()
print r
ssh.close()
if __name__ == '__main__':
file=open("/mess/filelist",'r')
a = file.read()
file.close()
for host in a.split("\n"):
if host:
user,ip,cmd,passwd = host.split("::")
print "-- %s run:%s --" % (ip, cmd)
ssh_cmd(user,ip,cmd,passwd)
-------------------------------------------------------------------------------------------
/mess/filelist
root::192.168.100.100::ls -l::password
root::192.168.100.100::ps -ef|grep ssh|awk '{print $8}'::password
--结束END--
本文标题: python与pexpect实现ssh操
本文链接: https://lsjlt.com/news/183659.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0