Running script to get files from psftp - batch-file

I'm having some trouble and thought maybe someone could help me. This is what I have so far:
call date2.cmd
md %DT_MM%-%DT_DD%-%DT_YYYY%
cd %DT_MM%-%DT_DD%-%DT_YYYY%
psftp servername.com -P port -l username -pw pass -b script to run while in server
So, I run date2 which gives me the correct date output it starts psftp but stops there. I want to do an mget to grab all the files on the server and place them into the folder that I just created. If anyone can help steer me in the right direction that would be appreciated. I am sure I forgot some details, please let me know if there is any more information required.

(Untested)
Make a script called commands.txt and change your psftp line to actually use it:
psftp servername.com -P port -l username -pw pass -b commands.txt
Then put some test commands in the script and see what happens:
pwd
quit
or maybe
mget *
quit

I would recommend you use Putty plink.exe . I think it's much easier and it is scriptable and is able to handle or ignore prompting during the SSH session. Would also work on linux.

Related

Reading %0 as %0 in CMD

I think this is a pretty basic doubt but I could not find the solution myself. What I am trying to do is to create a .bat file in order to automate a data upload process. But the command to be passed in .bat file has to have database password which unfortunately has '%0' in it. So, when I run it, cmd reads it as the file name instead, causing it to run into an error. So, can we write this in a way cmd reads '%0' as it is and doesn't change it to filename?
Without seeing your code, it's guessing ...
Probably you are using something like
mysql -u root -p secret%0
Then you can fix it by doubling the percent sign
mysql -u root -p secret%%0

Running a HTA file remotely using PSTools

Its almost certain that HTA files are obsolete, but i've found that they are much better than net send / msg.
I'm trying to run a HTA file on a remote machine using PSTools, but instead of it running, it brings back a broken window:
Running the HTA file using CMD (locally) works perfectly though.
My PsExec line:
PsExec.exe -accepteula -i -d \\itwall cmd 'mstha \\intranet\Downloads\VisitorSystemNewMessage.hta asd'
I even tried to run the HTA from a Batch file, but the exact same thing happens.
Any ideas?
It's because the account running the command cannot interact with the session of the remote user.
Use the -s switch to run the HTA using the system account of the remote computer.
Also, you shouldn't need to run cmd. You should be able to just specify mshta.exe then your arguments.
PsExec.exe -accepteula -s -i -d \\itwall mshta.exe \\intranet\Downloads\VisitorSystemNewMessage.hta asd
Edit: To illustrate that this is not an HTA issue. Run the following command:
PsExec.exe -accepteula -i -d \\itwall notepad.exe
Notice you'll have the same black window showing.

Error using PSEXEC to start a file on a remote computer

I have created a small script that initiates PSEXEC to run a file (a registry key) on a remote computer that is in the domain; however, when I run the script I receive an error indicating that "the system cannot find the file specified."
Below is my script:
#echo off
psexec \\PRECDP19425 -u PRECDP19425\WAKE -p <password> -h -i -d C:\Users\WAKE\Documents\AllowNetwork.reg
pause
I have double-checked the location to make sure I wasn't losing my mind and typed something incorrectly, yet that doesn't seem to be the case. The path is correct, I'm just not sure what is causing the script to fail.
The exact error I receive is:
PsExec could not start C:\Users\etc on PRECDP19425
The system cannot find the file specified.
Any help would be much appreciated.
C:\Users\WAKE\Documents\AllowNetwork.reg is not a valid command line because a .reg file is not an executable, you need to pass it to reg.exe:
REG IMPORT C:\Users\WAKE\Documents\AllowNetwork.reg

how can I use a plink to write to a text file

I need to send numbers to a text file located on a remote windows machine , I'm using plink (putty command line-I have installed a ssh server on the windows machine) to establish the connection and it's successful but I'm having problems when I want to write numbers :
#echo off
cmd /c C:\Users\rob\Desktop\plink.exe -l user -pw password -ssh IP-remoteserver echo 10>> C:\Users\ser\Desktop\test.txt
EXIT
C:\Users\ser\Desktop\test.txt--- the file where I want to save the results.
Any ideas of how can I solve the problem? , not sure how can I use the echo function ,I just one to run this command on the remote server:
echo 10>> C:\Users\ser\Desktop\test.txt
But not sure how to add this part to the command where I run plink.exe
Any help will be appreciated.
Thanks and regards.
According to these instructions:
http://the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter7.html
you can set up a saved session, then call plink with the session ID followed by your chosen command.

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