FastNetMon

Thursday, 18 March 2010

Выполнение команд на удаленном сервере по SSH2 из Python /Paramiko

Ставим библиотеку Paramiko:
apt-get install -y python-paramiko



#!/usr/bin/python
# -*- coding: utf-8 -*-

import paramiko
import sys

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )

try:
ssh_connection = ssh.connect('domain.ru', username='xxxx', password='yyyyy')
except paramiko.AuthenticationException:
print "Auth failed"
ssh.close()
sys.exit(0)

( stdin, stdout, stderr ) = ssh.exec_command('cat /proc/mdstat')

for line in stdout.read().splitlines():
print line


ssh.close()



Вот пример запуска команды:

./ssh_command.py
Personalities : [raid1] [raid10] [raid0] [raid6] [raid5] [raid4]
md0 : active raid1 sdb1[1] sda1[0]
4200896 blocks [2/2] [UU]

md1 : active raid1 sdb2[1] sda2[0]
2104448 blocks [2/2] [UU]

md2 : active raid1 sdb3[1] sda3[0]
1458830400 blocks [2/2] [UU]

unused devices:



по материалам: http://jessenoller.com/2009/02/05/ssh-programming-with-paramiko-completely-different/

No comments :

Post a Comment

Note: only a member of this blog may post a comment.