Python paramiko notes
Paramiko is a Python ssh package. The following is an example that makes use of my ssh config file, creates a ssh client, runs a command on a remote server, and reads a remote file using sftp. Paramiko is released under the GNU LGPL
Install paramiko
- Install pip
- Install paramiko
sudo pip install paramiko
Example
from paramiko import SSHClient, SSHConfig
# ssh config file
config = SSHConfig()
config.parse(open('/home/saltycrane/.ssh/config'))
o = config.lookup('testapa')
# ssh client
ssh_client = SSHClient()
ssh_client.load_system_host_keys()
ssh_client.connect(o['hostname'], username=o['user'], key_filename=o['identityfile'])
# run a command
print "\nRun a command"
cmd = 'ps aux'
stdin, stdout, stderr = ssh_client.exec_command(cmd)
for i, line in enumerate(stdout):
line = line.rstrip()
print "%d: %s" % (i, line)
if i >= 9:
break
# open a remote file
print "\nOpen a remote file"
sftp_client = ssh_client.open_sftp()
sftp_file = sftp_client.open('/var/log/messages')
for i, line in enumerate(sftp_file):
print "%d: %s" % (i, line[:15])
if i >= 9:
break
sftp_file.close()
sftp_client.close()
# close ssh client
ssh_client.close()
Results:
Run a command 0: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND 1: root 1 0.0 0.0 1920 536 ? S 2009 0:00 /sbin/init 2: root 2 0.0 0.0 0 0 ? S 2009 0:00 [migration/0] 3: root 3 0.0 0.0 0 0 ? SN 2009 0:00 [ksoftirqd/0] 4: root 4 0.0 0.0 0 0 ? S 2009 0:00 [watchdog/0] 5: root 5 0.0 0.0 0 0 ? S< 2009 0:00 [events/0] 6: root 6 0.0 0.0 0 0 ? S< 2009 0:00 [khelper] 7: root 7 0.0 0.0 0 0 ? S< 2009 0:00 [kthread] 8: root 8 0.0 0.0 0 0 ? S< 2009 0:00 [xenwatch] 9: root 9 0.0 0.0 0 0 ? S< 2009 0:00 [xenbus] Open a remote file 0: Feb 21 06:47:03 1: Feb 21 07:14:03 2: Feb 21 07:34:03 3: Feb 21 07:54:04 4: Feb 21 08:14:04 5: Feb 21 08:34:05 6: Feb 21 08:54:05 7: Feb 21 09:14:05 8: Feb 21 09:34:06 9: Feb 21 09:54:06
See also
Post a comment
About
I'm Eliot and this is my notepad for programming topics such as Python, Django, Ubuntu, Emacs, etc... more »
Search Blog
Tags
-
algorithms
(4)
-
aws
(8)
-
blogproject
(20)
-
c_cplusplus
(12)
-
cardstore
(8)
-
colinux
(2)
-
concurrency
(9)
-
conkeror
(2)
-
cygwin
(18)
-
datastructures
(15)
-
datetime
(3)
-
dell
(3)
-
django
(39)
-
emacs
(20)
-
files_directories
(10)
-
install_setup
(7)
-
javascript
(3)
-
keyboard
(6)
-
matplotlib
(5)
-
mercurial
(4)
-
nginx
(2)
-
preferences
(8)
-
processes
(3)
-
pyqt
(18)
-
python
(122)
-
ratpoison
(3)
-
regexes
(5)
-
rsync
(3)
-
softwaretools
(17)
-
sql
(13)
-
ssh
(7)
-
subversion
(6)
-
twisted
(6)
-
ubuntu
(60)
-
urxvt
(5)
-
vxworks
(25)
-
webservices
(4)
-
wmii
(7)
Blogroll
- Adam Gomaa
- Alex Clemesha
- Amir Salihefendic
- Armin Ronacher
- David Beazley
- David Ziegler
- Duncan McGreggor
- Gareth Rushgrave
- Glyph Lefkowitz
- Guido van Rossum
- Ian Bicking
- Jacob Kaplan-Moss
- James Bennett
- James Tauber
- Jesper Noehr
- Matt Harrison
- Nikolay Kolev
- Parand Darugar
- Peter Baumgartner
- Peter Bengtsson
- Rob Hudson
- Simon Willison
- Will McGugan