unix command is executing before the authentication completed over SSH - unix

i try to open a Unix session through java code and windows using putty.exe as follows:
Runtime.getRuntime().exec("cmd /c start /B C:/scripts/Session.bat ");
the 'Session.bat' file content is:
putty.exe -t -ssh root#aaa -pw abcd -P 22
aaa is the server name.
root is the user name.
abcd is the password
Now, the session opens well, but i want to run couple of simple commands from the above command (ll -s, pwd, etc.).
But when i try to add txt file which contains these commands to the above command, i see that the commands are executing after the user name entered and before the password.
the complete command is:
putty.exe -t -ssh root#aaa -pw abcd -P 22 -m C:\scripts\commands.txt
that's why i get the error (below) since the command "ll" came before the password:
Using username "root".
bash: ll:: command not found
please help me...
Thanks

There is no way the command could execute on the remote computer before the password is processed. Surely an alias is missing in root's bash startup files.
Try adding alias ll='ls -l' to /root/.bash_profile ?

Related

Running a command in SSH from command prompt

I am trying to access unix from command prompt using SSH connection with the below command
C:\Program Files\PuTTY>putty.exe -ssh gemini -l usename -pw password
Now i also want to run command in gemini from command prompt.
Will that be possible ?
I have found few solutions as below
C:\Program Files\PuTTY>putty.exe -ssh gemini -l username -pw password -m "C:\path\cmd.txt"
But running that just opens my putty and closes and not sure what is missing.
can someone help me with this please ?
Have you tried to remove doble quotes in file path?

Automating sftp using IBM AIX(UNIX) shell script

