简体   繁体   中英

create a file under a directory in multiple machines through some script?

I have to create a file functon.txt under a particular directory with hello world in it in lots of machine. This is what I was doing so far manually one by one logging into each box and creating the file. That directory is own by root so I have to make sure that new file is also owned by root user.

david@machineA:~$ sudo su
[sudo] password for david:
root@machineA:/home/david# cd /opt/Potle/ouyt/wert/1
root@machineA:/opt/Potle/ouyt/wert/1# vi functon.txt
root@machineA:/opt/Potle/ouyt/wert/1# ssh david@machineB

david@machineB:~$ sudo su
[sudo] password for david:
root@machineB:/home/david# cd /opt/Potle/ouyt/wert/1
root@machineB:/opt/Potle/ouyt/wert/1# vi functon.txt
root@machineB:/opt/Potle/ouyt/wert/1# ssh david@machineC

.....

Now I have to do this in around 200 machines. Is there any way I can do these things through some script? I am ok typing passwords multiple times if I have to but I don't want to manually login into those box and do all the other steps by hand.

I have a file hosts.txt which contains each machine line by line. I can read this file line by line and do above things but I am not sure how?

This is just one time exercise for me so any easy or simple way should be fine. I can even hardcode my password in the script to do this job. What is the best way to accomplish this task?

After installing Ansible:

ansible -i /path/to/hosts.txt -m ping -u david --ask-pass all

See if you can ping the machines successfully. If it is successful, then try the following with 2 machines (create another txt file with just 2 machines and pass it to -i option). Then you can run this for all machines. If the directory does not exist, the command will fail and you will see the failed machines in summary.

ansible -i /path/to/hosts.txt -m copy -a "src=/path/to/functon.txt dest=/opt/Potle/ouyt/wert/1/functon.txt" -u david --ask-pass --become --become-user root --ask-become-pass all

I didn't test this. So use caution.

  • -i : input host(s)
  • -m : module
  • -a : module arguments
  • -u : user
  • --ask-pass : Ask for user password
  • --become : become another user
  • --become-user : new user
  • --ask-become-pass : Ask for become user password

You can use expect to automate SSH copy / SSH login :

#!/usr/bin/expect

set password  [lindex $argv 1]

spawn scp -P 22 [lindex $argv 2] [lindex $argv 0] 

expect "*password:*"
send -- "$password\r"
send -- "\r"

expect eof

The expect command will wait for the string you give in arguments to be received.

You can iterate over your hosts from hosts.txt and run this script like this for each one :

./create_config.sh david@machineA:/opt/Potle/ouyt/wert/1/ somePassword functon.txt

If you dont have possibility to do SSH copy but only SSH, you can still send command with expect :

#!/usr/bin/expect

set password  [lindex $argv 1]

spawn ssh -p 22  [lindex $argv 0] 

expect "*password:*"
send -- "$password\r"
send -- "\r"

# expect the command prompt : change this if needed
expect "*$*" 

# execute some commands
send -- "echo 'some text to write to some file' > ~/some_file.txt\r"

# exit vm
send -- "exit\r"

expect eof

You can run this with :

./create_config.sh david@machineA somePassword

您可以使用sshfs:安装机器,执行您想要的操作,卸载并传递给下一个机器。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM