Re: Q: paramiko/SSH/ how to get a remote host_key

2008-01-30 Thread Charles_hans

I tried to get what host_key has been aquired after AutoPolicy is set. I
added the following code just before client.close() in rosty's final code:

try:
host_keys =
paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
except IOError:
try:
host_keys =
paramiko.util.load_host_keys(os.path.expanduser('~/ssh/known_hosts'))
except IOError:
print '*** Unable to open host keys file'

I still got 'Unable to open host keys file'. Can you tell me how to get the
remote host_key under this situation? Thanks!

Charles
1/30/2008

by Guilherme Polo Jan 21, 2008; 09:08am :

2008/1/21, DHR <[EMAIL PROTECTED]>:

Very nice =)

Just an advice, you dont need to import base64. Method decode of
strings allows you to specify encoding as 'base64' to perform needed
operations.


by rosty Jan 21, 2008; 08:43am :

Thank you! Now it works and the code looks like this: 

import paramiko 
import base64 
from paramiko import AutoAddPolicy, SSHClient 

client = paramiko.SSHClient() 
client.set_missing_host_key_policy(AutoAddPolicy()) 
client.connect('hostIP', username='uname', password='pass') 
stdin, stdout, stderr = client.exec_command('ls') 
for line in stdout: 
print '... ' + line.strip('\n') 

client.close() 
-- 
View this message in context: 
http://www.nabble.com/Q%3A-paramiko-SSH--how-to-get-a-remote-host_key-tp14996119p15189222.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Q: paramiko/SSH/ how to get a remote host_key

2008-01-30 Thread Charles_hans

Thank you, Guilherme. I was running demo_sftp.py included in paramiko
download.

It seems that '.ssh/known_hosts' should be the path of a key file on my
working directory on local PC. (Right?) I replaced this with 'test_rsa.key'
in C:\paramiko-1.7.2\demos and this did not generate error. But the returned
host_keys is empty. I traced the code to 'hostkeys.py' and found that the
line

   e = HostKeyEntry.from_line(line)

always retuned None. This means that my remote host name should have been in
this key file. (Right?)

I am very new to paramiko. How to create such a key file (with my remote
host name)? Should I also load this key file into the remote server? Please
advise. Thanks!

Charles
1/30


2008/1/30, Charles_hans <[EMAIL PROTECTED]>:
>
> I tried to get what host_key has been aquired after AutoPolicy is set. I
> added the following code just before client.close() in rosty's final code:
>
> try:
> host_keys =
> paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
> except IOError:
> try:
> host_keys =
> paramiko.util.load_host_keys(os.path.expanduser('~/ssh/known_hosts'))
> except IOError:
> print '*** Unable to open host keys file'
>
> I still got 'Unable to open host keys file'. Can you tell me how to get
> the
> remote host_key under this situation? Thanks!
>
> Charles
> 1/30/2008

Hey Charles,

If you take a look on your code, you will see that you are catching
IOError. So the problem you are noticing is related to I/O failing
such as non-existent file. Be sure to check if '~/.ssh/known_hosts'
exists, if the first try fails, check if "~/ssh/known_hosts" exists
then (since you are trying to access that file).

Cheers,

> by rosty Jan 21, 2008; 08:43am :
>
> Thank you! Now it works and the code looks like this:
>
> import paramiko
> import base64
> from paramiko import AutoAddPolicy, SSHClient
>
> client = paramiko.SSHClient()
> client.set_missing_host_key_policy(AutoAddPolicy())
> client.connect('hostIP', username='uname', password='pass')
> stdin, stdout, stderr = client.exec_command('ls')
> for line in stdout:
> print '... ' + line.strip('\n')
>
> client.close()
-- 
View this message in context: 
http://www.nabble.com/Q%3A-paramiko-SSH--how-to-get-a-remote-host_key-tp14996119p15192764.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


Why chdir command doesn't work with client.get_transport() ?

2008-02-02 Thread Charles_hans

I am new to paramiko. I wrote a script to copy files between Solaris and XP
machines as below:

import paramiko
def exec_command(trans, command):
chan = trans.open_session()
chan.exec_command(command)
stdin = chan.makefile('wb')
stdout = chan.makefile('rb')
stderr = chan.makefile_stderr('rb')
return stdin, stdout, stderr
def main():
client = paramiko.SSHClient()
client.connect(hostname, username=username, password=password)
trans = client.get_transport()
dummy_chan = trans.open_session()
exec_command(trans, 'cd temp')
stdin, stdout, stderr = exec_command(pwd)

>From the last line I found from stdout that the pwd is not changed. I tried
to type the full path in 'cd', did not help. Other UNIX commands such as ls,
mkdir, cp, rm will work. Only this 'cd' never works. I got stuck here.

Please help. Thanks!

Charles
2/2
-- 
View this message in context: 
http://www.nabble.com/Why-chdir-command-doesn%27t-work-with-client.get_transport%28%29---tp15248798p15248798.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why chdir command doesn't work with client.get_transport() ?

2008-02-03 Thread Charles_hans