I am trying to automate my SFTP command using a UNIX shell script but for some reason it doesn't work. Here is my code as below. Please share your thoughts/insights.
#This ftp script will copy all the files from source directory into the local directory.
#!/bin/sh
HOST='sftp.xyz.com'
USER='ABC'
PASSWORD='123'
SRC_DIR='From_Src'
TRGT_DIR='/work/'
FILE='abc.txt'
sftp -u ${USER},${PASSWORD} sftp://${HOST} <<EOF
cd $SRC_DIR
lcd $TRGT_DIR
get $FILE
bye
EOF
echo "DONE"
When I try executing the above code I get the below error.
sftp: illegal option -- u
usage: sftp [-1246Cpqrv] [-B buffer_size] [-b batchfile] [-c cipher]
[-D sftp_server_path] [-F ssh_config] [-i identity_file] [-l limit]
[-o ssh_option] [-P port] [-R num_requests] [-S program]
[-s subsystem | sftp_server] host
sftp [user#]host[:file ...]
sftp [user#]host[:dir[/]]
sftp -b batchfile [user#]host
There is no -u option for sftp, see the manual for available options. You can pass the username in this format:
sftp username#hostname
So in your case:
sftp sftp://${USER}#${HOST} <<EOF
This will prompt you the password though. If you don't want a password prompt, take a look at this topic: How to run the sftp command with a password from Bash script?
First, learn how to set up keys so that you can ssh, scp, and sftp to a server without a password. Look at ssh-keygen. It is fairly easy. I bet there are how tos on this site. In brief, generate your keys with ssh-keygen. They are created in ~/.ssh. Then add your public key to ~/.ssh/authorized_keys on the destination host where ~ is the home directory of the user you want to log in as. i.e. "ABC" in your example.
Now, you can just do "sftp ABC#sftp.xyz.com" and you will be at the sftp prompt on sftp.xyz.com. From there, getting your script to work should be easy.
My real suggestion is blow off sftp and use scp. e.g.
scp /path/to/the/source_file user#host:/remote/path/file
Its that simple. No "cd" and other gunk to deal with. You are making this way harder than it really is.
Good luck

sudo login using plink executing by a batch file (mind blogging)

Here is what I wanna do... connent to a remote unix server from plink which is executed in a batch file, to run a script of other user name(as sudo user). I tried this.
#echo off
set /p id="Enter Unix Login ID: " %=%
set /p pwd="Enter Password: " %=%
cls
del jobInfo_error.txt
plink -l %id% -pw %pwd% Server0 "sudo su - user222" 2>>JobInfo_error.txt
plink -l %id% -pw %pwd% Server0 "/export/home/user222/user222_unix_works.ksh" 2>>JobInfo_error.txt
start notepad JobInfo_error.txt
In the above code i login as user000 and then i wanna sudo login as user222 and run the script user222_unix_works.ksh. The privilege for executing and writing is only given to user222 and i dont wanna change it.
here is why i think I am not geting the result I want, the first plink command plink -l %id% -pw %pwd% Server0 "sudo su - user222" 2>>JobInfo_error.txt is execute but the session is terminated and when the second runs i login as user000 and fails to execute.
is there a way to achive this?
here is the error in JobInfo_error.txt
bash: /export/home/user222/user222_unix_works.ksh: Permission denied
oh 2 more things i cannot directly login as user222
and i can execute any file for which user000 has privileges. i.e if i simple run with
plink -l %id% -pw %pwd% Server0 "/export/home/user000/user000.ksh"
it works.

PSEXEC not redirecting 7zip output

I'm trying to use PSEXEC to uncompress a self extracting file (a console exe created with 7zip) in a remote machine and view the results on my screen.
The remote command executes just fine, but I don't see it's output locally.
This is the command I'm using:
PSEXEC.exe \MACHINE_NAME -u USER_NAME -p PASSWORD -w "\JCOLIN\TWClient" cmd /c "\JCOLIN\TWClient\TW1.17.19.exe" -y
I also have tried:
PSEXEC.exe \MACHINE_NAME -u USER_NAME -p PASSWORD -w "\JCOLIN\TWClient" cmd /c "\JCOLIN\TWClient\TW1.17.19.exe" -y > "\JCOLIN\TWClient\TW1.17.19.exe.log"
in order to save the results in a log file and then retrieve the contents using the TYPE command but even if the log file is create it is always empty
I also have tried:
PSEXEC.exe \MACHINE_NAME -u USER_NAME -p PASSWORD -w "\JCOLIN\TWClient" cmd /c "\JCOLIN\TWClient\TW1.17.19.exe" -y 2> "\JCOLIN\TWClient\TW1.17.19.exe.log"
but in this case the PSEXEC output is saved to the file, not TW1.17.19.exe's output.
By the way, I also tried with a console SFX created with WinRAR with the same problem. I just do not understand why PSEXEC can redirect output from some programs and not others.
Do you have any idea on how to get the desired output on my screen?
Thank you in advance for any help.
You might try putting an escape character, "^", before the redirection symbol (^> instead of just >):
PSEXEC.exe \MACHINE_NAME -u USER_NAME -p PASSWORD -w "\JCOLIN\TWClient" cmd /c "\JCOLIN\TWClient\TW1.17.19.exe" -y ^> "\JCOLIN\TWClient\TW1.17.19.exe.log"
This should cause the redirection to occur on the remote machine, not the local machine.

Problem with plink output

I'm using plink to run a command on a Unix remote machine.
The command is:
ls -1trd testegrep.txt |tail -1 |xargs tail -f| grep 's';
The way I'm sending this command is by using a file with a set of commands like:
plink.exe -ssh -t -l user -pw pwd tst.url.pt -m commands.out
When I run the command this way the plink does not receive any input. It seems that is waiting for input.
But if I run:
plink.exe -ssh -t -l user -pw pwd tst.url.pt "ls -1trd testegrep.txt |tail -1 |xargs tail -f| grep 's';"
I get the expected result.
I'm not using the plink with a file with the command because I choose so. I'm using a test automation software that allows me to run tests on remote hosts and this is the way the tool works.
Any thoughts on what is going wrong?
I tested the command you provided and it worked without problems.
Maybe the problem is related to:
The server's host key is not cached in the registry.
The path to the file is not correct.
The file is empty.
include server hostkey
most importantly, you need to include the unix profile using the -m paramater
You can include all your commands in the same file where the profile is kept also.
$Output = ((plink.exe -hostkey hostkey -l UNAME -i SSHKEY -P 22 -ssh server -batch -m PROFILE) | ? {$_ -ne ""})

Resources