[paramiko] Broken listinfo link
Jon Sabo
jonathan.sabo at gmail.com
Thu Feb 8 10:23:37 PST 2007
Sweet it is working! I was on #bzr earlier asking to have the link on the
paramiko site fixed too. I had a question about executing commands on
remote servers and how to receive the output from them. I want to make a
script that will allow you to specify the username and password and a sudo
user an password so you can execute commands remotely. Another thing i want
to do is to use it with pexpect to connect to these terminal servers I have
and tell me what hosts they are really connected to.... the sh hosts output
is all wrong and I want to automate generating t he host map. Anyway.
This is what I have so far. Just a modified demo.py: I'm by no means great
at python yet so suggestions and help is definitely wanted...
Its here: http://www.illfunction.com/~illsci/code/pssh.py
And here:
>>> (01:26 PM) (jsabo at frostedflakes) (0/pts/0) (~/Projects/pssh)
$ cat pssh.py
#!/usr/bin/env python
# This is just a modified demo.py from the paramiko module examples
# Yay no nsh!
import base64
from binascii import hexlify
from optparse import OptionParser
import getpass
import os
import select
import socket
import sys
import threading
import time
import traceback
import paramiko
import interactive
import pexpect
def agent_auth(transport, username):
"""
Attempt to authenticate to the given transport using any of the private
keys available from an SSH agent.
"""
agent = paramiko.Agent()
agent_keys = agent.get_keys()
if len(agent_keys) == 0:
return
for key in agent_keys:
print 'Trying SSH agent key: %s' % hexlify(key.get_fingerprint()),
try:
transport.auth_publickey(username, key)
return
except paramiko.SSHException:
print 'SSH agent key: %s Failed!' % hexlify(key.get_fingerprint
())
def manual_auth(transport, username, password):
"""
Attempt to authenticate to the given transport using passwd
authentication
"""
try:
transport.auth_password(username, password)
except:
print "Password Authentication failed"
sys.exit(1)
def ssh_connect(hostname, port):
"""
Connect to remote server and return socket.
"""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((hostname, port))
except Exception, e:
print 'Connection failed: ' + str(e)
traceback.print_exc()
sys.exit(1)
return sock
def get_transport(socket):
"""
Get transport.
"""
try:
transport = paramiko.Transport(socket)
transport.start_client()
except paramiko.SSHException:
print 'Secure Shell negotiation failed.'
traceback.print_exc()
sys.exit(1)
return transport
def check_hostkey(transport, hostname, known_keys):
"""
Check server's host key -- this is important.
"""
remote_key = transport.get_remote_server_key()
if not known_keys.has_key(hostname):
print 'WARNING: Unknown host key!'
elif not known_keys[hostname].has_key(remote_key.get_name()):
print 'WARNING: Unknown host key!'
elif known_keys[hostname][remote_key.get_name()] != remote_key:
print 'WARNING: Host key has changed!!!'
sys.exit(1)
else:
print 'Host key OK.'
def get_knownhosts_keys():
"""
Read in your known hosts file and get the keys.
"""
try:
known_keys = paramiko.util.load_host_keys(os.path.expanduser
('~/.ssh/known_hosts'))
except IOError:
print 'Unable to open host keys file'
known_keys = {}
return known_keys
def main():
# Remote Auth info
hostname = "myhostname"
port = int("22")
username = "myuser"
passwd = "apasswd"
sudo_user = "root"
logfile = "pssh.log"
command = "touch boh.txt"
#passwd = getpass.getpass('Password for %s@%s: ' % (username,
hostname))
# Turn on logging
paramiko.util.log_to_file(logfile, level=1)
mysock = ssh_connect(hostname, port)
t = get_transport(mysock)
known_keys = get_knownhosts_keys()
check_hostkey(t, hostname, known_keys)
agent_auth(t, username)
if not t.is_authenticated():
manual_auth(t, username, passwd)
if not t.is_authenticated():
print 'Authentication Failed.'
t.close()
sys.exit(1)
chan = t.open_session()
chan.exec_command(command)
chan.close()
t.close()
if __name__ == "__main__":
main()
On 2/8/07, Wouter van Heyst <larstiq at larstiq.dyndns.org> wrote:
>
> Hi all,
>
> On lag.net/paramiko there is a link to this mailinglist under the
> documentation header, but it seems broken.
> http://www.lag.net/mailman/listinfo/paramiko
>
> I'm mailing this to the list also to see if the actualy list itself is
> still working.
>
> Wouter van Heyst
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.6 (GNU/Linux)
>
> iQCVAwUBRctYOenTGucjhaP+AQIEYQP/aRsq6AvLC8fYN77ItMOB56gVbVWUaatA
> hCUyPzwjBKOwaWWqnw8h4XOozdK4xL4FDfEEajrfeTfdpCpgNan/FHW7ai24Vw9S
> nBB4baGjAZlm4rwWuAuRp/azr1v/6XkDBVAvLpM6KpqEmV9bSr9iB6GsbOU93Ixd
> 51+wXksFAqg=
> =x37y
> -----END PGP SIGNATURE-----
>
> _______________________________________________
> paramiko mailing list
> paramiko at lag.net
> http://www.lag.net/mailman/listinfo/paramiko
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.lag.net/pipermail/paramiko/attachments/20070208/45d68aa6/attachment.htm
More information about the paramiko
mailing list