自动配置ssh公私钥
来源:互联网 发布:暂七师军乐队 知乎 编辑:程序博客网 时间:2024/04/20 20:27
有些程序中,常需要ssh连接到远程主机进行操作。本程序通过pexpect模块,与命令行进行交互。通过输入远程主机的ip,用户名与密码,自动完成ssh公私钥的配置,下次即可直接登录,不想要输入密码。
# -*- coding: utf-8 -*-import pexpectimport osimport timeimport getpasslogFileId = open("logfile.txt", 'w+')def ssh_detection(ip, user, passwd): try: ssh = pexpect.spawn('ssh '+user+'@'+ip+" echo hello") ssh.logfile = logFileId index = ssh.expect(['password: ', 'continue connecting (yes/no)?', 'hello']) if index == 0 : ssh.sendline(passwd) index = ssh.expect('hello') if index == 0: print "未配置ssh公私钥" ssh.close(force=True) return 2 elif index == 1: ssh.sendline('yes\n') index = ssh.expect('password: ') print "未配置ssh公私钥" ssh.close(force=True) return 2 elif index == 2: print "已配置ssh公私钥" ssh.close(force=True) return 1 except: print "ERROR" return 0def ssh_configure(ip, user, passwd): print "开始配置公私钥" path = os.getcwd() if path.split('/')[1] == 'home': rsa_path = '/home/'+path.split('/')[2]+'/.ssh/id_rsa.pub' else: name = raw_input("请输入当前主机的用户名:") rsa_path = '/home/'+name+'/.ssh/id_rsa.pub' if not os.path.exists(rsa_path): try: keygen = pexpect.spawn("ssh-keygen") keygen.logfile = logFileId index = keygen.expect("Enter file in which to save the key") keygen.sendline('') index = keygen.expect("Enter passphrase ") keygen.sendline('') index = keygen.expect("Enter same passphrase again") keygen.sendline('') time.sleep(1) keygen.close(force=True) except: print "ERROR ssh-keygen" else: pass try: ssh = pexpect.spawn("ssh-copy-id -i "+rsa_path+" "+user+"@"+ip) ssh.logfile = logFileId time.sleep(10) index = ssh.expect("password") ssh.sendline(passwd) time.sleep(1) ssh.close(force=True) except: print "ERROR ssh-copy-id" try: ssh = pexpect.spawn('ssh '+user+'@'+ip+' echo hello') ssh.logfile = logFileId index = ssh.expect(['password', 'hello']) if index == 0: # ssh.close(force=True) pexpect.run("eval 'ssh-agent -s'") pexpect.run("ssh-add") ssh.close(force=True) ssh = pexpect.spawn('ssh '+user+'@'+ip+" echo hello") ssh.logfile = logFileId index = ssh.expect('hello') if index == 0: print "ssh公私钥配置成功" return True elif index == 1: print "ssh公私钥配置成功" return True except: print "配置失败" return Falseif __name__=='__main__': ip = raw_input("请输入远程ssh连接的IP 地址:") user = raw_input("登录名:") passwd = getpass.getpass("登录密码:") index = ssh_detection(ip,user,passwd) if index == 0: print "ssh连接有问题,请检查用户名密码输入是否正确,以及远程主机是否允许ssh连接" exit() elif index == 2: index = ssh_configure(ip,user,passwd) if not index: print "ssh公私钥配置失败,请尝试手动配置" exit() print ".......ssh检查pass"
0 0
- 自动配置ssh公私钥
- ssh公私钥配置
- 配置SSH公私钥免密码登陆
- CentOS 配置SSH免密码登陆(公私钥登陆)
- ubuntu中ssh, git公私钥问题
- ssh公私钥登录方式设置
- Linux/Windows ssh公私钥访问
- 使用ssh-keygen生成公私钥
- iOS 支付宝支付公私钥配置
- ssh公私钥正确的情况下免密登录失败原因
- ssh-keygen生成公私钥免密码登录远程服务器
- Linux系统用ssh-keygen生成公私钥
- 配置SSH自动登录
- SSH配置 自动
- 生成RSA公私钥
- RSA公私钥结构
- 公私钥加密机制
- 公私钥加解密
- hdu 1085
- Android 开发,关于依赖库的制作,打包aar,及使用过程
- ZigBee_2530初学笔记
- pyspark + mongodb
- 学习运维——虚拟机管理
- 自动配置ssh公私钥
- RecyclerView加载不同类型的布局
- Oracle数据库的归档模式与非归档模式
- spring+mybatis+spring-mvc
- 廖雪峰的Python教程学习笔记一Python简介和基础语法
- FCM(Fuzzy C-Means)模糊C聚类
- Android Studio导入项目的遇到的两个问题
- 如何在Eclipse下查看JDK源代码以及java源代码阅读方法
- 使用VMware安装CentOS7详请