Passing inputs to program prompt in a batch file - windows

I am running simulations in parallel using mpich2. I've got rather stringent security on my workstation, and must register using a new password each time I run a simulation. I have to enter:
mpiexec -register
which then prompt me for a username, and then prompt me for a password. Unfortunately, there seem to be no way to pass the user/pass to mpiexec on a single line, e.g.
mpiexec -register user:pass
does not work.
I'm trying to prepare a batch file that can automatically pass the username and password to the mpiexec prompts, but I cannot seem to get it to work. I've tried various things like timeout /t 5 but that doesn't work.
Can anyone tell me how to pass these inputs to the mpiexec program prompts in a batch file?
Thanks!
EDIT: I think I am getting closer. I've tried
(
echo username
echo password
echo password
) | mpiexec -register
which appears to be passing the username and password inputs to the mpiexec prompts. Program is still hanging at the next step however - not sure if that's a problem with the way I'm passing these or not.

You could redirect or pipe into mpiexec.
With redirection it's gets a bit nasty for user/password entries, as there are often unwanted (and unvisible) spaces at the line ends.
(
echo user
echo pwd
) | more > fetch.txt
Creates in fetch.txt
user<space>
pwd<space>
When you want to suppress the spaces use a file redirection instead
(
echo user
echo pwd
) > file.tmp
< file.tmp mpiexec -register
In both cases (redirection or pipe), you need to serve all inputs for the program, not only username and password.
You can't enter inputs from keyboard anymore.

Related

Send an (enter) to a runas command without user input

I have a need to perform a runas command, and send an enter keypress to the command without user interaction.
I understand that piping results out to a file, but i want to do it the other way, send n enter keypress to the command.
This is the command
runas.exe /user:XXX\USER_LOGIN notepad.exe
When this command is run, it prompts for a password... i dont want to supply a password, just press the enter key.
FYI - this is a very simple user refresh, so it doesn't go stale when not used in a while.
Try piping in a blank quoted string:
echo "" | runas.exe /user:XXX\USER_LOGIN notepad.exe

Stop SCP from password prompt

I am trying to copy my profile to a list of servers ( 5K ). I am using a private key to get the authentication working. Everything runs smoothly and as expected but I have this small annoyance :
Sometimes a server does not accept my key ( thats ok, I dont care if a few servers dont receive my profile ) but as the same was rejected, a prompt pops up asking for password and stops the execution until I type CTRL-C to abort it.
How can I make sure SCP uses the key and ONLY the key, and never prompts for any password?
NOTE : Im planning to add an ampersand at the end so all the copies will be done in parallel later.
Code
#!/bin/bash
while read server
do
scp -o "StrictHostKeyChecking no" ./.bash_profile rouser#${server}:/home/rouser/
done <<< "$( cat all_servers.txt )"
-B' Selects batch mode (prevents asking for passwords or passphrases)

How to write shell script which handles interactive inputs?

I have a command which takes 2 arguments.
When I run the command manually, I do this way:
cmd -i xyz.dat
hit enter
enter password in the prompt
hit enter
confirm password
My script needs to do the above operations without expecting user to enter password. I can hardcode the passwords in the file, but need a way to run this command successfully when I execute the shell script.
As on Feb 7th,
I have expect installed on my AIX. When I type expect at the command prompt, the prompt changes to expect 1.1> OS is 64 bit AIX
I have followed the instructions mentioned in the below comment, but I keep getting error - could not execute the command; no such file or directory"? I am able to manually run this command from same directory I am running the script. Besides that I have given the complete path of the command and the
file.
I am pasting another program I tried to su with root password as below: i get the same error message when I run the test program. I doubt if this is something related to quotes.
#!/bin/bash
set timeout 20
spawn "su"
expect "Password:" { send:"temp123\r" }
interact
Can someone please help me fix this error?
Sounds like you want to use expect. Here is a page with some examples.
So for your command you would want something like:
#!/usr/bin/expect
set timeout 20
spawn "cmd -i xyz.dat"
expect "<your password prompt" { send "<your password>\r" }
expect "<your password confirmation prompt" { send "<your password>\r" }
interact

