I'm using Python Paramiko and scp to perform some operations on remote machines. Some machines I work on require files to be available locally on their system. When this is the case, I'm using Paramiko and scp to copy the files across. For example:
from paramiko import SSHClient
from scp import SCPClient
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect('192.168.100.1')
scp = SCPClient(ssh.get_transport())
scp.put('localfile', 'remote file')
scp.close()
ssh.close()
My question is, how can I check to see if 'localfile' exists on the remote machine before I try the scp?
I'd like to try and use Python commands where possible i.e. not bash
Use paramiko's SFTP client instead. This example program checks for existence before copy.
#!/usr/bin/env python
import paramiko
import getpass
# make a local test file
open('deleteme.txt', 'w').write('you really should delete this]n')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect('localhost', username=getpass.getuser(),
password=getpass.getpass('password: '))
sftp = ssh.open_sftp()
sftp.chdir("/tmp/")
try:
print(sftp.stat('/tmp/deleteme.txt'))
print('file exists')
except IOError:
print('copying file')
sftp.put('deleteme.txt', '/tmp/deleteme.txt')
ssh.close()
except paramiko.SSHException:
print("Connection Error")
It should be possible to use only paramiko combined with 'test' command to check file existence. This doesn't require SFTP support:
from paramiko import SSHClient
ip = '127.0.0.1'
file_to_check = '/tmp/some_file.txt'
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect(ip)
stdin, stdout, stderr = ssh.exec_command('test -e {0} && echo exists'.format(file_to_check))
errs = stderr.read()
if errs:
raise Exception('Failed to check existence of {0}: {1}'.format(file_to_check, errs))
file_exits = stdout.read().strip() == 'exists'
print file_exits
Related
I am trying to write a Python 3 script to pragmatically ssh into a Linux server and change the password. I have put a script together using the Paramiko module.
I am running into issues when trying to run multiple shell commands. My script attempts to execute the commands but Paramiko times out after one shell command.
This is the script I am currently working on. Any insight would be greatly appreciated.
import paramiko
def change_pw():
hostname = "IP" #IP Address of Linux Server
username = "root" #username
password = "oldpw!" #password for Linux Server
#NOTE - This variable is suppose to define 3 shell commands. I do not believe the script is sending these commands as listed because the password does not update.
commands = [
"passwd",
"newpw!",
"newpw!"
]
#NOTE - Attempted to utilize '\n' to execute multiple commands and failed
# commands = [
# "passwd \n newpw! \n newpw!"
# ]
# initialize the SSH clientp0-
client = paramiko.SSHClient()
# add to known hosts
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
client.connect(hostname=hostname, username=username, password=password)
except:
print("[!] Cannot connect to the SSH Server")
exit()
# execute the commands
for command in commands:
print("="*50, command, "="*50)
stdin, stdout, stderr = client.exec_command(command)
print(stdout.read().decode())
err = stderr.read().decode()
if err:
print(err)
change_pw()
You do not have three commands. You have one command, the passwd, which takes two lines of input.
These two questions show how to provide an input to commands using Paramiko:
Pass input/variables to command/script over SSH using Python Paramiko
Executing command using "su -l" in SSH using Python
So specifically for passwd, you need to use:
stdin, stdout, stderr = client.exec_command('passwd')
# answer the new password prompts
stdin.write('newpw\n')
stdin.write('newpw\n')
stdin.flush()
# wait for the command to complete a print the output
stdout.channel.set_combine_stderr(True)
print(stdout.read().decode())
For the purpose of the Channel.set_combine_stderr, see Paramiko ssh die/hang with big output.
Obligatory warning: Do not use AutoAddPolicy – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".
The issue is that I was trying to utilize 3 input commands to change the password for root. I only needed to call the passwd command and then pass two input variables for "Enter new PW" and "Confirm new PW"
import paramiko
import time
hostname = 'IP'
username = 'root'
password = 'oldpw'
commands = ['passwd']
# initialize the SSH clientp
client = paramiko.SSHClient()
# add to known hosts
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
client.connect(hostname=hostname, username=username, password=password)
except:
print("[!] Cannot connect to the SSH Server")
exit()
# execute the commands
for command in commands:
print("="*50, 'PW change executed', "="*50)
stdin, stdout, stderr = client.exec_command(command)
stdin.write('newpw' '\n' 'newpw' '\n') #input varuables for "Enter new PW" and "re-enter new PW"
stdin.flush()
print(stdout.read().decode())
err = stderr.read().decode()
if err:
print(err)
So I built a program that prints out the login logs of my ubuntu server using tail -f.
The program uses Paramiko to connect via ssh and runs the command to tail the logs.
The program works but it prints out the motd from the server which is unnecessary.
I've tried splicing using itertools.
Tried using next().
Still doesn't work.
Here's my code:
import yaml, paramiko, getpass, traceback, time, itertools
from paramiko_expect import SSHClientInteraction
with open("config.yaml", "r") as yamlfile:
cfg = yaml.load(yamlfile, Loader=yaml.FullLoader)
def main():
command = "sudo tail -f /var/log/auth.log"
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
server_pw = getpass.getpass("Enter the password for your account %s on %s:" % (cfg['ssh_config']['username'], cfg['ssh_config']['host']))
sudo_pw = getpass.getpass("Enter the sudo password for %s on %s: " % (cfg['ssh_config']['username'], cfg['ssh_config']['host']))
ssh.connect(hostname = cfg['ssh_config']['host'], username = cfg['ssh_config']['username'], port = cfg['ssh_config']['port'], password = server_pw)
interact = SSHClientInteraction(ssh, timeout=10, display=False)
interact.send(command)
interact.send(sudo_pw + "\n")
with open(interact.tail(line_prefix=cfg['ssh_config']['servername']+': ')) as tail:
for line in itertools.islice(tail, 17, None):
print(line)
except KeyboardInterrupt:
print('Ctrl+C interruption detected, stopping tail')
except Exception:
traceback.print_exc()
finally:
try:
ssh.close()
except:
pass
if __name__ == '__main__':
main()
You get MOTD because you are opening an interactive shell session. I do not think you need that, quite on the contrary.
Use SSHClient.exec_command instead:
stdin, stdout, stderr = ssh.exec_command(command, get_pty=True)
stdin.write(sudo_pw + "\n")
stdin.flush()
for line in iter(stdout.readline, ""):
print(line, end="")
Related questions:
Get output from a Paramiko SSH exec_command continuously
Pass input/variables to command/script over SSH using Python Paramiko
What is the difference between exec_command and send with invoke_shell() on Paramiko?
Obligatory warning: Do not use AutoAddPolicy – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".
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()
I'm using the Paramiko module to log into a server (ssh on some and sftp on others). I can get text and log files from specific folders on the server no problem. But there are many sub-directories that have .txt and .log files. I read some where that the get method will not accept (*.txt). Does anyone know a way around this. Here is the code that I'm currently using to log into a server and get a specific log:
import paramiko
import sys
import os
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('10.5.48.74', username='root', password='******')
ftp = ssh.open_sftp()
ftp.get('/var/opt/crindbios/log/crindbios.log', '.')
ftp.close()
Acquire a list of files with the following script. Then iterate over the list with ftp.get
import paramiko
import os
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('localhost',username='****')
apath = '/var/log'
apattern = '"*.log"'
rawcommand = 'find {path} -name {pattern}'
command = rawcommand.format(path=apath, pattern=apattern)
stdin, stdout, stderr = ssh.exec_command(command)
filelist = stdout.read().splitlines()
ftp = ssh.open_sftp()
for afile in filelist:
(head, filename) = os.path.split(afile)
print(filename)
ftp.get(afile, './'+filename)
ftp.close()
ssh.close()
It is what dustyprogrammer proposed: On the remote server you apply shell commands to acquire the file list. Then you postprocess the list with python.
To download you have to create a new filepath for each file - download to directory as you proposed doesn't work (for me).
The filenames are easy accessible via sftp.listdir(). Therefore, I do it this way
import os
import paramiko
rserver = "raspberrypi"
ruser = "pi"
rpassword ="<your-password>"
rdirectory_charging_log = "/home/pi/logs/"
directory_charging_log = "/Users/<your-user>/logs/"
ssh = paramiko.SSHClient()
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
ssh.connect(rserver, username=ruser, password=rpassword)
sftp = ssh.open_sftp()
rfiles = sftp.listdir(rdirectory_charging_log)
rfile = ""
for rfile in rfiles:
sftp.get(rdirectory_charging_log+rfile, directory_charging_log+rfile)
sftp.close()
ssh.close()
I need a method of paramiko based file transfer with a lightweight SSH2 server (dropbear) which has no support for SCP or SFTP. Is there a way of achieving a cat and redirect style file transfer, such as:
ssh server "cat remote_file" > local_file
with paramiko channels?
Can paramiko.Transport.open_channel() or Message() do the job? I am unsure of how to proceed.
The following may be useful as a starting point (e.g. ./sshpipe host "command"):
#! /usr/bin/env python
import sys
import paramiko
def sshpipe(host, line) :
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host)
stdin, stdout, stderr = client.exec_command(line)
output = stdout.read()
sys.stdout.write(output)
stdin.close()
stdout.close()
stderr.close()
client.close()
sshpipe(sys.argv[1], sys.argv[2])