Watch terminal for a particular prompt and automatically enter the response












0















I am using ZSH and I am wondering whether there is a way of watching the stdout for a particular prompt and then automatically input the response?



In particular I would like to automatically populate the MFA token when prompted like below;



Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:


I can generate the token by the use of the http://soundly.me/oathplus/ tool but want to get this seamlessly working so I don't need to manually enter the token.



Requirements:



I would like to include the script (possibly using expect) in the ~/.zshrc file. This would be loaded once when starting the shell/ terminal and would then be waiting for the prompt "Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:". I will then use the terminal in a normal fashion and might eventually use a command that triggers the MFA token prompt.



Note, the MFA token prompt will not always pop up when executing the same command as it depends ono whether the previous connection is still valid. Furthermore, there might be days where I don't use the commands that would trigger the MFA token prompt but this script would still live in the background and jumps into action once there is actually a token prompt. Since the token itself would only be valid for a minute, it needs to be generated after the prompt popped up.










share|improve this question




















  • 3





    Sounds like a task for expect.

    – glenn jackman
    Feb 27 at 16:05











  • Correct me if I'm wrong but I believe 'expect' is only useful if you know that you are going to execute the command and expect the prompt. My aim is to use the terminal for whichever commands and it might be days before I execute a command (without explicitly using 'expect') which produces this prompt.

    – quat
    Feb 28 at 22:05
















0















I am using ZSH and I am wondering whether there is a way of watching the stdout for a particular prompt and then automatically input the response?



In particular I would like to automatically populate the MFA token when prompted like below;



Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:


I can generate the token by the use of the http://soundly.me/oathplus/ tool but want to get this seamlessly working so I don't need to manually enter the token.



Requirements:



I would like to include the script (possibly using expect) in the ~/.zshrc file. This would be loaded once when starting the shell/ terminal and would then be waiting for the prompt "Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:". I will then use the terminal in a normal fashion and might eventually use a command that triggers the MFA token prompt.



Note, the MFA token prompt will not always pop up when executing the same command as it depends ono whether the previous connection is still valid. Furthermore, there might be days where I don't use the commands that would trigger the MFA token prompt but this script would still live in the background and jumps into action once there is actually a token prompt. Since the token itself would only be valid for a minute, it needs to be generated after the prompt popped up.










share|improve this question




















  • 3





    Sounds like a task for expect.

    – glenn jackman
    Feb 27 at 16:05











  • Correct me if I'm wrong but I believe 'expect' is only useful if you know that you are going to execute the command and expect the prompt. My aim is to use the terminal for whichever commands and it might be days before I execute a command (without explicitly using 'expect') which produces this prompt.

    – quat
    Feb 28 at 22:05














0












0








0








I am using ZSH and I am wondering whether there is a way of watching the stdout for a particular prompt and then automatically input the response?



In particular I would like to automatically populate the MFA token when prompted like below;



Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:


I can generate the token by the use of the http://soundly.me/oathplus/ tool but want to get this seamlessly working so I don't need to manually enter the token.



Requirements:



I would like to include the script (possibly using expect) in the ~/.zshrc file. This would be loaded once when starting the shell/ terminal and would then be waiting for the prompt "Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:". I will then use the terminal in a normal fashion and might eventually use a command that triggers the MFA token prompt.



Note, the MFA token prompt will not always pop up when executing the same command as it depends ono whether the previous connection is still valid. Furthermore, there might be days where I don't use the commands that would trigger the MFA token prompt but this script would still live in the background and jumps into action once there is actually a token prompt. Since the token itself would only be valid for a minute, it needs to be generated after the prompt popped up.










share|improve this question
















I am using ZSH and I am wondering whether there is a way of watching the stdout for a particular prompt and then automatically input the response?



In particular I would like to automatically populate the MFA token when prompted like below;



Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:


I can generate the token by the use of the http://soundly.me/oathplus/ tool but want to get this seamlessly working so I don't need to manually enter the token.



Requirements:



I would like to include the script (possibly using expect) in the ~/.zshrc file. This would be loaded once when starting the shell/ terminal and would then be waiting for the prompt "Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:". I will then use the terminal in a normal fashion and might eventually use a command that triggers the MFA token prompt.



Note, the MFA token prompt will not always pop up when executing the same command as it depends ono whether the previous connection is still valid. Furthermore, there might be days where I don't use the commands that would trigger the MFA token prompt but this script would still live in the background and jumps into action once there is actually a token prompt. Since the token itself would only be valid for a minute, it needs to be generated after the prompt popped up.







command-line zsh prompt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 5 at 11:10







quat

















asked Feb 27 at 16:03









quatquat

25638




