首页 > 技术文章 > 脚本之SSH登录

hanfei-1005 2016-07-26 15:52 原文

脚本之SSH登录

一)【python实现】
导入pxssh模块
常用的三个方法:
Login() 建立ssh连接
Logout() 断开连接
Prompt() 等待系统提示符,用于等待命令执行结束

Sendline() 发送命令并回车
Send() 发送命令不回车

[root@server0 318]# cat a.py 
#!/usr/bin/python
import getpass
import pxssh
try:
	s=pxssh.pxssh()
	hostname=raw_input("please input the name: ")
	username=raw_input("please inpyt the username: ")
	password=getpass.getpass('please input the password: ')
	s.login(hostname,username,password)
	s.sendline('uptime')
	s.prompt()
	print s.before
	s.sendline('ls -l')
	s.prompt()
	print s.before
	s.sendline('df')
	s.prompt()
	s.logout()
except:
	print 'failed'

推荐阅读