在自动化运维平台没有做完之前,常需要登录服务器做很多维护操作,每次找好长好长的密码,那么多服务器,你会疯掉的,所以瞎搞了以下脚本.先解一下燃眉之急,哈哈

cat login_root.exp

#!/usr/bin/expect -c

set IP [lindex $argv 0]
set PWD [lindex $argv 1]
set timeout 2
spawn ssh root@$IP
expect "*yes/no*" {send "yes\r"}
expect "*assword*" {send "$PWD\r";interact}

用下面脚本调用 /opt/.script/login_root.exp 脚本,登录你需要登录的服务器

cat  login_remote.py
#!/usr/bin/env python
#author:wangqiankun@lashou-inc.com
#name:login_remote.py import re,time
import sys
import commands
import subprocess def login_main(ip,pwd):
if ip != "" and pwd != "":
print "\033[32;1m start login at:%s\033[0m" %time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
p = subprocess.call("expect /opt/.script/troot.exp %s '%s'" %(ip,pwd),shell=True) else:
print "\033[32;1m IP or the password is wrong, please handle \033[0m" def re_ip(ipinfo):
with open('/root/.script/password.txt','r') as f:
for ipline in f.readlines():
hostinfo = re.match(r'^192.168.%s .*' %ipinfo,ipline,re.M|re.I)
if hostinfo:
print "\033[32;1m You are going to the login server IP is:%s\033[0m" %hostinfo.group().split()[0]
return hostinfo.group().split()[0],hostinfo.group().split()[1] if __name__ == "__main__":
try:
ipinfo = sys.argv[1].strip()
if ipinfo != "":
hostinfo = re_ip(ipinfo)
try:
login_main(hostinfo[0],hostinfo[1])
except TypeError:
print "\033[31;1mThere is no matching to the correct IP address \033[0m"
except IndexError:
print "\033[31;1m You do not need to log in to the IP information input\033[0m"
#print ipinfo,

测试:

  python login_remote.py 10.100

免密码登录服务器python脚本的更多相关文章

  1. ssh 免密码登录服务器

    本机生成 ssh key ssh-keygen -t rsa -C "your_email@example.com" 上传公钥文件(假设用户为 user,服务器 ip 为 1.2. ...

  2. [转] ssh免密码登录服务器

    1. tencent 同事写的比较靠谱 https://www.chenyudong.com/archives/ssh-using-private-public-key-no-password.htm ...

  3. jenkins发布项目到远程主机上,配置linux使用SSH免密码登录

    一.首先要配置两台linux如何使用SSH免密码登录,这样脚本执行scp命令以及远程执行脚本都不需要输入密码: A为本地主机(即用于控制其他主机的机器,jenkins服务器) ; B为远程主机(即被控 ...

  4. Linux 配置ssh 免密码登录

    在平常应用中,我们经常会登录到其他主机,比如说服务器,每次都需要用户名和密码. 我们可以通过ssh免密码登录服务器而不需要输入密码. 现在有一台ubuntu的阿里云服务器,称之为 server.  公 ...

  5. CentOS 配置集群机器之间SSH免密码登录

    SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定:SSH 为建立在应用层和传输层基础上的安全协议.SSH 是目前较可靠,专为远 ...

  6. python实现免密码登录lunx服务器

    import paramikoimport oshostname='192.168.76.10'username='root'# password='123456'ssh=paramiko.SSHCl ...

  7. Linux多台服务器间SSH免密码登录配置

    SSH实现各个服务器间的文件相互备份,如运行scp命令,可以实现免密码登录,从而可以使用SHELL脚本实现一些自动化的处理. 假如A机要免密码登录B机,具体方法如下: 1.在A机运行:"ss ...

  8. 多台CentOS服务器下实现SSH免密码登录

    ROOT用户下实现SSH免密码登录 第一步:进入目录/root/.ssh $ cd  /root/.ssh/ 执行以下命令,会在当前目录下生成公钥(id_rsa.pub)/私钥(id_rsa)对 第二 ...

  9. Centos 解决SSH 免密码登录 以及Crontab制作定时SSH自动登录和关闭的脚本

    一.SSH免密码登录 假设要登录的机器为192.168.1.100,当前登录的机器为192.168.1.101. 首先在101的机器上生成密钥(如果已经生成可以跳过): $ ssh-keygen -t ...

随机推荐

  1. sysbench 环境安装,压测mysql

    源码路径:https://github.com/akopytov/sysbench 版本linux 6.8sysbench 0.5mysql 5.6.29 1.安装pip略 2.pip 安装bzr p ...

  2. ACM解题之在线翻译 Give Me the Number

    Give Me the Number Time Limit: 2 Seconds                                     Memory Limit: 65536 KB  ...

  3. linux mysql表名大小写

    1.用ROOT登录,修改/etc/my.cnf 2.在[mysqld]下加入一行:lower_case_table_names=1 0:区分大小写,1:不区分大小写 3.重新启动数据库即可

  4. java 基础 --Collection(Map)

    Map是不是集合?哈哈哈 java编程思想>的第11章,第216页,正数第13行,中原文:“……其中基本的类型是LIst.Set.Queue和Map.这些对象类型也称为集合类,但由于Java类库 ...

  5. 【bzoj4428】[Nwerc2015]Debugging调试 数论+记忆化搜索

    题目描述 一个 $n$ 行的代码出了bug,每行都可能会产生这个bug.你要通过输出调试,在其中加入printf来判断bug出现的位置.运行一次程序的时间为 $r$ ,加入一条printf的时间为 $ ...

  6. BZOJ5091 摘苹果(概率期望)

    大胆猜想每一步都相当于是第一步.稍微验证一下发现是对的.就做完了. #include<iostream> #include<cstdio> #include<cmath& ...

  7. IP组播技术

      1  概述 1.1  产生背景 传统的IP通信有两种方式:一种是在源主机与目的主机之间点对点的通信,即单播:另一种是在源主机与同一网段中所有其它主机之间点对多点的通信,即广播.如果要将信息发送给多 ...

  8. 洛谷 P1344 [USACO4.4]追查坏牛奶Pollutant Control

    题目描述 你第一天接手三鹿牛奶公司就发生了一件倒霉的事情:公司不小心发送了一批有三聚氰胺的牛奶.很不幸,你发现这件事的时候,有三聚氰胺的牛奶已经进入了送货网.这个送货网很大,而且关系复杂.你知道这批牛 ...

  9. pbuilder编译构建工具分析

    1. 简介 pbuilder(personal Debian package builder)是ubuntu环境下维护debian包的专业工具,能够为每个deb包创建纯净的编译构建环境,自动解析和安装 ...

  10. 在 Android开发中,性能优化策略十分重要

    在 Android开发中,性能优化策略十分重要本文主要讲解性能优化中的布局优化,希望你们会喜欢.目录 示意图 1. 影响的性能 布局性能的好坏 主要影响 :Android应用中的页面显示速度 2. 如 ...