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