Reputation: 267
I am trying to execute some command to remote machine through python
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(SERVER_IP, username='root', password='xxxxx')
stdin, stdout, stderr = ssh.exec_command(
"tar -C /home -xzf /home/Tests.tar.gz;dos2unix /home/Tests/run.py;chmod +x /home/Tests/run.py;/home/Tests/run.py>/home/Tests/log.txt"
)
it seems the last command /home/Tests/run.py>/home/Tests/log.txt
is not working the log.txt
is not having the values, the same works well if I do /home/Tests/run.py>/home/Tests/log.txt
on remote machine terminal.
How to resolve it ?
Thanks in advance
Upvotes: 0
Views: 462
Reputation:
You are not transporting the client to session like so :
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(SERVER_IP, username='root', password='xxxxx')
#note the next line you are missing
ssh_session = ssh.get_transport().open_session()
Then (after the declaration of ssh_session) you may use ssh_session.exec_command(. . .)
.
Try that see if it works.
Upvotes: 1
Reputation: 20373
To make sure your paramiko
code is working and the connection is being made, try something simple like
stdin, stdout, stderr = ssh.exec_command("hostname")
and make sure that stdin
contains what you expect. Then have a go at debugging the command you're executing remotely:
$ tar -C /home -xzf /home/Tests.tar.gz
$ dos2unix /home/Tests/run.py
$ chmod +x /home/Tests/run.py
$ /home/Tests/run.py > /home/Tests/log.txt
If you run code on the remove machine through a regular ssh
connection it must perform as you expect it to if you want it to also work over paramiko
.
Upvotes: 0