I'm trying to dump DHCP settings from an older server thats being decommissioned. I ran the command fine while on the actual server but when trying to run it using psexec remotely, it keeps failing. The command is: psexec \\$source netsh dhcp server \\$source dump>$dhcpSettings
$source = the server being decommissioned
$dhcpSettings = the path to save the dumped settings
I've tried all sorts of combinations of encapsulating quotation marks but still nothing. the errors I'm getting is, "The system cannot find the file specified" and "The system cannot find the path specified"
EDIT: So I got rid of the path to save the dumped settings and now it works. But how should I format the command so that it'll save the settings to the remote computer's C:\USER.SET\LOG directory?
One solution might be to bundle the command you want to run and the stdout redirection into a single line cmd file and use PsExec -c or -f to copy and execute that file on the remote system. As an example
Create a line cmd file named DHCPSettings.cmd with the following in it and save it to C:\temp\:
netsh dhcp server \\localhost dump >c:\user.set\log\dhcpsetting.log
Then use
psexec \\$source -c c:\temp\DHCPSettings.cmd
You did not really provide any code to go by and I am not sure I understand the all requirements and constraints you have, so consider this as an idea; not the exact commands you need to run. Hope it helps.
Related
I have 1000 images, and thanks to ImageMagick I can retrieve a lot of information regarding an image by writing "identify -verbose [PATH OF IMAGE]" in the cmd.
I installed ImageMagick with 'legacy on' , to access individual tools rather than the monolithic version of the library.
If I write "identify -verbose C:\\temp\\cbirCorel10k\\1.jpg" in the command prompt it works perfectly, outputting data I need.
My problem is when I insert this code into PROGRAM of PostgreSQL like so :
copy manyline(txt)
from program 'identify -verbose C:\\temp\\cbirCorel10k\\1.jpg'
It gives me this error, and please note that I do have permissions on the image and parent folders to be accessed:
ERROR: program "identify -verbose C:\temp\cbirCorel10k\1.jpg"
failed SQL state: 38000 Detail: child process exited with exit code
1
I want that the output from that command line, is inputted into the table 'manyline' under column 'txt'.
Please note that the following code works well, but I want to make use of the PROGRAM function.
copy manyline(txt)
from 'C:\\temp\\something.txt'
The postgresql server needs permissions to run that program and read that file. it usually runs under username postgres (unless they've changed that recently). this is a "service user" and can be hard to find in the windows GUI; especially on "home" editions of windows. I can't say for sure if granting NETWORK SERVICE access is enough.
easiest solution is to use the psql command-line interface and do
\copy manyline(txt) from program 'identify -verbose C:\\temp\\cbirCorel10k\\1.jpg'
\copy is a psql built-in which runs the command (opens the file etc) as the current user, instead of asking the server to run it. this also works well where the server is not local.
The UPS software I am using has functionality to run a script on the management PC (PC1) when it goes into battery mode. I have come up with a powershell script (ShutdownVM.ps1) that works fine on its own to Invoke-Commands on the VM server; shutting down VM's gracefully and turning off the host machine (SERVER). The first problem arose when the UPS software could not directly run a .ps1 file.
Simple enough, I thought I would make a simple .bat (shutdown.bat) file to run the .ps1 file on PC1 to shut everything off on SERVER. Running my .bat file from the desktop pc worked perfectly, but as the UPS software would run it as a service, my poor .bat file would run from Session 0.
Here is the code i used in Shutdown.bat:
#echo on
Powershell.exe -executionpolicy unrestricted -command C:\Windows\ShutdownVM.ps1
The interactive services manager would pop up and my computer would hang and finally the UPS software would turn it off and I'd be back to square one, with SERVER and its VM's still running. So I thought I would download and use PSExec to execute my shutdown.bat file.
I made another .bat file and called it PSExec.bat and below is the code in it:
#echo on
psexec.exe -accepteula \\PC1 -h -u user -p pass -i 2 C:\Windows\shutdown.bat
Finally! It tries to run! Upon checking the interactive services manager, it showed the PSExec was throwing the error "the system cannot find the file specified"
"The System Cannot Find The File Specified"
I have no idea what file it is even looking for, I have double, triple checked the path names in my scripts and still have no idea as to what it is doing. If anyone could shed any light (or let me know of an easier way to achieve what I am trying to do) that would be great. Thanks!
-F
Is there any way to save the PuTTY output to a file using the command line? I know this is easily done using the GUI but in my case it has to be done automatically.
What I'm working on:
User clicks batch file -> starts PuTTY, automatically connects to my device over SSH and runs a bunch of commands -> PuTTY should save the output to a file.
The last part I can't get working. Is there any command to do this?
This can be done with putty. The answer is little late considering the time the questions was asked, however this might help someone.
In putty, using GUI, you can save sessions with logging option on, as shown below.
Enter Host Name, Name the session, Go to Logging Option in the left top corner, select all sessions, provide log file name and location, go back to Session tab, click on the save button. Done, you have saved a session.
Now open CMD and write the command as below
You are done. Every time this session is invoked, the commands and output will be logged.
Hope this helps.
The specific program putty is not designed for this. Instead use plink, a different program in the PuTTY suite,
which uses the same session settings and keys as putty but gets input from stdin and puts output to stdout,
both of which can be redirected in the usual ways. See http://the.earth.li/~sgtatham/putty/0.63/htmldoc/Chapter7.html#plink .
As mentioned in previous answer, use plink for this.
Make sure it is in your environment path, by typing
plink -V
in your console. If it returns a version number, then you know it is in environment path variables. If it doesn't, probably best to fix this first. There are plenty of good SO answers to help you with this. Failing that, use the full path to your plink.exe in the CLI command that follows.
Then use plink to open your ssh connection, with the option -v set to provide verbose output. Finally, this all needs to be piped to a log file.
The complete cli command that I use is
plink -v username#xxx.xxx.xxx.xxx > ssh-output.log 2>&1
Open up the file ssh-ouput.log to see the results.
Expanding on Dave's and Charlie's answers...
Apart from making sure plink is in the path, also check whether you have write access to local ouput file.
This is how you redirect command output from remote machine to local file with plink. In this example we store an output from man page for nfcapd:
plink joe#192.168.50.50 -pw joespassword man nfcapd > output.log 2>&1
The first time you try to access the server, it will ask you store key in cache. So make sure to access the machine at least once before:
plink joe#192.168.50.50 -pw joespassword
The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
...
Store key in cache? (y/n)
I used PsExec to copy and run an exe file in a remote machine. I also want to copy a xml file to remote machine. I am able to do this way
PsExec.exe -d -c \\someserver c:\somefile.xml
The above command throws error saying system cannot find the file specified but adds the xml file to remote server.
Do u know any better way of copying files to remote server.
Is there any PsTool available for that?
Or an option in PsExec ?
Edit: (Answer)
I found out that using Powershell we can copy file to remote machines and it worked.
As you can read from psexec help
-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.
So your xml file is copied on remote sys/USER:[domainname]username]tem and executed, this gives you the error.
If your xml is part of an application you have to run in remote computer, one solution is compress the app with all necessary files in a self-extracting EXE that runs main command when extracted.
If you just have to copy a file, why don't you use a simple script that maps remote folder and then copies file? Something like:
NET USE \\computername\sharename password /USER:[domainname\]username
xcopy .....
NET USE \\computername\sharename /DELETE
PsExec is not designed to copy files across machines. It can only copy the program it is going to run remotely.
If you have access to the remote machine, the copy could be done by running copy c:\somefile.xml \\\\remote-machine\Admin$ before running PsExec.
You can use this pattern with psexec to copy any extension ...
psexec -d -i 2 \PC Name -u domain\username -p password cmd /c copy
\server\location\filename c:\xx\xx\xx
PS: Refer to PSEXEC switches if you're unsure of what -d and i does. However "2" is a session id of remote desktop user that may change every time a new remote desktop session is created.
this helped me copy my exe file into c:\windows directory (one to one copy within same domain) :
PsExec.exe -d -c \\remoteserver -u administrator -p password c:\executable.exe
Is it possible to have a Perl script run shell aliases? I am running into a situation where we've got a Perl module I don't have access to modify and one of the things it does is logs into multiple servers via SSH to run some commands remotely. Sadly some of the systems (which I also don't have access to modify) have a buggy SSH server that will disconnect as soon as my system tries to send an SSH public key. I have the SSH agent running because I need it to connect to some other servers.
My initial solution was to set up an alias to set ssh to ssh -o PubkeyAuthentication=no, but Perl runs the ssh binary it finds in the PATH instead of trying to use the alias.
It looks like the only solutions are disable the SSH agent while I am connecting to the problem servers or override the Perl module that does the actual connection.
Perhaps you could put a command called ssh in PATH ahead of the ssh which runs ssh as you want it to be run.
Alter the PATH before you run the perl script, or use this in your .ssh/config
Host *
PubkeyAuthentication no
Why don't you skip the alias and just create a shell script called ssh in a directory somewhere, then change the path to put that directory before the one containing the real ssh?
I had to do this recently with iostat because the new version output a different format that a third-party product couldn't handle (it scanned the output to generate a report).
I just created an iostat shell script which called the real iostat (with hardcoded path, but you could be more sophisticated), passing the output through an awk script to massage it into the original format. Then, I changed the path for the third-party program and it started working fine.
You could declare a function in .bashrc (or .profile or whatever) with that name. It could look like this (might break):
function ssh {
/usr/bin/ssh -o PubkeyAuthentication=no "$#"
}
But using a config file might be the best solution in your case.