batch file creation pse - batch-file

I'm trying to run a batch file that will connect to the list of ip's on my network, open up cmd then run a list of commands: like ipconfig /all, nbstat -c, arp -a. Then it must save the results into a folder renamed as that "computername".
I already have a batch file made that can do the commands I want and create a folder with the computer, then input the different commands into txt files within that folder.
Here is the WindowsCommands batch file:
md %computername%
echo off
echo ARP Command
arp -a >> %cd%\%computername%\arp-a.txt
echo NBSTAT Command
nbtstat -c >> %cd%\%computername%\nbstat.txt
echo Ipconfig Command
ipconfig /all >> %cd%\%computername%\ipconfig-all.txt
echo Ipconfig DNS Command
ipconfig /displaydns >> %cd%\%computername%\ipconfig-displaydns.txt
echo Netstat Command
netstat -ano >> %cd%\%computername%\netstat-ano.txt
echo Tasklist Command
tasklist /v >> %cd%\%computername%\tasklist.txt
echo LG Admin Command
net localgroup administrators >> %cd%\%computername%\netlocalgroupadmin.txt
echo Directory Command
dir C:\Windows\Prefetch >> %cd%\%computername%\prefetch.txt
exit
I also created a hosts.txt file that contains my local Ip addresses that I want to run the commands on.
I also created another batch file name psexec for running a For loop.
Now my troubles and arising when trying to run the psexec batch file.
Here is my psexec file:
for /f %%a in (hosts.txt) do (
psexec \\%%a C:\Users\ISSG\Documents\WindowsCommands.bat
)
Now that is just a rough draft I'm not entirely sure if that is how it should be coded. This is one of the first automated scripts I have ever wrote.
So in a nutshell i need to be able to run this batch file from my local computer- psexec into the IP's. Gather the information and output it into txt files on my local computer.
If anyone could point me in the right direction that would be great!
Thanks!

If your Batch file doesn't exist in the system directory on all of the computers, you have to use the -c switch to copy it to them in order to run it. It's a good idea to try to ping the computer first to save time trying to connect to it.
for /f %%a in (hosts.txt) do (
for /f "tokens=2 delims=:" %%b in ('ping -n 1 %%a ^| find "TTL="') do (
if errorlevel 0 (
psexec \\%%a -c -f -u username -p password C:\Users\ISSG\Documents\WindowsCommands.bat
)
)
)
Also, keep in mind that this will create the folder in the System32 directory on the remote computer. If you want it on your local drive, do something like this:
#echo off
FOR /F "tokens=2" %%A IN (
'net use * "\\computer\share"'
) DO IF NOT %%A.==command. SET dl=%%A
if not exist "%dl%\%computername%" md "%dl%\%computername%"
echo ARP Command
arp -a >> %dl%\%computername%\arp-a.txt
echo NBSTAT Command
nbtstat -c >> %dl%\%computername%\nbstat.txt
echo Ipconfig Command
ipconfig /all >> %dl%\%computername%\ipconfig-all.txt
echo Ipconfig DNS Command
ipconfig /displaydns >> %dl%\%computername%\ipconfig-displaydns.txt
echo Netstat Command
netstat -ano >> %dl%\%computername%\netstat-ano.txt
echo Tasklist Command
tasklist /v >> %dl%\%computername%\tasklist.txt
echo LG Admin Command
net localgroup administrators >> %dl%\%computername%\netlocalgroupadmin.txt
echo Directory Command
dir C:\Windows\Prefetch >> %dl%\%computername%\prefetch.txt
Net use %dl% /delete

Related

How to store the windows OS NAME of remotes servers listed on a txt file

