python服务器登录_免密码登录服务器python脚本

标签:

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

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

标签:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值