I'm writting a Software to get some Information about Server
and i just want to get the python Version from an remote Server.
Here is my code:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(self.server, username=self.user, password=self.pass)
stdin, stdout, stderr = ssh.exec_command("/usr/bin/python -V")
stdin.flush()
data = stdout.readlines()
print data #just debug
ssh.close()
The print just returns "[]".
It's in stderr. Don't ask me why:
>>> import paramiko
>>> ssh = paramiko.SSHClient()
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh.connect('tek')
>>> stdin, stdout, stderr = ssh.exec_command('python --version')
>>> stdin.flush()
>>> data = stdout.readlines()
>>> data
[]
>>> data = stderr.readlines()
>>> data
['Python 2.6.6\n']
Related
I am looking at downloading a file from URL into remote machine directly.. Is this possible with python paramiko sftp?
You can copy the download script first to the remote machine and the execute it
Example:
import paramiko
host="hostname"
user="username"
#SSH Connection#
ssh = paramiko.SSHClient()
ssh.load_host_keys()
ssh.connect(host, username=user, password='password')
#Copy Script to Remote#
sftp = ssh.open_sftp()
sftp.put("localpath", "remotepath")
sftp.close()
#Close#
stdin, stdout, stderr = ssh.exec_command("python " + "remotepath" + '/downloadfile.py')
print "stderr: ", stderr.readlines()
print "pwd: ", stdout.readlines()
or you can call the wget command directly instead of using the download script.
Example:
import paramiko
host="hostname"
user="username"
#SSH Connection#
ssh = paramiko.SSHClient()
ssh.load_host_keys()
ssh.connect(host, username=user, password="password")
stdin, stdout, stderr = ssh.exec_command("cd download_folder; wget http://DOWNLOAD.URL")
print "stderr: ", stderr.readlines()
print "pwd: ", stdout.readlines()
Using paramiko to connect to remote host. How do i move across directories and perform unix operations? Sample code below
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('remote ip', username='username', password='password')
ftp = ssh.open_sftp()
ssh.exec_command('cd /folder1/folder2')
How do i perform operations like listing files in a directory, checking current working directory and perform other unix commands?
This way (example on ls command):
import paramiko
import sys
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('x.x.x.x', port=22, username='user', password='pass')
stdin, stdout, stderr = ssh.exec_command('ls')
# Wait for the command to terminate
while not stdout.channel.exit_status_ready():
# Only print data if there is data to read in the channel
if stdout.channel.recv_ready():
rl, wl, xl = select.select([stdout.channel], [], [], 0.0)
if len(rl) > 0:
# Print data from stdout
print stdout.channel.recv(1024),
ssh.close()
import paramiko
paramiko.util.log_to_file(r'D:\logs\paramico.log')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('xxx.xxx.xx.xx', port=22, username='xxxxx', password='xxxxxx')
stdin, stdout, stderr = ssh.exec_command('ll')
output = stdout.readlines()
print '\n'.join(output)
print output
This is a linux host and I want to list the files/folders present in it. But, I am getting empty list [].
Can please any one suggest me how to proceed to list the contents present in that.
import paramiko
paramiko.util.log_to_file(r'D:\logs\paramico.log')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('xxx.xxx.xx.xx', port=22, username='xxxxx', password='xxxxxx')
stdin, stdout, stderr = ssh.exec_command('ls -al')
output = stdout.readlines()
output1 = stderr.readlines()
print '\n'.join(output)
print output
print output1
As my host has hidden files, I need to use ls -al
I am a beginner trying to do SSH writing a basic code, I have tried everything not able to debug this , My code is as follows :
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print ("1")
ssh.connect('196.5.5.6', username='abc', password='abc')
print ("2")
stdin, stdout, stderr = ssh.exec_command('show version')
print ("3")
output= stdout.readlines()
print ("4")
print(output)
Output I get is 1
2
3
At 4 it get's stuck somewhere , there is problem that I am not able to fetch the data , Please help anyone. Code just hangs at the output step. Everywhere the solution is totally same.
You should enter commands try this
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print ("1")
ssh.connect('ip', username='user', password='pass')
print ("2")
stdin, stdout, stderr = ssh.exec_command('show version')
print ("3")
stdin,stdout,stderr = ssh.exec_command("ls /")
print stdout.readlines()
You don't need to do readlines() on stdin. You can print it directly. readlines() expect a file to be opened and read from the file. Whereas stdin, stdout, stderr are not files, rather a block of strings ( or string buffer used in paramiko channel). If you check the type of stdin, stdout, stderr, you will find <class 'paramiko.channel.ChannelFile'>, which are not exactly files, but are file like objects created to store the buffers in paramiko channel.
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print 1
ssh.connect('196.5.5.6', username='abc', password='abc')
print 2
stdin, stdout, stderr = ssh.exec_command('show version')
print 3
output= stdin
print 4
print(output)
print '---', stdout
print '---==', stderr
# sshpy v1 by s0urd
# simple ssh client
# irc.gonullyourself.org 6667 #code
import paramiko
import os
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
privatekey = os.path.expanduser('/home/rabia/private')
mkey = paramiko.RSAKey.from_private_key_file(privatekey)
ssh.connect('78.46.172.47', port=22, username='s0urd', password=None, pkey=mkey)
while True:
pick = raw_input("sshpy: ")
stdin, stdout, stderr = ssh.exec_command(pick)
print stdout.readlines()
ssh.close()
When I try to run more then 1 command I get this error:
AttributeError: 'NoneType' object has no attribute 'open_session'
Looks like it's because at the end of the while loop you do ssh.close() (thus closing the session).