paramiko

Preserve timestamp with Paramiko

馋奶兔 提交于 2019-12-05 07:11:37
Is there a way of preserving the timestamp when using Paramiko to SFTP files from one server to another similar to the -p argument in Linux? Original file: jim@vm3634:~$ ls -la -rwxrwx--- 1 jim admin 2214 Mar 30 17:33 compcip.asc Uploaded file: sftp> ls -la -rwxrwx--- 1 no-user no-group 2214 Mar 30 18:49 compcip.asc The uploaded file needs to have the same timestamp as the original. Paramiko does not support that. You have to explicitly call the SFTPClient.utime after the upload. Note that pysftp (that internally uses Paramiko) supports preserving the timestamp with its pysftp.Connection.put()

9-1

空扰寡人 提交于 2019-12-05 05:32:03
import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy) ssh.connect(hostname='192.168.0.108', port=22, username='root', password='test123') stdin, stdout, stderr = ssh.exec_command('df') result = stdout.read() print(result) print(result.decode()) ssh.close() import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy) ssh.connect(hostname='192.168.0.108', port=22, username='root', password='test123') stdin, stdout, stderr = ssh.exec_command('df') res, err = stdout.read(), stderr.read() result = res if res else err

Python - using env variables of remote host with / SSH

流过昼夜 提交于 2019-12-05 04:22:24
Any help on this issue would be greatly appreciated. Basically I'm writing a python script that will ssh onto various servers and to execute scripts. The problem is that these scripts use an env variable to start. Ie the script is test.sh but we use an env variable to launch it, run test.sh. So far the routes I have taken, such as Paramiko module execute commands but do not actually take on the env variables. import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('testserver' ) stdin, stdout, stderr = ssh.exec_command(cd /home/test/;

paramiko - connect with private key - not a valid OPENSSH private/public key file

点点圈 提交于 2019-12-05 02:28:20
问题 I am trying to find the solution to this and can't understand what I'm doing wrong. On my linux server I have run the following command: ssh-keygen -t rsa This generated an id_rsa and id_rsa.pub file. I then copied them both locally and attempted to run the following code: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('myserver', username='myuser', key_filename='id_rsa') sftp = ssh.open_sftp() sftp.chdir('/path/to/file') sftp.get(file, os

paramiko python module hangs at stdout.read()

左心房为你撑大大i 提交于 2019-12-05 01:40:00
I am using the below code: import paramiko def runSshCmd(hostname, username, password, cmd, timeout=None): client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(hostname, username=username, password=password, allow_agent=False, look_for_keys=False, timeout=timeout) stdin, stdout, stderr = client.exec_command(cmd) stdin.flush() data = stdout.read() print (data) client.close() runSshCmd("10.128.12.32", "root", "C0mput3Gr!d", "ts_menu") when it comes to stdout.read() , it hangs... sometimes it prints the output after long time. Can you please

How to see (log) file transfer progress using paramiko?

泪湿孤枕 提交于 2019-12-05 01:07:22
I'm using Paramiko's SFTPClient to transfer file between hosts. I want my script to print the file transfer progress similar to the output seen using scp. $ scp my_file user@host user@host password: my_file 100% 816KB 815.8KB/s 00:00 $ Any idea? Thanks in advance Use the optional callback parameter of the put function . Something like this: def printTotals(transferred, toBeTransferred): print "Transferred: {0}\tOut of: {1}".format(transferred, toBeTransferred) sftp.put("myfile","myRemoteFile",callback=printTotals) 来源: https://stackoverflow.com/questions/8313080/how-to-see-log-file-transfer

CMDB连接方式

安稳与你 提交于 2019-12-04 23:03:52
1.agent agent (放在每台客户端服务器上,定时任务) 脚本演示 # 采集本机的信息 执行命令 import subprocess v1 = subprocess.getoutput('ipconfig') v1 = v1[20:30] #print(v1) v2 = subprocess.getoutput('dir') v2 = v2[2:12] #print(v2) 2.paramiko(SSH) pip install paramiko 连接服务器 import paramiko private_key = paramiko.RSAKey.from_private_key_file(r'C:/Users/Administrator/.ssh/id_rsa') # 创建SSH对象 ssh = paramiko.SSHClient() # 允许连接不在know_hosts文件中的主机 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接服务器 #password密码 #pkey密钥 ssh.connect(hostname='192.168.16.85', port=22, username='root', pkey=private_key) # 执行命令 stdin, stdout,

Python ssh using Tor proxy

杀马特。学长 韩版系。学妹 提交于 2019-12-04 22:47:07
问题 I would like to be able to send data through Tor when I use ssh from Python scripts. Tor works as expected when I use an OpenSSH client to manually ssh to the host. This is my ssh config file. I use connect-proxy with ProxyCommand to route the connections through Tor (again, this works fine via a standard OpenSSH client): host host user user hostname host.domain.com CheckHostIP no Compression yes Protocol 2 ProxyCommand connect-proxy -S localhost:9050 %h %p I have this Python test script:

Paramiko: how to ensure data is received between commands

随声附和 提交于 2019-12-04 22:28:17
问题 I'm using Paramiko to issue a number of commands and collect results for further analysis. Every once in a while the results from the first command are note fully returned in time and end up in the output for the second command. I'm attempting to use recv_ready to account for this but it is not working so I assume I am doing something wrong. Here's the relevant code: pause = 1 def issue_command(chan, pause, cmd): # send commands and return results chan.send(cmd + '\n') while not chan.recv

Process dies, if it is run via paramiko ssh session and with “&” in the end

一个人想着一个人 提交于 2019-12-04 20:43:32
I just want to run tcpdump in background using paramiko. Here is the part of the code: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(host, username=login, password=password) transport = ssh.get_transport() channel = transport.open_session() channel.get_pty() channel.set_combine_stderr(True) cmd = "(nohup tcpdump -i eth1 port 443 -w /tmp/dump20150317183305940107.pcap) &" channel.exec_command(cmd) status = channel.recv_exit_status() After I execute this code, pgrep tcpdump returns nothing. If I remove & sign tcpdump runs correctly, but my ssh