0

I am trying to insert a variable in my code to run a command on a remote linux host, in order to do this i am using the .format method, see my below code:

import paramiko
from datetime import datetime, timedelta

hostnames = [
   'hostname',
]
username = 'username'
password = 'password'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_system_host_keys()
ydate = datetime.strftime(datetime.now() - timedelta(1), '%Y%m%d')

for host in hostnames:
   ssh.connect(host, username=username, password=password, look_for_keys=False)
   ssh.exec_command('cp test.txt {}.mrs_stats0.{}.pytest.gz ; chmod +r {}.mrs_stats0.{}s.gz').format(host, ydate, host, ydate)

I am failing when trying to use {} and .format, with the following error:

Traceback (most recent call last):
  File "C:/Users/IdeaProjects/main.py", line 25, in <module>
    ssh.exec_command('cp test.txt {}.mrs_stats0.{}.pytest.gz ; chmod +r {}.mrs_stats0.{}s.gz').format(host, test, host, test)
AttributeError: 'tuple' object has no attribute 'format'

I cannot understand why i am receiving this error, furthermore i have done some reading to further understand tuples and i cannot understand why my variables are not being read as strings

1 Answer 1

3

I think you want the last line to be

ssh.exec_command('cp test.txt {}.mrs_stats0.{}.pytest.gz ; chmod +r {}.mrs_stats0.{}s.gz'.format(host, ydate, host, ydate))

just a misplaced bracket. In your current code ssh is paramiko.SSHClient() instance and its method exec_command() method returns the stdin, stdout, and stderr of the executing command, as a 3-tuple (link to docs]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.