I am looking to store the windows OS NAME of remotes servers listed on a txt file called servers.txt, into another txt file, called osversion.txt.
I have a TXT file with a content like this:
HOSTNAME1
HOSTNAME2
........
HOSTNAMEn
I am writing a script with the following line that should do the job after getting the hostname on the variable HOSTNAME:
for /F "tokens=*" %%A in (servers.txt) do (
SET HOSTNAME=%%A
"C:\Oper\PsTools\psexec.exe" \\HOSTNAME -u %USER% -p %PASSWORD% cmd /K systeminfo | find "OS Name" >> \\%LOCALSERVER%\C$\Users\%MYUSER%\Desktop\osversion.txt
)
However my osversion.txt is empty and the cmd window
As an alternative, have you tried using WMIC?
Here's an example:
#Set "LIST=MyComputers.txt"
#Set "USER=MyUserName"
#Set "PASSWORD=MyPassword"
#Set "RESULTS=MyFile.csv"
#"%__APPDIR__%wbem\WMIC.exe" /FailFast:On /Node:"#%LIST%" /Output:"%RESULTS%" /Password:"%PASSWORD%" /User:"%USER%" OS Get Caption,OSArchitecture,Version /Format:CSV
Just propagate the values between the = and the closing " on each of the first four lines, with your actual strings.
Please open up a Command Prompt window and enter wmic /? to begin reading its usage information and to refine the above example to meet your needs.

Multiple commands using Plink and batch file for Cisco switch

