from fabric.api import env, sudo
def get_hostname():
env.hosts = ['user@host_ip']
env.passwords = {'user@host_ip': 'password'}
hostname = run_cmd('hostname')
print hostname
def run_cmd(cmd):
return sudo(cmd)
if __name__ == '__main__':
get_hostname()
This code is not working it is saying:
No hosts found. Please specify (single) host string for connection:
The function
get_hostnamewill not be called unless you specify it in the fab arguments, calling it underif __name__ == '__main__'will not do what you think it does because a fabfile is not like a usual python script.What you have to do is call your fabfile like this:
fab get_hostname run_cmdand to have a more consistent error you can use require function like this: