Command to automatically input password for pscp - batch-file

I want to copy some file to a remote Linux system from my Windows PC using pscp (from putty). I wrote a small script that call the pscp commands in this way:
"C:\Users\hp\Desktop\pscp.exe" -scp C:\Users\hp\Desktop\scripts\* root#192.168.1.177:/root/scripts
"C:\Users\hp\Desktop\pscp.exe" -scp C:\Users\hp\Desktop\scripts2\* root#192.168.1.177:/root/scripts2
pause
But when I launch this bat script, I'm asked to input the password, so I input the password manually.
Is there a way to input the password automatically through the batch file?

pscp -pw yourPasswordHere C:\Users\testUser\Downloads\test.sh testUser#123.123.123.123:/home/testUser
I've noticed that no one posted sample of that command with -pw option.

From putty documentation
5.2.2.6 -pw passw login with specified password
If a password is required to connect to the host, PSCP will
interactively prompt you for it. However, this may not always be
appropriate. If you are running PSCP as part of some automated job, it
will not be possible to enter a password by hand. The -pw option to
PSCP lets you specify the password to use on the command line.

You can work this stuff out for yourself. Use the /? option to see the help. The pertinent parts are included below.
>pscp /?
PuTTY Secure Copy client
Release 0.60
Usage: pscp [options] [user#]host:source target
pscp [options] source [source...] [user#]host:target
pscp [options] -ls [user#]host:filespec
Options:
......
-l user connect with specified username
-pw passw login with specified password
......
Use the -l and -pw options to specify a user and password.
So whilst that answers the specific question that you asked, the real opportunity for you is to learn how to obtain documentation from command line utilities.

Use putty to set up a remote login without a password. This will involve the puttygen command. You will need a private key and the target will need a public key. Once you verified that you have a good private key file and login without typing a password, you can reference it with the "-i" private key option on pscp. You might be able to set up the key without the putty connect if it isn't allowed, but this is the easiest way. Just say no to passwords in the clear:
C:\Users\riglerjo>pscp -i .ssh\rigler_rsa.ppk test.txt rigler#rigler.org:.
test.txt | 0 kB | 0.0 kB/s | ETA: 00:00:00 | 100%

If your coping a local file to remote directory use:
pscp -pw yourPasswordHere C:\Users\testUser\Downloads\test.sh testUser#10.10.10.10:/home/testUser
If your coping a remote file to local directory use:
pscp -pr yourPasswordHere testUser#10.10.10.10:/home/testUser/test.sh C:\Users\testUser\Downloads\

sshpass -p'password' pscp -A -H "ip" -t 20 -l root /var/www/html/temp_santosh.txt /tmp/
You can use sshpass with -p option just before pscp

Related

automate the following steps

I have created an .bat file with the following command with a text file which contain "sudo su - oradev2"(C.txt).
cd C:\Users\chakraborty_sayantan\Desktop
plink -ssh serverA -l username -pw password -m "C:\Users\username\Desktop\c.txt" -t
The command above completes the first step of logging into a DB and the c.txt file has the instruction to enter into sudo mode. However, post this there is an authentication. Is there a way to automate the step of entering the password? Any thoughts?
Procedure to automate
login
password
sudo su - oradev
password
echo $ORACLE_SID
sqlplus / as sysdba
create user identified by
default tablspace
The above steps need to get automate using a bat file which consist of plink/putty.
-Sayantan
You can make a password and username txt file and maybe use the IF NOT EXIST Command to check if that user and pass file exists and set some commands up to create that file
Hope this is what you meant :)

Layout of command output executed automatically in PuTTY from a batch file is broken

I have followed this question to build a batch file to run the PuTTYwith my username and password:
How to run a command file in PuTTY using automatic login in a command prompt?
#echo off
START putty.exe -ssh [domain] -l [username] -pw [password] -m code.txt
#echo
And the PuTTY will try to run the code.txt file, which have the following code:
HResults -p -e "???" sil -e "???" sp -L labels/test lib/words3 results/*.rec
read
It will show a matrix. I try to run the batch file, it is able to open PuTTY, login and run the command in text file. But the output in PuTTY terminal is a mess. The layout of output is fine, when I doing those things manually. Is that mean some kind of setting is missing? It's not making any sense a batch file will change the output of another application...... Thanks
The -m switch imply a non-interactive session. While, when logging in manually, an interactive mode is used by default.
It may fundamentally affect output of some applications.
Try forcing the interactive mode using the -t switch:
START putty.exe -ssh [domain] -t -l [username] -pw [password] -m code.txt

How to use make scp prompt for password when executed with Plink

I want to write a batch script which will run scp command in remote server, using Plink from my Windows.
The code is:
plink.exe -ssh 10.168.5.15 -l username -pw passwd scp abc.txt xuser#10.168.10.108:/home
What I'm expecting is a prompt for password for the remote machine to where I'm coping file in my batch window. When I run above code my batch window shows, "Permission Denied, please try again" three times and "lost connection" message. I also tried scp with PasswordAuthentication=yes and BatchMode=yes but no use.
And I'm sure that Plink is interactive.
Any suggestions please?
The PLink runs the command (scp) in a non-interactive session by default. The scp won't even prompt for password in the non-interactive session for security reasons.
Use the -t switch to force the interactive session.
plink.exe -t -ssh 10.168.5.15 ...
Quoting section 3.8.3.12 of PuTTY/Plink manual:
3.8.3.12 -t and -T: control pseudo-terminal allocation
The -t option ensures PuTTY attempts to allocate a pseudo-terminal at the server, and -T stops it from allocating one. These options are only meaningful if you are using SSH.
These options are equivalent to the ‘Don't allocate a pseudo-terminal’ checkbox in the SSH panel of the PuTTY configuration box (see section 4.24.1).

putty from a batch file and a script?

I have a batch file that opens putty just fine.
c:\putty.exe root#192.168.12.34 -pw boyhowdy. But to make this work for me I need to understand how to include a script of a command so it will run under the putty tool. Like mount –o remount,rw / . Or is this something I can do with a tool called pscp. I am a nube to these tools and really could use some guideance. I have a bunck of these scripts and would really like to automate them. Thankyou
If your goal is to execute shell commands remotely through putty, you should probably look at plink (putty without the gui, in other words an ssh client for windows) and then apply the standard here-doc techniques to plink.
plink is part of the putty collection and is also downloadable from the same page as putty.
If you want to execute a local script you would use
plink user#host -m local_script.sh
For instance. Assuming you're running on some Windows box (fyi the putty suite also runs on Linux) and want to execute a batch of commands on some remote box you would create a shell script on your local machine (say mount.sh) and run it like this:
C:\> type mount.sh
whoami
hostname
/usr/sbin/mount -t iso9660 -o ro /dev/cdrom /mnt
/usr/sbin/mount | grep mnt
C:\> plink remoteuser#remotehost -pw secret -m mount.sh
remoteuser
remotehost
/dev/cdrom on /mnt type iso9660 (ro)
Also, it's probably better to copy your public key over so that the password is not coded in some batch file.
Finally, be aware that not all environment variable defined in an interactive shell process will be available in the remote shell process. You might need to 'source' some profile script at the beginning of your script.

How to send password using sftp batch file

I'm trying to download a file from sftp site using batch script. I'm getting the following error:
Permission denied (publickey,password,keyboard-interactive).
Couldn't read packet: Connection reset by peer
When running the command:
sftp -b /home/batchfile.sftp <user>#<server ip>:<folder>
the batchfile.sftp includes these data:
password
lcd [local folder]
cd [sftp server folder]
get *
bye
Note: It's working when running at the prompt as
sftp <user>#<server ip>:<folder>
But I need the ability to enter the password automatically.
You'll want to install the sshpass program. Then:
sshpass -p YOUR_PASSWORD sftp -oBatchMode=no -b YOUR_COMMAND_FILE_PATH USER#HOST
Obviously, it's better to setup public key authentication. Only use this if that's impossible to do, for whatever reason.
If you are generating a heap of commands to be run, then call that script from a terminal, you can try the following.
sftp login#host < /path/to/command/list
You will then be asked to enter your password (as per normal) however all the commands in the script run after that.
This is clearly not a completely automated option that can be used in a cron job, but it can be used from a terminal.
I advise you to run sftp with -v option. It becomes much easier to fathom what is happening.
The manual clearly states:
The final usage format allows for automated sessions using the -b
option.
In such cases, it is necessary to configure non-interactive authentication
to obviate the need to enter a password at connection time (see
sshd(8) and ssh-keygen(1) for details).
In other words you have to establish a publickey authentication. Then you'll be able to run a batch script.
P.S.
It is wrong to put your password in your batch file.
You mention batch files, am I correct then assuming that you're talking about a Windows system? If so you cannot use sshpass, and you will have to switch to a different option.
Two of such options, that follow diametrically opposite philosophies are:
psftp: command-line tool that you can call from within your batch scripts; psftp is part of the PuTTY package and you can find it here http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
Syncplify.me FTP Script: a scriptable FTP/S and SFTP client for Windows that allows you to store your password in encrypted "profile files"; check it out here http://www.syncplify.me/products/ftp-script/
Either way, switching from password to PKI authentication is strongly recommended.
PSFTP -b path/file_name.sftp user#IP_server -hostkey 1e:52:b1... -pw password
the file content is:
lcd "path_file for send"
cd path_destination
mput file_name_to_send
quit
to have the hostkey run:
psftp user#IP_SERVER
You need to use the command pscp and forcing it to pass through sftp protocol. pscp is automatically installed when you install PuttY, a software to connect to a linux server through ssh.
When you have your pscp command here is the command line:
pscp -sftp -pw <yourPassword> "<pathToYourFile(s)>" <username>#<serverIP>:<PathInTheServerFromTheHomeDirectory>
These parameters (-sftp and -pw) are only available with pscp and not scp. You can also add -r if you want to upload everything in a folder in a recursive way.
This command will help you
sshpass -p MYPASSWORD sftp MYUSERNAME#HOST

Resources