I am currently trying to write a script to change the location (snmp) of more than 200 Cisco switches.
My problem is that I can't run more than one command at once. I've made a batch file which connects to the switch automatically and reads a .txt file where the commands are listed. But no matter what I do the best result I got was that only the first command was executed.
batch File:
cmd.exe /c echo n | "Filepath(plink)" -ssh Switch Hostname -l Username -pw "Password" -m "txt File"
txt File:
conf t
snmp-server location test
end
wr
exit
I've already tried other delimiters in the txt-File like ; | etc.
But nothing seems to work.
It's actually a known limitation of Cisco, that it does not support multiple commands in an SSH "exec" channel command.
Quoting section 3.8.3.6 -m: read a remote command or script from a file of PuTTY/Plink manual:
With some servers (particularly Unix systems), you can even put multiple lines in this file and execute more than one command in sequence, or a whole shell script; but this is arguably an abuse, and cannot be expected to work on all servers. In particular, it is known not to work with certain ‘embedded’ servers, such as Cisco routers.
Though actually, your commands are probably not standalone top-level shell commands anyway. I guess that the snmp-server (and others) are subcommands of conf t, aren't they? So your code would not work, even if Cisco did support multiple commands on the "exec" channel.
For details, see How to type commands in PuTTY by creating batch file?
You need to execute the conf t and then provide its subcommands to its standard input.
One way to do that is like this:
(
echo snmp-server location test
echo end
echo wr
echo exit
) | plink -ssh hostname -l username -pw password conf t
If the above mentioned Cisco limitation doesn't affect this syntax:
SET /P USERNAME=Enter remote Username:
SET "psCommand=powershell -Command "$pword = read-host 'Enter remote Password' -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)"" for /f "usebackq delims=" %%p in (`%psCommand%`) do set PASSWORD=%%p
plink -t -pw %PASSWORD% %USERNAME%#Hostname "COMMAND1; COMMAND2; COMMAND3; ETC"
If the above mentioned Cisco limitation DOES affect the above syntax:
SET /P USERNAME=Enter remote Username:
SET "psCommand=powershell -Command "$pword = read-host 'Enter remote Password' -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)"" for /f "usebackq delims=" %%p in (`%psCommand%`) do set PASSWORD=%%p
plink -t -pw %PASSWORD% %USERNAME%#Hostname "COMMAND1"
plink -t -pw %PASSWORD% %USERNAME%#Hostname "COMMAND2"
plink -t -pw %PASSWORD% %USERNAME%#Hostname "COMMAND3"
plink -t -pw %PASSWORD% %USERNAME%#Hostname "Etc"
Barbaric, yes, but I think Cisco can be thanked for that ;) (This is un-tested as I don't have a cisco device to poke at, but theory should be sound)

FTP commands in a batch script does not working properly

I've made a simple FTP upload script that should upload multiple files from a Windows 2008 Server to the FTP location. I've tried this manually by executing every command of the script directly in CMD and it works fine. However when I run script.bat it says that none of the commands are recognized as internal or external commands. I checked the ENV variables and there is a path to System32 so it should be fine. Can anyone please help with this. Thank you
open xx.xxx.xx.xx
user
pass
prompt
bin
lcd X:\test\test\
cd /tempTest/tempTest
binary
mput "*.*"
disconnect
quit
You can also try something like that with a batch file for multiple file Upload :
MultipleFileUpload.bat
#echo off
Title Multiple file Upload by Hackoo
mode con cols=85 lines=22 & Color A
::***********************************
Set FTPSERVER=ftp.xx.xxx.xx.xx.com
Set USER=UserName
Set Password=YourPassword
Set LocalFolder=X:\test\test
Set RemoteFolder=/tempTest/tempTest/
::***********************************
> ft.do echo Open %FTPSERVER%
>> ft.do echo %USER%
>> ft.do echo %Password%
>> ft.do echo prompt n
>> ft.do echo bin
>> ft.do echo lcd %LocalFolder%
>> ft.do echo cd %RemoteFolder%
>> ft.do echo mput *.*
>> ft.do echo bye
ftp -s:ft.do
del ft.do
Pause
Place your script in a text file on your desktop called ftpscript.txt
Create a batch file called getftp.bat and inside it have this - then you can click the bat file.
#echo off
ftp -i -s:"%userprofile%\desktop\ftpscript.txt"
pause

Psexec commands to remotely delete files in a few computers and looping of txt file

Hi I am New to scripting .
I have to remotely execute a PSEXEC command to delete al files in a certain drive in a few computers.
I do have a TXT file with every ip address of all the computers.
Is there a way to use PSEXEC to cmd command all the computers to delete the folders by executing every command for every IP in my txt file?
currently i am using this line whereby i need to manually enter the ip address in the command line for eg . PsTools>PsExec.exe \ 1.1.1.1 , 1.1.1.2 cmd /c rmdir /S /Q D:\
FOR /F %%i IN (FileWithEveryIP.txt) DO PSEXEC \\%%i CMD /C DIR folderToDelete
Use %i instead of %%i if you run it from command prompt window instead of batch file.
Replace DIR with RMDIR /S /Q if it works.

AutoIt or batch for FTP transfer to on multiple servers

I must log in to multiple servers and upload some files. Is there a chance to code this with AutoIt?
I have tested it with an batch file, but doesn't work:
echo open < /FTP/ftphost.txt
echo < /FTP/ftpuser.txt
echo < /FTP/ftppass.txt
echo cd C:\Dokumente und Einstellungen\Administrator\Desktop\FTP\dateien>>ftp.txt
echo cd httpdocs>>ftp.txt
echo mput test.txt test2.txt
echo bye
p
in the ftphost.txt File should be the diffrent FTP Servers
in the ftpuser.txt file the diffrent ftp users
in the ftppass.txt file one password.
If you use a file with the FTP information like this (and each FTP server has the same folder structure)
FTPlist.txt
ftp.host1.com|username1|password1
ftp.host2.com|username2|password2
ftp.host3.com|username3|password3
ftp.host4.com|username4|password4
Then this batch file can upload the set of files to each FTP server:
#echo off
for /f "usebackq tokens=1,2,3 delims=|" %%a in ("FTPlist.txt") do (
(
echo.open %%a
echo.%%b
echo.%%c
echo.binary
echo.lcd "C:\Dokumente und Einstellungen\Administrator\Desktop\FTP\dateien"
echo.cd httpdocs
echo.mput test.txt test2.txt
echo.bye
) >ftp.script
ftp -i -s:ftp.script >> ftp.log
)
del ftp.script

Resources