Python's pxssh implementation of SSH login batch "handyman"

Source: Internet
Author: User

A simple summary

This is my production environment with a script, according to the actual operation of the situation, the white is that people are lazy, do not want to do chores. Ha ha! Do not understand the classmate can @ me Oh! Thank!


Second, the Code

#!/usr/bin/env pythonfrom pexpect import pxsshimport ostry:    s  = pxssh.pxssh ()     for i in range (64,65):      #在这个位置定义起始和结束的数字用于IP的主机号     ipaddr =  ' 192.168.1.%s '  % i      #生成一个完整的IP地址     os.environ[' IP ']=str (ipaddr)     # Variable call, Python variable can be taken in Shell     print  ' ssh connection host:%s '  %  Ipaddr        s.login (ipaddr, ' root ', ' 123456 ')     # Login host (IP, user, password)     print  ' login ok! '      #提示登录ok而已, that's all     print  ' create  (/etc/ceph/)   Directory ... '      #登录进去就得干活了, such as creating a directory     s.sendline (' mkdir -p / Etc/ceph ')     print  ' create ok! '     print  ' Issued by the configuration file ... '     os.system ( ' sshpass -p 123456 scp /etc/ceph/* [email protected] $ip:/etc/ceph ')  # This file is issued file, here with Pxssh no matter     print  ' file issued ok! '     print  ' Issued by the script '     s.sendline ( ' Mkdir -p /openstack ')   #这里也是在远程主机执行命令, the others are similar to     os.system (' Sshpass -p  123456 scp /root/osd.sh [email protected] $ip:/openstack ')      Os.system (' sshpass -p 123456 scp /root/com.txt [email protected] $ip:/openstack ')     print  ' script issued ok! '     print  ' Run the script '          s.sendline (' sh /openstack/osd.sh ')     print  ' script run ok! '     s.prompt ()     print  ' host:%s completes, exit  The ssh '  % ipaddr    s.logout ()     print  ' exit  ok! ' Except pxssh. exceptionpxssh, e:    print  "Pxssh failed on login"      print str (e)


This article is from the "it cock thread aggregation q group 223843163" blog, please be sure to keep this source http://trtan.blog.51cto.com/8272891/1866032

Python's pxssh implementation of SSH login batch "handyman"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.