Run a batch file on remote host using PsExec - batch-file

I am experimenting with PsExec and I am trying to run a batch file on a remote host from a local PC on the same LAN. The batch file has been tested on the local PC and works fine. I managed to connect to the remote host via PsExec using the below commands.
PsExec -u Username -p Password \\Remote_Host_IP C:\Path_to_batch_file\Batch.bat
I am getting this error:-
PsExec could not start C:\Path_to_batch_file\Batch.bat on Remote_Host_IP:
The system cannot find the file specified.
This is probably occurring since it is searching for the file on the remote host while the file is located on the local PC thus not finding the file.
I do not want to make any manual intervention on the remote host.
After trying hard to find the correct commands on the net I cannot solve this issue.

If you want you can try this:
PsExec.exe #pc_list.txt >>pc_log.txt -c D:\PC\pc.bat
Where:
pc_list.txt is a list of all your PC in your network
pc_log.txt is a log
D:\PC\pc.bat is the path where your script is
You can schedule it from a server that has access on all your network with task scheduler

Try this.
Psexec \\remotehost -u username -p password -c local_path\file.bat

Run it as:
PsExec \Remote_Host_IP -u Username -p Password cmd/c "C:\Path_to_batch_file\Batch.bat"
This should fix it

Related

PHP Script to copy database from one server to another

I have a scenario where I need to copy production database to my dev database on daily basis. Both are different servers. What I have thought of writing a cronjob that will do the stuff. I have written a php script. I am connecting to remote production server via sshpass, taking its dump and then populating that dump.
exec("sshpass -p 'mypassword' ssh root#IP_ADDRESS:PORT");
exec("mysqldump -u root -p DB > production_dump.sql");
exec("mysql -u root -p test < production_dump.sql");
But at first line it throws error of stating
ssh: Could not resolve hostname IP_ADDRESS:PORT: Name or service not known
I have tried given solution on internet but non of them worked. Can any on please explain what I am doing wrong?
Your command is failing because it's not formatted right. You need to use one of the following formats:
sshpass -p 'mypassword' ssh root#IP_ADDRESS PORT
sshpass -p 'mypassword' ssh root#IP_ADDRESS -p PORT
sshpass -p 'mypassword' ssh ssh://root#IP_ADDRESS:PORT
However, I'm not sure if the rest of the script will work, especially if it starts asking for a password. A bash script would be the way to go.

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

Using psexec.exe in jenkins, handle is invalid

I am using Jenkins on a Windows7 system. I would like to use it to execute a batch script on a remote Windows system. The batch script will be used to flash a development board and run some tests. I came across psexec.exe. That works well through a command prompt window--I can connect and run the script without any issues, but when I try to have Jenkins do it, I get the following output:
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
The handle is invalid.
Connecting to ABCDEFG...
Couldn't access ABCDEFG:
Connecting to ABCDEFG...
Build step 'Execute Windows batch command' marked build as failure
The command I am using in both cases is:
psexec.exe \\ABCDEFG -u "DOMAIN\username" -p "password" "C:\test.bat"
The user associated with username has administrator privileges on the remote system (ABCDEFG is not the real name of the system).
Can anyone help me figure out why it is not working through Jenkins? Or, is there an easier/better way to execute a batch script on a remote Windows system through Jenkins?
Thanks to all your help, especially Technext, I have a solution.
I needed run "services.msc", find "Jenkins", right click on it, and go to "Properties". Once the properties windows appeared, I had to click the "Stop" button to stop Jenkins, open the "Log On" tab, enter in my username and password (the username I used when running through command prompt), and start Jenkins again. That got rid of the "handle is invalid" message in Jenkins.
Update:
A better solution was to go onto the system I was using psexec.exe to get onto, go to Control Panel > User Accounts > Give other users access to this computer. Click on "Add..." and type in the username and domain Jenkins uses to run its commands (to find this, open your Jenkins in a browser window, go to Manage Jenkins > System Information and look for USERNAME and USERDOMAIN under Environment Variables). Make sure you give it Administrator rights. Then click ok. Now psexec.exe shouldn't have the "handle is invalid" issue.
Sorry, I don't have enough reputation for comments, but is the single \ a typo? Since
The handle is invalid.
probably means that the computer address is invalid. Try
psexec.exe \\ABCDEFG -u "DOMAIN\username" -p "password" "C:\test.bat"
Notice the two backslashes to access a locally mapped computer.
otherwise if that does not work i recommend the # tag
psexec.exe #servername.txt -u "DOMAIN\username" -p "password" "C:\test.bat"
where #servername.txt is a textfile containing only the servernames, one per line. The file parameter handles the formatting of \
ex servername.txt
ABCDEFG
COMPUTER2
EDIT: also found after some quick googling that it can be related to windows security.
Check out that a simple restart of the remote machine doesn't solve the problem. Also, adding parameters -h and -accepteula may help. Modified command:
psexec.exe \\ABCDEFG -u "DOMAIN\username" -p "password" -h -accepteula "C:\test.bat"
I execute below code from Jenkins pipeline groovy script to connect dynamically created VM as a resource on Jenkins master. Below code connect dynamically created VM as resource on Jenkins master with 4 executors. You can change the number of executors based on your requirement.
bat label: 'ConnectResource', script: """
#echo OFF
C:\\apps\\tools\\psexec \\\\${machine_ip} -u ${machine_ip}\\${machine_username} -p ${machine_password} -accepteula -d -h -i 1 cmd.exe /c "cd C:\\apps\\jenkins\\ & java -jar C:\\apps\\jenkins\\swarm.jar -master http://pnlv6s540:8080 -username ${jenkins_user_name} -password ${jenkins_user_password} -name ${machine_ip}_${BUILD_NUMBER} -labels ${machine_ip}_${BUILD_NUMBER} -deleteExistingClients -disableClientsUniqueId -executors 4" & ping 127.0.0.1 -n 60 > nul
"""

What could cause PSExec to only work remotely?

I am trying to update some legacy software so that it works on a new computer. The software connects to an AS400 with runs a .batfile on the local machine.
The command:
psexec \\localhost -i c:\path\to\file.bat s3
works like a champ when executed from the command line on the local machine, but when fired from the AS400, nothing happens. No errors. No anything.
Any ideas?
update The computer is a Windows 7 machine.
update 2 - batch file contents
FTP -s:C:\scls\PGINPC3I.FTP i.p.a.d.d.r.e.s.s
C:\scls\NGINPC3 S3
FTP -s:C:\scls\PGINPC3O.FTP i.p.a.d.d.r.e.s.s
Provided your version of PSExec is recent. If you haven't ran PSExec on the remote machine physically and accepted the license agreement, you'll get this kind of behavior. There is an undocumented switch for PSExec to alleviate this kind of issue. it's /accepteula just add that in your call to PSExec and see what happens.
Exactly like this with the forward slash:
psexec \\localhost -i /accepteula c:\path\to\file.bat s3
EDIT
The -c switch:
-c Copy the specified program to the remote system for
execution. If you omit this option the application
must be in the system path on the remote system.

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