Thank you, Martin! I think that you are right.
But I can't use rsh since I am on XP to send commands to UNIX. I used telnet
before. Now I am converting to ssh/sftp, which is my purpose.

I put some more efforts in the following code:

t = paramiko.Transport((hostname, port))
t.connect(username=username, password=password, hostkey=None)
sftp = t.open_sftp_client()
sftp.chdir('temp')
pwd = sftp.getcwd()   # Successful
s = t.open_session()
s.exec_command('ls')
stdout = s.makefile('rb')
for line in stdout:
print '... ' + line.strip('\n') # Find: the default
directory shown

This means the channel s created above has nothing to do with the sftp
client which successfully changed the pwd. Any idea that we can link s and
sftp together?  Thanks!

I can't find something equivalent to Telnet in paramiko.

Charles
2/3/2008



-- 
View this message in context: 
http://www.nabble.com/Why-chdir-command-doesn%27t-work-with-client.get_transport%28%29---tp15248798p15256088.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why chdir command doesn't work with client.get_transport() ?

2008-02-05 Thread Charles_hans

Thank you, Matt, for your valuable advice! I did try using ';' to issue three
commands at once and it works!

However, I have more problems with issuing ClearCase commands, which is what
I am really doing.

Under telnet, I could issue commands one by one, just as typing at the
command prompt. Now I tried to use ';' manually typing two commands at once
on my Solaris command line, such as 

 ct setview viewName; cd dir_in_clearcase

(ct = cleartool) The second command will not be executed, since the command
prompt changes after the first command. --- I think that it was because
there was a long delay after the first command. (any way out?)

When I used exec_command() to do this in Python, even the first command was
not recognized (error: bad phone number. I used 'man ct' and found that ct
dials a phone number). Even if 'ct' was recognized, the second command would
not be executed. And it is useless to do it in the next exec_command()
function. (Without the first command, one can not cd into a directory in
ClearCase.)

Do you think that paramiko can replace telnet in my application? Thanks.

Charles
2/5


Matthew_WARREN wrote:
> 
> 
> As other have said, it's because exec_command uses a new session each
> time.
> 
> You may get some joy with this, untested
> 
> exec_command('cd /some/where; somecommand')
> 
> uses the semi-colon to separate multiple commands on one command line.
> 
> 
> Matt.
> 
> 
> 
> 
>   
>
>Internet   
>
>[EMAIL PROTECTED]  
>   
>   
>
> To 
>
> python-list  
>Sent by:   
>
> cc 
>python-list-bounces+matthew.warren=uk.bnpparibas.com@  
>
>python.org 
>   
> Subject 
>
> Why chdir command doesn't work with  
>02/02/2008 23:41
> client.get_transport() ? 
>   
>
>   
>
>   
>
>   
>
>   
>
>   
>
>   
>
> 
> 
> 
> 
> 
> I am new to paramiko. I wrote a script to copy files between Solaris and
> XP
> machines as below:
> 
> import paramiko
> def exec_command(trans, command):
> chan = trans.open_session()
> chan.exec_command(command)
> stdin = chan.makefile('wb')
> stdout = chan.makefile('rb')
> stderr = chan.makefile_stderr('rb')
> return stdin, stdout, stderr
> def main():
> client = paramiko.SSHClient()
> client.connect(hostname, username=username, password=password)
> trans = client.get_transport()
> dummy_chan = trans.open_session()
> exec_command(trans, 'cd temp')
> stdin, stdout, stderr = exec_command(pwd)
> 
>>From the last line I found from stdout that the pwd is not changed. I
> tried
> to type the full path in 'cd', did not help. Other UNIX commands such as
> ls,
> mkdir, cp, rm will work. Only this 'cd' never works. I got stuck here.
> 
> Please help. Thanks!
> 
> Charles
> 2/2
> --
> View this message in context:
> http://www.nabble.com/Why-chdir-command-doesn%27t-work-with-client.get_transport%28%29---tp15248798p15248798.html
> 
> Sent from the Python - python-list mailing list archive at Nabble.com.
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
> 
> 
> 
> This message and any atta

Re: Why chdir command doesn't work with client.get_transport() ?

2008-02-06 Thread Charles_hans

Thank you, Matt, for your valuable advice!

My application is converting (to sftp/ssh) a script which used ftp/telnet to
load/copy/zip files with labels to/from a ClearCase server. ClearCase is a
version control software similar to MS Source Safe or PVCS. The command 'ct
setview aViewName' is just one of my commands which worked fine using
telnet. If you don't type this command and type 'cd aDirNameInClearCase', it
won't work. After typing 'ct ...', the prompt will be changed and the 'cd
...' command will work.

Now my problem has been solved. I used the class channel's get_pty(),
invoke_shell() and sendall() functions. Basically I used an interactive
shell, so that I could issue commands one by one like using telnet.

Charles
2/6
-- 
View this message in context: 
http://www.nabble.com/Why-chdir-command-doesn%27t-work-with-client.get_transport%28%29---tp15248798p15312652.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list