Windows Batch script for Unix commands

I wish to create a folder on a unix server B from my windows box using windows batch script.

Below is my windows batch script.

 
@ ECHO OFF
::Enter your Directory name:
echo Enter your Directory name:
set /p mydir=
plink user1@ServerA
mkdir %mydir%

At plink command i get logged on to ServerA and get auto ssh to Unix ServerB.

Post auto-login to ServerB the command [mkdir %mydir%] does not get executed on serverB from my Windows batch prompt. I have to manually type the mkdir command from my windows DOS shell inorder for the folder to be created on ServerB.

Can you help the mkdir command in my windows batch script to get auto executed on ServerB ?

This is to be expected. A script runs on a certain system. If you i.e. issue

cmd1
cmd2
cmd3

then you expect "cmd1" to get executed, then "cmd2", then "cmd3". "plink ..." is such a command and gets executed - and as soon as it terminates, then next command gets executed.

If you want to remotely execute the last command, remove it from the script. Then, depending on the functionality of this "plink" facility:

Put a parameter to the plink-command to instruct it to execute the command remotely. With "ssh" you can do that, even on Windows.

Put the necessary command into a script at the remote system. You'd still have to execute that script per hand or using the method above.

Install a "rexec" (remote exec) or "rsh" (remote shell) feature (or however this may be called in Windows, i have no idea), which is designed exactly for your purpose. Be advised that this is considered a security problem in most productive environments, which is why ssh has an analogous facility built in. That does the same but in a more secure way.

I hope this helps.

bakunin

The problem with plink is this matter is

If i instruct it to execute the command remotely then it says "Authentication Failed" becoz as I provide the password for Unix ServerA it auto switches to Unix ServerB which is where rather than prompting for the password for the second time for unix ServerB it reads the command

mkdir

and passes the command as an input to the password.

I hope you now understand what the problem is.

Now, can you please help me get the mkdir command somehow executed on unix ServerB from my windows ?

If "plink" can't be brought to do what you want consider alternatives. Use "ssh" or whatever else i already told you.

have a look at the startup files for your unix server too: it isn't usualy to immediately ask for a password for another machine. If you have an automatic connection to a remote system in some profile you should remove it, but this has nothing to do with the script at hand, just with the configuration of the remote system.

Anyway, this is all guessing wildly in the dark until you show any relevant information. Trace the process of running he script thoroughly on both systems and present *any* outcome here (unfiltered, instead of telling us about what you think the output might have been meaning).

This might get solved the problem, anything else is ultimately idle chit-chat.

I hope this helps.

bakunin

plink can take the password as a parameter, you don't have to jam it into stdin.

You should be checking the commands you run for success or failure, anyway.

The problem with this is, I am using Soft Token and the random password gets generated every 1 minute and the same password cannot be used twice.

It uses the first password for login to unix server A, and then expect a new random generated password for unix server B which I am not sure how to pass using plink along with the mkdir command.

the ssh option with plink does not do anything different.

plink -ssh user1@244.60.212.17 -pw 724494

It logs me in to unix ServerA and then to unix ServerB where is prompts for the password. Then I am into the unix shell from my windows shell as seen in the snap below.

In unix Server A home directory I see this mechanism to make it auto-switch to unix ServerB.

more .profile

echo "Connecting to: Server2"
exec /usr/local/bin/dev13
                exit
 
ls -ltra
-r-xr-xr-x 1 root root 2406 Feb  8  2012 /usr/local/bin/dev13

and below is the /usr/local/bin/dev13 file...

