CSDN学习社区 python 脚本实现ssh登录--expect 和 pxssh

python 脚本实现ssh登录--expect 和 pxssh

expect#!/usr/bin/env pythonimport pexpecthost="x.x.x.x"user="user"password="pwd"command="df -h"child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command))child.expect('password:')...

expect


#!/usr/bin/env python
import pexpect
host="x.x.x.x"
user="user"
password="pwd"
command="df -h"
child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command))
child.expect('password:')
child.sendline(password)
child.expect(pexpect.EOF)
print child.before

pxssh


#!/usr/bin/env python
import pxssh
import getpass
try:
    s = pxssh.pxssh()
    hostname = raw_input('hostname: ')
    username = raw_input('username: ')
    password = getpass.getpass('password: ')
    s.login (hostname, username, password)
    s.sendline ('uptime')  # run a command
    s.prompt()             # match the prompt
    print s.before         # print everything before the propt.
    s.sendline ('ls -l')
    s.prompt()
    print s.before
    s.sendline ('df')
    s.prompt()
    print s.before
    s.logout()
except pxssh.ExceptionPxssh, e:
    print "pxssh failed on login."
    print str(e)


转载于:https://blog.51cto.com/yayang/1407572

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐

  • 浏览量 445
  • 收藏 0
  • 0

所有评论(0)

查看更多评论 
已为社区贡献1280条内容