someone please help!
im currently writting a python script to actually retrieve a file size that is in a local PC and a remote server. then, what i do is, i compare if the file's size are the same. below is my code :
A = "/path/of/the/file/in/my/local/PC"
B = "/path/of/the/file/in/remote/PC"
statinfo1 = os.stat(A)
statinfo2 = os.system ("ssh" " root@192.168.10.1" " stat -c%s "+B)
if statinfo1 == statinfo2 :
print 'awesome'
else :
break
problem encountered : statinfo1 is able to return the file size in the local PC, but statinfo2 is not able to return the file size.. anyone please help? i want to use SSH method
Why dont you use Paramiko SSHClient. Its a nifty third party library simplifying ssh
access.
So to check file size of remote file the code would be something like -
import paramiko, base64
B = "/path/of/the/file/in/remote/PC"
key = paramiko.RSAKey(data=base64.decodestring('AAA...'))
client = paramiko.SSHClient()
client.get_host_keys().add('ssh.example.com', 'ssh-rsa', key)
client.connect('192.168.10.1', username='root', password='yourpassword')
stdin, stdout, stderr = client.exec_command("stat -c " + B)
for line in stdout:
print '... ' + line.strip('\n')
client.close()
Check this too - How to get size of remote file? and SSH programming with Paramiko
Using paramiko or pexpect will work, but may be kind of heavyweight for your simple use case here.
You can use a lightweight solution that simply relies on ssh
of the underlying OS, and python builtin subprocess
module.
import os
A = "/path/of/the/file/in/my/local/PC"
B = "/path/of/the/file/in/remote/PC"
# I assume you have stat visible in PATH on both the local and remote machine,
# and that you have no password nags for ssh because you've setup the key pairs
# already (using, for example, ssh-copy-id)
statinfo1 = subprocess.check_output('stat -c%s "{}"'.format(A), shell=True)
statinfo2 = subprocess.check_output('ssh root@192.168.10.1 stat -c%s "{}"'.format(B), shell=True)
You could also take a look at fabric for easy remote tasks.
fabfile.py:
1 from fabric.api import run, env
2
3 env.hosts = ['user@host.com']
4
5 def get_size(remote_filename):
6 output = run('stat -c \'%s\' {0}'.format(remote_filename))
7 print 'size={0}'.format(output)
Shell command:
~ $ fab get_size:"~/.bashrc"
statinfo1 = os.stat(A)
statinfo2 = os.system ("ssh" " root@192.168.10.1" " stat -c%s "+B)
What does os.stat
return?
- a data structure with several fields, one of which is the size.
what does os.system
return?
- it returns an
int
representing the exit code of the program invoked.
So the comparison between them is bound to fail. Consider Paramiko as @Srikar suggests, or address the issues with this approach.
For the latter, try this:
import commands
statinfo1 = os.stat(A).st_size
cmd = "ssh" " root@192.168.10.1" " stat -c%s "+B
rc, remote_stat = commands.getstatusoutput(cmd)
if rc != 0:
raise Exception('remote stat failure: ' + remote_stat)
statinfo2 = int(remote_stat)
if statinfo1 == statinfo2 :
print 'awesome'
else :
break