#!/bin/ksh
# $Header: /home/m37677/SOURCE/Server/RCS/dev13,v 1.16 2010/11/19 15:34:30 m37677 Exp $
touch $HOME/.lastlog
UNAME="$LOGNAME"
LANDHOST="dev13"
LANDTXT='Default (dev13)'
DATE=`date +%m%d%y%H%M`
LOG="$HOME/`basename $0`.log.$DATE"
PATH=$PATH:/usr/sbin
HOSTNAME="`hostname | awk -F. '{print $1}'`"
ALOG="/home1/logs/activity.log"
DLOG="/home1/logs/dactivity.log"
#function to do logging
LOGIT () {
FINISHED=`date +%d-%b-%y\ %T`
SFINISHED=`date +%m/%d/%Y\ %T`
echo "$LOGNAME|$HOST|$UNAME|$START|$FINISHED|$CMD|$FNAME|$TYPE|$SIZE|$HOSTNAME">>$ALOG
echo "$LOGNAME|$SRCIP|$DSTIP|$PORT|$UNAME|$START|$FINISHED|$CMD|$FNAME|$TYPE|$SIZE|$HOSTNAME">>$DLOG
echo "|$LOGNAME|$SRCIP|$DSTIP|$PORT|$UNAME|$SSTART|$SFINISHED|$CMD|$FNAME|$TYPE|$SIZE|$HOSTNAME" |logger -t SSHPT -p local5.info
}
#create activity log if it does not exsist
/usr/local/bin/Create_activity_log
let TT=`echo $LANDHOST |awk -F. '{print $1}'`/1 >/dev/null 2>&1
if [ $? -gt 0 ]; then
DSTIP="`nslookup $LANDHOST |grep Address |tail -1 |awk -F: '{print substr($2,2)}'`"
else
DSTIP=$LANDHOST
fi
unset TT
echo "STARTED: `date`" >$LOG
START=`date +%d-%b-%y\ %T`
SSTART=`date +%m/%d/%Y\ %T`
TIME=`date +%T`
if [ "`uname`" = "Linux" ];then 
SRCIP="`tail /var/adm/messages |grep $TIME | grep port |grep Accepted |tail -1|awk '{print $11\"|\"$13}' `"
else
SRCIP="`tail /var/log/messages |grep $TIME | grep port | awk '{print $11\"|\"$13}' |awk -F: '{print $4}' `"
SRCIP="`tail /var/log/messages |grep $TIME | grep port | awk '{print $11\"|\"$13}' |awk -F: '{print $4}' `"
SRCIP=`tail /var/adm/messages |grep $TIME | grep port | awk '{print $13\"|\"$15}' 2>/dev/null`
#Having problems capturing the log info from Sun.
if [ "$SRCIP" = "" ];then
SRCIP="`tail /var/adm/messages |grep $LOGNAME | grep port |tail -1 |awk '{print $14\"|\"$16}' `"
fi
fi
#if data is null use "|"
[ "$SRCIP" = "" ] && SRCIP="0.0.0.0|0"
#Log clean up
find $HOME -name "`basename $0`.$2.log.*" -mtime +5 -exec rm {} \; >/dev/null 2>&1 &
CMD="`basename $0`"
if [ -s /usr/localcw/opt/OPENssh/bin/ssh ];then
FNAME="N/A"
/usr/localcw/opt/OPENssh/bin/ssh -a -x $LANDHOST
fi
if [ -s /usr/bin/ssh ];then
echo "Session log = $LOG.session" >>$LOG
FNAME="$LOG.session"
/usr/bin/script -qc "/usr/bin/ssh -a -x $LANDHOST" $LOG.session 
fi
SIZE="N/A"
TYPE="N/A"
echo "FINISHED: `date`" >>$LOG
LOGIT

Is this what will help you help me :stuck_out_tongue:

bump ...:wall:

To be honest i thought it was obvious. This is the line which gets you in trouble. There is no way to log on to server A without getting automatically connected to server B, because "~/.profile" is executed every time the user logs on to the server A. Between us, i think this system of redirecting users from one host to another in their profile rather silly, but that just as an aside.

You could: move/remove/change the file "~/.profile". Give it another (any other) name and it will not be executed any more. Other options are to ask the systems admin for another user-ID, which doesn't have this profile. Either way, you have to make sure the profile doesn't get executed, that's all.

If you need to log on to the other host, server B, log on to it directly.

I hope this helps.

bakunin