I'm writing a python script capturing FQDN from within the remote host's itself just for some sanity checks, below is the small code I'm trying to run and want to capture the stdoutout of the same into my current host from i'm running it but rather its creating files into the remote hosts, and not the one from where I'm running.
#!/usr/bin/python3 system_name = socket.gethostname() f = open("output.txt", "a") print("Hostname: {}".format(system_name, file=f) f.close() OR
#!/usr/bin/python3 system_name = socket.gethostname() sys.stdout=open("output.txt","a") print("Hostname: {}".format(system_name) sys.stdout.close() Even i tried adopting like but that didn't worked though.
name = 'mycurrenthost' system_name = socket.gethostname() short_sysname = socket.gethostname().split('.', 1)[0] if short_sysname == name: f = open("output.txt", "a") print("Hostname: {}".format(system_name), file=f) f.close() My example myhostlist looks like..
mydbhost1 mydbhost2 mydnhost3 mydbhost4 Below id how I'm executing the above code..
$ for host in `cat myhostlist`;do timeout -t 20 ssh -o "StrictHostKeyChecking no" "$host" "python -s" < ./myPythonScript.py;done My output on the current console while not writing to a file...
Hostname: mydbhost1.example.com Hostname: mydbhost2.example.com: Hostname: mydnhost3.example.com: Hostname: mydbhost4.example.com: please help