using grep in a script which prompt user for input

I have written one shell script which ask for some username and password from standart input.
Once username and password is typed there is a output depending upon the parameters passed in the script.
Say my script name is XYZ.ksh.
Now my problem is that users of these script want to use want to use this script in conjugation with other shell commands like grep, less, more, wc etc.
Normally yes they can use
XYZ.ksh | grep abc
But in my case since XYZ is prompting for username and password we are not able to use "|" in front of that. It blocks forever.
I just wanted to know how can I implement the functinality.
What I tried
I tried taking input of "more commands " from user where user types things like "| grep abc"
but when i used this input in my script it did not work.
Use <<< like this:
XYZ.ksh <<< "your inputs" | grep abc
In your script you can test to see if stdout is connected to a terminal with:
if [[ -t 1 ]]
That way you can supress the prompt if the output is not going to the console.
Alternatively, with your "more commands" solution, run the command connected to a named pipe.
There are multiple solutions commonly used for this kind of problem but none of them is perfect :
Read password from standard input. It makes it really hard to use the script in pipes. This method is used by commands that deal with changing passwords : passwd, smbpasswd
Provide username and password in the command line parameters. This solution is good for using the script in pipes, but command line can be viewed by anyone, using ps -ef for exemple. This is used by mysql, htpasswd, sqlplus, ...
Store username and password unencrypted in a file in user's home directory. This solution is good for using the script in pipes, but the script must check if the file is visible or modifiable by other users. This is used by mysql
Store private key in local file and public key in distant file, as used by SSH. You must have a good encryption knowledge to do this correctly (or rely on SSH), but it's excellent for use in pipes, even creating pipes accross different machines !
Don't deal with passwords, and assume that if a user is logged in in the system, he has the right to run the program. You may give execute privilege only to one group to filter who can use the program. This is used by sqlplus from Oracle, VirtualBox, games on some Linux distributions, ...
My preferred solution would be the last, as the system is certainly better than any program I could write with regard to security.
If the password is used to login to some other service, then I would probably go for the private file containing the password.
One less-than-optimal possibility is to display the prompt to stderr instead of stdout.
echo -n "Username:" >/dev/stderr
A better solution would be to check stdin of the shell. If it's a terminal, then open it for writing and redirect to that file. Unfortunately, I'm not sure how to do that in bash or ksh; perhaps something like
echo -n "Username:" >/dev/tty
You can use (I assume you are reading username and password in your script with read)
(
read -p "user:" USER
read -p "pass:" PASS
) < /dev/tty > /dev/tty
and you'll be able to run
$ cmd | XYZ.ksh
However, I agree with other answers: just don't ask for user and password and give the correct permissions to the script to allow access.

plink truncating commands

I'm using plink.exe on WinXP to run some commands on Z/OS BASH. My commands are interspersed with echo commands so that I can parse the output and work out what is where. The first dozen or so commands run fine, but then one of them gets truncated.
For example:
echo :end_logdetail:
echo Job Name : TfmMigration
echo :jobinfo:
What happens:
user#host:/dev> echo :end_logdetail:
:end_logdetail:
user#host:/dev> echo Job Name : Tf
Job Name : Tf
user#host:/dev> echo :jobinfo:
:jobinfo:
I just checked where in the input file the error occurs, and it's exactly 4444 bytes in, on line 116 (so it's done 115 successful commands before it goes wrong). The command I'm using is:
Code:
plink -batch -pw xxxx user#host < "c:\dev\telnetcmd.txt" > "c:\dev\telnetout.txt"
The telnetcmd.txt is just a DOS text file with an "exit" command at the end.
Any idea why one of my commands is being truncated in this way?
Update: I don't get the problem if I pass the command file to plink with -m, only when I feed it in with the < operator.
As shellter points out, I should have been using the -m option. This does mean that (unlike the telnet solution that I was using) my commands do not show up in the output, and neither do the shell prompts, but I can manage without those.

Resources