加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ssh.py 1.09 KB
一键复制 编辑 原始数据 按行查看 历史
D60 提交于 2016-12-05 18:07 . add source code
# encoding: UTF-8
import pexpect
PROMPT = ['#','>>>', '>','\$']
def send_command(child, cmd):
child.sendline(cmd)
child.expect(PROMPT)
print(child.before)
def connect(user, host, password):
ssh_newkey = 'Are you sure you want to continue connecting'
connStr = 'ssh '+user+'@'+host
child = pexpect.spawn(connStr)
try:
ret = child.expect([pexpect.TIMEOUT, ssh_newkey, '%s@%s\'s [P|p]assword:' % (user,host)])
except:
print("[-] Error Connecting")
exit(0)
if ret == 0:
print('[-] Error Connecting')
return
if ret == 1:
child.sendline('yes')
ret = child.expect([pexpect.TIMEOUT, '[P|p]assword:'])
if ret== 0:
print('[-] Error Connecting')
return
child.sendline(password)
child.expect(PROMPT)
if ret==0:
print('[-] Error Connecting, maybe password error')
exit(0)
return child
def main():
host = '123.57.145.149'
user = 'root'
password='JEjgOYIlGSX7'
child = connect(user,host,password)
send_command(child,'ls -la')
if __name__ == '__main__':
main()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化