Python 远程批量修改密码脚本 #tar -zxvf pexpect-3.0.tar.gz #cd pexpect-3.0 #python setup.py install #!... == 1: child.sendline(oldpasswd) child.expect('#') child.sendline...('exit') except pexpect.TIMEOUT: print >>sys.stderr, ip+' timeout' except pexpect.EOF..., timeout=5) if i == 0: ssh.sendline(passwd) elif i == 1: ssh.sendline...('yes\n') ssh.expect('password:') ssh.sendline(passwd) ssh.sendline(cmd
/usr/bin/evn python import re,sys,os,pexpect from pexpect import * #coding=utf-8 #create environment.../demoCA/index.txt') #create root ca child0=pexpect.spawn('openssl req -new -x509 -keyout ca0.key -out...('test@test.com') #create root.pfx child1=pexpect.spawn('openssl pkcs12 -export -inkey ca0.key -in ca0... Create key file cmd2='openssl genrsa -des3 -out '+a+str(i)+'.key' + ' 2048' child2=pexpect.spawn...req -new -key '+ a+str(i)+'.key' + ' -out '+a+str(i)+'.csr' +' -config openssl.cnf' child3=pexpect.spawn
/pexpect-2.3.tar.gz tar xzf pexpect-2.3.tar.gz cd pexpect-2.3 sudo python ....' % (user,ip)) child.expect ('password:') child.sendline (mypassword) child.expect('$')...child.sendline('sudo -s') child.expect (':') child.sendline (mypassword) child.expect...('#') child.sendline('ls -la') child.expect('#') print child.before # Print the result...child.sendline("echo '112' >> /home/forever/1.txt ") child.interact() # Give control of the child
/usr/local/python3/bin/python3 import pexpect def exec_command(user,host,password): new_connection =...'Are you sure you want to continue connecting' conn = 'ssh '+user+'@'+host child = pexpect.spawn(conn...) #启动ssh命令 index = child.expect([pexpect.TIMEOUT,new_connection,'password:']) #等待程序输出,匹配字符串进行后续操作 if...('yes') index = child.expect([pexpect.TIMEOUT,new_connection,'password:']) #等待程序输出,匹配字符串进行后续操作 if...child.sendline('ls -l') #执行命令 child.sendline('uptime') child.sendline('exit') child.interact() #
下载并安装pexpect与ptyprocess模块并结合python2.7实现如下操作 tar -zxvf pexpect-4.0.1.tar.gz cd pexpect-4.0.1 python setup.py.../usr/bin/env python # -*- coding: utf-8 -*- import pexpect #from getpass import getpass #passwd =..., 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:
Pexpect的安装 可以使用pip安装 pip install pexpect 也可以使用easy_install easy_install pexpect 本菜鸡在win7下使用pip安装的时候抛出好多异常...ftp.expect('Password:') ftp.sendline('anonymous') ftp.sendline('pwd') ftp.interact() #执行完成后保持交互状态...,把控制权交给控制台 运行结果: pexpect组件简介 1. spawn类 spanw是pexpect的主要接口,功能就是启动和控制子应用程序。...例如: send(self,s) 发送命令,不回车 sendline(self,s=’’) 发送命令,回车 sendcontrol(self,char) 发送控制字符test.sendcontrol...password is " + password ftp.sendline('bye') ftp.close() return message
1、 安装python的Pexpect模块 wget http://jaist.dl.sourceforge.net/project/pexpect/pexpect/Release%202.3/pexpect...-2.3.tar.gz tar xzf pexpect-2.3.tar.gz cd pexpect-2.3 python setup.py install 2、 执行脚本 可以登录到机器执行一些简单的命令等..., timeout=5) if i == 0 : ssh.sendline(passwd) ...elif i == 1: ssh.sendline('yes') ssh.expect('password...: ') ssh.sendline(passwd) except pexpect.EOF: print
/usr/bin/env python # -*- coding: utf-8 -*- import pexpect import time import traceback def doRsync..., timeout=5) if i == 0 : ssh.sendline(passwd) ...elif i == 1: ssh.sendline('yes') ssh.expect('password...: ') ssh.sendline(passwd) ssh.expect(['#',pexpect.TIMEOUT,pexpect.EOF...//blog.51cto.com/749807/1566258 pexpect模块参数解释 https://www.jianshu.com/p/cfd163200d12
password'; echo 'password') | pure-pw useradd www -u www-data -d /home/ftpuser/www 方法二:Python脚本 在此我们使用了Pexpect...模块,我们可以使用pip install pexpect安装。.../usr/local/bin/python import pexpect child = pexpect.spawn('pure-pw useradd www -u www-data -d /home.../ftpuser/www'); child.expect('Password: '); child.sendline('password'); child.expect('Enter it again...: '); child.sendline('password'); child.expect(pexpect.EOF); print child.before;
Pexpect是一个纯Python模块,用于生成子应用程序;控制他们;并对输出中的预期模式作出响应。Pexpect的工作原理类似于Don Libes的Expect。...Pexpect允许脚本生成一个子应用程序,并像键入命令一样控制它。Pexpect可用于自动化交互应用程序,如ssh、ftp、passwd、telnet等。...== 0: return 0 # 发送密码 child.sendline(Password) child.expect(PROMPT..., 'password: ']) if ret == 0: # 连接超时 return 0 ret = child.sendline...== 0: return 0 # 发送密码 child.sendline(Password) child.expect(PROMPT
/usr/bin/python import pexpect foo = pexpect.spawn('passwd mqjia') foo.expect("New UNIX password:") foo.sendline...("1234567") foo.expect("Retype new UNIX password:") foo.sendline("1234567") foo.interact()
child.sendline('yes') index=child.expect([USER_REGEX, '[P|p]assword:', pexpect.EOF, pexpect.TIMEOUT...elif index == 1: child.sendline(passwd) index = child.expect([PROMPT,...child.sendline(context.passwd) context.set_state(SuccessState()) class SuccessState(SSHState...child.sendline(context.login_name) class PasswordStrategy(SSHStrategy): def handle(self, child,...child.sendline(context.passwd) class SuccessStrategy(SSHStrategy): def handle(self, child, context
来源:网络技术联盟站 本脚本使用了 pexpect 库来自动化 Telnet 登录到交换机并执行命令,然后将输出保存到文件中。...('telnet %s' % switch_info["ip"]) child.expect('login:') child.sendline("admin") child.expect('(?...i)ssword:') child.sendline(switch_info["passwd"]) child.expect(switch_info["name"]) # 执行命令并将输出写入 Excel...def execute_command(command): child.sendline(command) child.expect(switch_info["name1"])...command in commands: output = execute_command(command) worksheet.append([command, output]) child.sendline
如果没有安装easy_install,那么wget -q http://peak.telecommunity.com/dist/ez_setup.py 获取一下 python ez_setup.py pexpect...是python一个模块,可以通过:easy_install pexpect 来安装。...这里主要是用pexpect执行ssh,查看远程uptime和df -h看硬盘状况。...#ssh_cmd.py #coding:utf-8 import pexpect def ssh_cmd(ip, user, passwd, cmd): ssh = pexpect.spawn('ssh...if i == 0 : ssh.sendline(passwd) elif i == 1: ssh.sendline('yes') except pexpect.EOF: ssh.close() else
掌握这个概念之后 pexpect 的使用就很容易了。...注意: spawn() ,或者说 pexpect 并不会转译任何特殊字符 比如 | * 字符在Linux的shell中有特殊含义,但是在 pexpect 中不会转译它们,如果在 linux 系统中想使用这些符号的正确含义就必须加上...sendline() - 发送带回车符的字符串 sendline() 和 send() 唯一的区别就是在发送的字符串后面加上了回车换行符,这也使它们用在了不同的地方: 只需要发送字符就可以的话用send...() 如果发送字符后还要回车的话,就用 sendline() 它也会返回发送的字符数量 sendcontrol() - 发送控制信号 sendcontrol() 向子程序发送控制字符,比如 ctrl...if ret == 0: print('[-] Error Connecting') return if ret == 1: child.sendline
telnet很好用,但是总是有人喜欢更强大更好用的程序,于是就有了pexpect,pexpect 是 Don Libes 的 Expect 语言的一个 Python 实现,是一个用来启动子程序,并使用正则表达式对程序输出做出特定响应...看了写资料,也用pexpect写了一个小程序,实现刚才同样的功能: #!...(['login',pexpect.EOF,pexpect.TIMEOUT],timeout=1) if index == 0: child.sendline(userName) index...= child.expect('Password',timeout=1) child.sendline(password) child.expect(prompt,timeout=1)...child.sendline('ls') child.expect('ls',timeout=1) child.expect(prompt,timeout=1) print
/usr/bin/env python import time import pexpect import sys def h3c_ssh(ip,username,password1,password2...,type): try: ssh = pexpect.spawn('ssh %s@%s' %(username,ip)) i = ssh.expect(['password:'...,timeout=5) time.sleep(1) if i == 0: ssh.sendline(password1) elif i == 1: ssh.sendline('...yes') ssh.expect('password:') ssh.sendline(password1) time.sleep(1) if password2 !...= 'null': ssh.expect('>') time.sleep(1) ssh.sendline('super') ssh.sendline(password2
本脚本使用了 pexpect 库来自动化 Telnet 登录到交换机并执行命令,然后将输出保存到文件中。如果你想要将输出存储到 Excel 文件中,你可以使用 openpyxl 库来实现。...('telnet %s' % switch_info["ip"])child.expect('login:')child.sendline("admin")child.expect('(?...i)ssword:')child.sendline(switch_info["passwd"])child.expect(switch_info["name"])# 执行命令并将输出写入 Exceldef...execute_command(command): child.sendline(command) child.expect(switch_info["name1"]) result...同时,记得确保在运行脚本之前安装了所需的库:pip install pexpect openpyxl自动化与 Telnet 进行交互时,稳定性和错误处理非常重要,因为网络环境可能会导致意外情况。
pyhton 利用 pexpect 模块实现 svn chekout 一,安装 pexpect pip install pexpect 二,python 脚本实现 #!.../usr/bin/env python # -*- coding: utf-8 -*- import pexpect username = 'admin' password = 'passwd'...checkout 在当前目录 child = pexpect.spawn(svn_link) child.expect('Password for \'admin...child.sendline(password+'\n') child.expect('Store password unencrypted (yes/no)?') ...child.sendline('yes\n') pass
/usr/bin/env python import pexpect import sys host="146.11.85.xxx" user="inteuser" password="xxxx" command...="ls -l" child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command)) child.expect('password:') child.sendline...)#如果子进程结束了,你再去child.expect(pattern)会报EOF错误,模块提供了一种方法,child.expect(pexpect.EOF),不会报错,如果子进程结束了返回0 childlog.close.../usr/bin/env python import pexpect import sys host="146.11.85.xxx" user="inteuser" password="xxxx" command...="ls -l" child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command)) child.expect('password:') child.sendline
领取专属 10元无门槛券
手把手带您无忧上云