25638








  • 3





    Sounds like a task for expect.

    – glenn jackman
    Feb 27 at 16:05











  • Correct me if I'm wrong but I believe 'expect' is only useful if you know that you are going to execute the command and expect the prompt. My aim is to use the terminal for whichever commands and it might be days before I execute a command (without explicitly using 'expect') which produces this prompt.

    – quat
    Feb 28 at 22:05














  • 3





    Sounds like a task for expect.

    – glenn jackman
    Feb 27 at 16:05











  • Correct me if I'm wrong but I believe 'expect' is only useful if you know that you are going to execute the command and expect the prompt. My aim is to use the terminal for whichever commands and it might be days before I execute a command (without explicitly using 'expect') which produces this prompt.

    – quat
    Feb 28 at 22:05








3




3





Sounds like a task for expect.

– glenn jackman
Feb 27 at 16:05





Sounds like a task for expect.

– glenn jackman
Feb 27 at 16:05













Correct me if I'm wrong but I believe 'expect' is only useful if you know that you are going to execute the command and expect the prompt. My aim is to use the terminal for whichever commands and it might be days before I execute a command (without explicitly using 'expect') which produces this prompt.

– quat
Feb 28 at 22:05





Correct me if I'm wrong but I believe 'expect' is only useful if you know that you are going to execute the command and expect the prompt. My aim is to use the terminal for whichever commands and it might be days before I execute a command (without explicitly using 'expect') which produces this prompt.

– quat
Feb 28 at 22:05










1 Answer
1






active

oldest

votes


















1














The expect would be something like this:



expect -c '
set mfa_token "your token here"
set mfa_prompt "Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:"
spawn zsh
interact {
-o $prompt {send_user $prompt; send "$mfa_promptr"}
}
'


That drops you into zsh, and you interact with it like a normal interactive shell.



The -o watches for that prompt coming from the shell's output, and automatically sends the token.



Expect has a pretty narrow use case these days, but this is in the wheelhouse.






share|improve this answer
























  • Thank you for your answer but it does not quite fit the use case. I will update the question to make it clearer.

    – quat
    Mar 5 at 11:01












Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1409914%2fwatch-terminal-for-a-particular-prompt-and-automatically-enter-the-response%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The expect would be something like this:



expect -c '
set mfa_token "your token here"
set mfa_prompt "Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:"
spawn zsh
interact {
-o $prompt {send_user $prompt; send "$mfa_promptr"}
}
'


That drops you into zsh, and you interact with it like a normal interactive shell.



The -o watches for that prompt coming from the shell's output, and automatically sends the token.



Expect has a pretty narrow use case these days, but this is in the wheelhouse.






share|improve this answer
























  • Thank you for your answer but it does not quite fit the use case. I will update the question to make it clearer.

    – quat
    Mar 5 at 11:01
















1














The expect would be something like this:



expect -c '
set mfa_token "your token here"
set mfa_prompt "Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:"
spawn zsh
interact {
-o $prompt {send_user $prompt; send "$mfa_promptr"}
}
'


That drops you into zsh, and you interact with it like a normal interactive shell.



The -o watches for that prompt coming from the shell's output, and automatically sends the token.



Expect has a pretty narrow use case these days, but this is in the wheelhouse.






share|improve this answer
























  • Thank you for your answer but it does not quite fit the use case. I will update the question to make it clearer.

    – quat
    Mar 5 at 11:01














1












1








1







The expect would be something like this:



expect -c '
set mfa_token "your token here"
set mfa_prompt "Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:"
spawn zsh
interact {
-o $prompt {send_user $prompt; send "$mfa_promptr"}
}
'


That drops you into zsh, and you interact with it like a normal interactive shell.



The -o watches for that prompt coming from the shell's output, and automatically sends the token.



Expect has a pretty narrow use case these days, but this is in the wheelhouse.






share|improve this answer













The expect would be something like this:



expect -c '
set mfa_token "your token here"
set mfa_prompt "Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:"
spawn zsh
interact {
-o $prompt {send_user $prompt; send "$mfa_promptr"}
}
'


That drops you into zsh, and you interact with it like a normal interactive shell.



The -o watches for that prompt coming from the shell's output, and automatically sends the token.



Expect has a pretty narrow use case these days, but this is in the wheelhouse.







share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 28 at 22:25









glenn jackmanglenn jackman

16.4k32645




16.4k32645













  • Thank you for your answer but it does not quite fit the use case. I will update the question to make it clearer.

    – quat
    Mar 5 at 11:01



















  • Thank you for your answer but it does not quite fit the use case. I will update the question to make it clearer.

    – quat
    Mar 5 at 11:01

















Thank you for your answer but it does not quite fit the use case. I will update the question to make it clearer.

– quat
Mar 5 at 11:01





Thank you for your answer but it does not quite fit the use case. I will update the question to make it clearer.

– quat
Mar 5 at 11:01


















draft saved

draft discarded




















































Thanks for contributing an answer to Super User!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1409914%2fwatch-terminal-for-a-particular-prompt-and-automatically-enter-the-response%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Probability when a professor distributes a quiz and homework assignment to a class of n students.

Aardman Animations

Are they similar matrix