1 Star 0 Fork 0

Da yang/Socketio

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ssh.py 1.30 KB
一键复制 编辑 原始数据 按行查看 历史
Da yang 提交于 2021-03-13 19:31 +08:00 . c
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import pexpect
import getpass, os
def ssh_command(user, host, password, command):
ssh_newkey = 'Are you sure you want to continue connecting'
child = pexpect.spawn('ssh -l %s %s %s' % (user, host, command))
i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: '])
# 如果登录超时,打印出错信息,并退出.
if i == 0: # Timeout
print
'ERROR!'
print
'SSH could not login. Here is what SSH said:'
print
child.before, child.after
return None
# 如果 ssh 没有 public key,接受它.
if i == 1: # SSH does not have the public key. Just accept it.
child.sendline('yes')
child.expect('password: ')
i = child.expect([pexpect.TIMEOUT, 'password: '])
if i == 0: # Timeout
print
'SSH could not login. Here is what SSH said:'
print
child.before, child.after
return None
child.sendline(password)
return child
def main():
host = raw_input('Hostname: ')
user = raw_input('User: ')
password = getpass.getpass()
command = raw_input('Enter the command: ')
child = ssh_command(user, host, password, command)
child.expect(pexpect.EOF)
print
child.before
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/shyanglin/Socketio.git
git@gitee.com:shyanglin/Socketio.git
shyanglin
Socketio
Socketio
master

搜索帮助