Python Code to get the files from remote host using Paramiko - python

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

Related

Download file from a URL into a remote machine

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()

Perform unix operations after connecting to remote host using ssh

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()

How do I take screenshot on Unix using python script on windows PC?

Am trying to take a screenshot of UNIX server from my Windows PC. My command is not working it seems. When I try the same command on terminal it saves the file, however it is not with my below code.
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(sftp_server, username=sftp_login, password=sftp_password)
stdin, stdout, stderr = ssh.exec_command("xwd -root | convert xwd:- screenshot22.jpg")
sftp = ssh.open_sftp()
transport = paramiko.Transport((sftp_server, sftp_port))
transport.connect(username = sftp_login, password = sftp_password)
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.get("screenshot22.jpg", 'screenshot22.jpg', None)
sftp.close()
ssh.close()
Note:
1. xwd is installed on my UNIX Server.
2. Tried Import command, but that takes (2nd desktop of UNIX, not the one am trying to)
With the help of #Christopher Apple, I have figured out a way.
The working source code is,
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(sftp_server, username=sftp_login, password=sftp_password)
stdin, stdout, stderr = ssh.exec_command("xwd -out screenshot.xwd -root -display :0.0")
stdin, stdout, stderr = ssh.exec_command("convert screenshot.xwd screenshot22.jpg")
sftp = ssh.open_sftp()
transport = paramiko.Transport((sftp_server, sftp_port))
transport.connect(username = sftp_login, password = sftp_password)
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.get("screenshot22.jpg", 'screenshot22.jpg', None)
sftp.close()
ssh.close()

Python Script to SSH

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

Get Python Version from Remote Host with paramiko

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']

Categories