#!/usr/bin/env python "need to pay attention to a few questions 1, Pxssh class, exist in the Pexpect module, if you do not install this module installed on its own, if this module already exists, but there is no pxssh class, verified, That is, the version of this module is too low. Please update to the new version 2, about the command in Os.system (), this is the execution of a shell command, and itself sshpass is implemented in the execution of the SCP file is to automatically enter the account password of the peer machine, if the Sshpass is not installed, please install it yourself. "' #!/usr/bin/env pythonfrom pexpect import pxsshimport osusername=" root "passwd=" 1qaz #EDC "Try: for i in range (151,156): s = pxssh.pxssh () ipaddr= ' 192.168.100.%s ' % i #生成一个完整的IP地址 os.environ[' IP ']=str (ipaddr) #变量互换, Python variables can be obtained in the shell s.login (IPADDR,USERNAME,PASSWD) #登录主机 (IP, user, password) print ' host:%s login complete! ' % ipaddr s.sendline (' mkdir -p /root/.ssh/') #运行命令, create a hidden ssh directory os.system (' sshpass -p 1qaz#edc scp /root/.ssh/ Authorized_keys [email protected] $ip:/root/.ssh/') #下发公钥文件: Authorized_keys print ' host:%s task running ok! ' % ipaddr s.prompt () #匹配系统提示符 s.logout () #断开SSH连接except pxssh. exceptionpxssh, e: print "Pxssh failed on login" print str (e)
This article is from the "Fa&it-Q Group: 223843163" blog, please be sure to keep this source http://freshair.blog.51cto.com/8272891/1873416
Python Pxssh the implementation of batch issued SSH public key file