Pass variable to spawn

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












I want to pass server name which is listed in a file (servers_list) as variable to spawn command which is in another script (user_create_script). Purpose of this script is to create user(david) in multiple servers.



#cat servers_list
server1
server2
server3


#cat user_create_script
spawn ssh -t user@$i sudo /usr/sbin/useradd david
expect "password:"
send "pass123r"
interact


Can anyone help me to achieve this.










share|improve this question







New contributor




Dev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • would you please read this answer unix.stackexchange.com/questions/187339/spawn-command-not-found
    – Goro
    2 hours ago






  • 1




    @Goro, thanks alot. Given link is very helpful. My Problem solved. Thanks aton
    – Dev
    1 hour ago














up vote
3
down vote

favorite












I want to pass server name which is listed in a file (servers_list) as variable to spawn command which is in another script (user_create_script). Purpose of this script is to create user(david) in multiple servers.



#cat servers_list
server1
server2
server3


#cat user_create_script
spawn ssh -t user@$i sudo /usr/sbin/useradd david
expect "password:"
send "pass123r"
interact


Can anyone help me to achieve this.










share|improve this question







New contributor




Dev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • would you please read this answer unix.stackexchange.com/questions/187339/spawn-command-not-found
    – Goro
    2 hours ago






  • 1




    @Goro, thanks alot. Given link is very helpful. My Problem solved. Thanks aton
    – Dev
    1 hour ago












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I want to pass server name which is listed in a file (servers_list) as variable to spawn command which is in another script (user_create_script). Purpose of this script is to create user(david) in multiple servers.



#cat servers_list
server1
server2
server3


#cat user_create_script
spawn ssh -t user@$i sudo /usr/sbin/useradd david
expect "password:"
send "pass123r"
interact


Can anyone help me to achieve this.










share|improve this question







New contributor




Dev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I want to pass server name which is listed in a file (servers_list) as variable to spawn command which is in another script (user_create_script). Purpose of this script is to create user(david) in multiple servers.



#cat servers_list
server1
server2
server3


#cat user_create_script
spawn ssh -t user@$i sudo /usr/sbin/useradd david
expect "password:"
send "pass123r"
interact


Can anyone help me to achieve this.







shell scripting expect






share|improve this question







New contributor




Dev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Dev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Dev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









Dev

211




211




New contributor




Dev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Dev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Dev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • would you please read this answer unix.stackexchange.com/questions/187339/spawn-command-not-found
    – Goro
    2 hours ago






  • 1




    @Goro, thanks alot. Given link is very helpful. My Problem solved. Thanks aton
    – Dev
    1 hour ago
















  • would you please read this answer unix.stackexchange.com/questions/187339/spawn-command-not-found
    – Goro
    2 hours ago






  • 1




    @Goro, thanks alot. Given link is very helpful. My Problem solved. Thanks aton
    – Dev
    1 hour ago















would you please read this answer unix.stackexchange.com/questions/187339/spawn-command-not-found
– Goro
2 hours ago




would you please read this answer unix.stackexchange.com/questions/187339/spawn-command-not-found
– Goro
2 hours ago




1




1




@Goro, thanks alot. Given link is very helpful. My Problem solved. Thanks aton
– Dev
1 hour ago




@Goro, thanks alot. Given link is very helpful. My Problem solved. Thanks aton
– Dev
1 hour ago










1 Answer
1






active

oldest

votes

















up vote
3
down vote













You'd have
user_create_script like this



#!/usr/bin/expect -f
set fh [open servers_list r]
while [gets $fh server_name] != -1
spawn ssh -t user@$server_name sudo /usr/sbin/useradd david
expect "password:"
send "pass123r"
expect eof

close $fh


I assume your remote user does not require a password for sudo.



More documentation about Tcl (upon which expect is built), including tutorials, is here: https://tcl.tk/doc/






share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    Dev is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f475265%2fpass-variable-to-spawn%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    You'd have
    user_create_script like this



    #!/usr/bin/expect -f
    set fh [open servers_list r]
    while [gets $fh server_name] != -1
    spawn ssh -t user@$server_name sudo /usr/sbin/useradd david
    expect "password:"
    send "pass123r"
    expect eof

    close $fh


    I assume your remote user does not require a password for sudo.



    More documentation about Tcl (upon which expect is built), including tutorials, is here: https://tcl.tk/doc/






    share|improve this answer
























      up vote
      3
      down vote













      You'd have
      user_create_script like this



      #!/usr/bin/expect -f
      set fh [open servers_list r]
      while [gets $fh server_name] != -1
      spawn ssh -t user@$server_name sudo /usr/sbin/useradd david
      expect "password:"
      send "pass123r"
      expect eof

      close $fh


      I assume your remote user does not require a password for sudo.



      More documentation about Tcl (upon which expect is built), including tutorials, is here: https://tcl.tk/doc/






      share|improve this answer






















        up vote
        3
        down vote










        up vote
        3
        down vote









        You'd have
        user_create_script like this



        #!/usr/bin/expect -f
        set fh [open servers_list r]
        while [gets $fh server_name] != -1
        spawn ssh -t user@$server_name sudo /usr/sbin/useradd david
        expect "password:"
        send "pass123r"
        expect eof

        close $fh


        I assume your remote user does not require a password for sudo.



        More documentation about Tcl (upon which expect is built), including tutorials, is here: https://tcl.tk/doc/






        share|improve this answer












        You'd have
        user_create_script like this



        #!/usr/bin/expect -f
        set fh [open servers_list r]
        while [gets $fh server_name] != -1
        spawn ssh -t user@$server_name sudo /usr/sbin/useradd david
        expect "password:"
        send "pass123r"
        expect eof

        close $fh


        I assume your remote user does not require a password for sudo.



        More documentation about Tcl (upon which expect is built), including tutorials, is here: https://tcl.tk/doc/







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 1 hour ago









        glenn jackman

        49k467105




        49k467105




















            Dev is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            Dev is a new contributor. Be nice, and check out our Code of Conduct.












            Dev is a new contributor. Be nice, and check out our Code of Conduct.











            Dev is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f475265%2fpass-variable-to-spawn%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            Long meetings (6-7 hours a day): Being “babysat” by supervisor

            Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

            Confectionery