Batch script adjusting Aruba Switch configuration and download it - batch-file

I've made a script that'll change the configuration and downloads the configuration of an Aruba 2530 switch, but it doesn't work all the time. How can I run the ip ssh filetransfer command from within the batchfile so I can automate things?
** config2530.bat **
#echo off
set /p USERNAME="Username? "
set /p PASSWORD="Password? "
for /f "tokens=*" %%i in (switchlist.txt) do (
echo plink -batch -ssh -l %USERNAME% -pw %PASSWORD% %%i -m config.txt
echo pscp -scp -pw %PASSWORD% %USERNAME%#%%i:/cfg/running-config d:\%%i.cfg
echo plink -batch -ssh -l %USERNAME% -pw %PASSWORD% %%i -m exit.txt
)
** config.txt **
config
timesync sntp
sntp unicast
sntp server priority 1 10.36.8.11
time daylight-time-rule western-europe
time timezone 60
write memory
ip ssh filetransfer
exit
exit
** switchlist.txt **
10.36.1.101
10.36.1.102
10.36.1.103
... etc. etc.
** exit.txt **
config
tftp
exit
exit
The issue is that I can't have plink run the commands from the text files with out "ip ssh filetransfer". I've tried enabling filetransfer using:
plink -ssh -l {username} -pw {password} {ipaddress} (config; ip ssh filetransfer)
but this gives:
Keyboard-interactive authentication prompts from server: ------------------
-Access granted. Press Return to begin session.
End of keyboard-interactive prompts from server ---------------------------
SSH command execution is not supported.
Without SSH filetransfer enabled I can't run config.txt or download the configuration from the switch. When I use Putty and manually enter the command "config" followed by "ip ssh filetranser" and then run the batch-file it'll do whatever I need it to do. I

Related

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)

Windows async batch cannot open database requested by the login

I'm running SQL Server 2005 (Express) on Windows Server 2003 SP2. I'm using a scheduled task to launch a batch program that runs for several hours, during which many child processes are launched.
Almost all these child processes are running synchronously, so the root batch program waits for each child program to complete.
As one of the last actions in the root batch program, several child processes are called asynchronously using start. This allows programs that depend on the work done by this scheduled task to start working on the updated data. The root batch program finishes by sending an email.
scheduled.bat
This is the batch program which is scheduled in Windows Scheduled Tasks.
cd backend
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined timestamp set timestamp=%%x
taskkill /F /FI "USERNAME eq admincm" /FI "WINDOWTITLE eq AtlasSync*"> Sync.Lte.%timestamp:~0,14%.log
title AtlasSync-%timestamp:~0,14%
sqlcmd -t 600 -d ATLAS -i Sync.Lte.build.sql -o Sync.Lte.bat
Sync.Lte.bat>> Sync.Lte.%timestamp:~0,14%.log
Sync.Lte.Bat (truncated)
The root batch program which calls sqlcmd, putty and mail several times. It connects to a remote host, runs a few scripts there, then collects all the log files. It FTPs the data back to the Windows server, where it gets imported into the "ATLAS" database. As soon as the data has been imported, dependencies are called via start.
D:
cd D:\PATH\Sync\backend
echo #!/bin/sh> DirtyNodes.Lte.sh
echo cd /home/cm/PATH/SYNC/>> DirtyNodes.Lte.sh
...
echo open 12.34.56.78> DirtyNodes.Lte.ftp
echo cm>> DirtyNodes.Lte.ftp
...
ftp -v -s:DirtyNodes.Lte.ftp
echo echo `date` -- cd /home/cm/PATH/SYNC/> DirtyNodes.Lte.putty
echo cd /home/cm/PATH/SYNC/>> DirtyNodes.Lte.putty
echo echo `date` -- chmod 755 DirtyNodes.Lte.sh>> DirtyNodes.Lte.putty
...
D:\PATH\TOOLS\putty -ssh -load AtlasSync -l cm -pw XXXXX -P 22 -m DirtyNodes.Lte.putty 12.34.56.78
echo #!/bin/sh> Refresh.Lte.build
echo MOS=/home/cm/PATH/SYNC/Refresh.Lte.mos>> Refresh.Lte.build
echo rm -f $MOS>> Refresh.Lte.build
...
echo #!/bin/sh> Refresh.Lte.sh
echo cd /home/cm/PATH/SYNC/>> Refresh.Lte.sh
echo echo `date` -- Running refresh amosbatch>> Refresh.Lte.sh
...
sqlcmd -t 600 -d CM -h -1 -W -i IpDatabase.Lte.build.sql -o ipdatabase
echo open 12.34.56.78> Refresh.Lte.ftp
echo cm>> Refresh.Lte.ftp
...
ftp -v -s:Refresh.Lte.ftp
echo echo `date` -- cd /home/cm/PATH/SYNC/> Refresh.Lte.putty
echo cd /home/cm/PATH/SYNC/>> Refresh.Lte.putty
echo echo `date` -- chmod 755 Refresh.Lte.build>> Refresh.Lte.putty
...
D:\PATH\TOOLS\putty -ssh -load AtlasSync -l cm -pw XXXXX -P 22 -m Refresh.Lte.putty 12.34.56.78
echo open 12.34.56.78> Import.Lte.ftp
echo cm>> Import.Lte.ftp
...
ftp -v -s:Import.Lte.ftp
cd D:\PATH\Sync\tables
unzip -o Tables.Lte.zip
cd D:\PATH\Sync\backend
sqlcmd -t 600 -d ATLAS -i Import.Lte.build.sql -o Import.Lte.sql
sqlcmd -t 600 -d ATLAS -i Import.Lte.sql -o Import.Lte.log
start D:\PATH\Harmony\backend\ConsistencyCheck.bat
start D:\PATH\Compass\backend\ConsistencyCheck.bat
cd D:\PATH\MAIL\XXXXX
mail.exe -s "Atlas Synchronized" -f "D:\PATH\Sync\backend\Import.Lte.log" -b XXXXX#XXXXX.com XXXXX#XXXXX.com
ConsistencyCheck.bat
This batch is started from Sync.Lte.bat asynchronously. It uses the same credentials as the user running the root batch program.
D:
cd D:\PATH\Harmony\backend
sqlcmd -t 10800 -d ATLAS -i ConsistencyCheck.sql -o ConsistencyCheck.log
ConsistencyCheck.log
Sync.Lte.bat runs just fine, and the email at the end is sent. But in ConsistencyCheck.log, I find the following errors:
Cannot open database "ATLAS" requested by the login. The login failed.
Login failed for user 'XXXXX\admincm'.
I have made both XXXXX\admincm and admincm db_owner of ATLAS.
All programs called from the scheduled task are run under the same user. All other programs (including sqlcmd calls to ATLAS) authenticate just fine, but the started programs do not.
When running ConsistencyCheck.bat manually (interactive RDP session) as another user, it runs fine.
When running ConsistencyCheck.bat manually as XXXXX\admincm, it runs fine.
When scheduling a test from XXXXX\admincm in a couple of minutes (making sure I'm fully logged out of that user in an attempt to emulate normal environment), it runs fine.
When the original scheduled task runs at night, the async'd batches fail with the mentioned errors above.

batch file creation pse

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

Using user input to customize a .bat shutdown command

Okay. I've figured out a way to turn the shutdown command in a .bat file into an instant messenger, because where I work there are no instant messengers allowed:
shutdown -s -m \\[computer name] -t 20 -c "[message]"
PING 127.0.0.1 -n 6
shutdown -a -m \\[computer name]
This works fine, aborting the shutdown command after 5 seconds, but you have to manually edit the computer name and message using a text editor and restart the program in order to send another message. I would like a way to take user input using the SET command that would take the computer name and message as input. I have tried this out, but it didn't work:
:Jump
set /P computer ="Enter the computer name: "
set /P message ="Enter the message: "
shutdown -s -m %computer% -t 20 -c "%message%"
PING 127.0.0.1 -n 6
shutdown -a -m %computer%
GOTO Jump
Any ideas?
You need to remove the space between your variable name and the equals sign:
set /p computer=Enter the computer name:
Otherwise the variable will not be set (it will default to "")

User Input - DOS batch file

I get a bat file as below:
#ECHO Executing scripts...
PAUSE
for %%X in (*.SQL) do SQLCMD -S localhost -d CTL -I -i "%%X" >> ResultScript.txt
pause
In this I want to has user inputs for localhost (Sql server instance) and CTL (database).
How can this be achieved in DOS (os: WinXP)
Thanks
SET /P variable=PromptString
So your batch file would be something like this
#ECHO Executing scripts...
PAUSE
SET /P sqlServer=Please enter SQLServer:
SET /P CTL=Please enter CTL:
for %%X in (*.SQL) do SQLCMD -S %sqlServer% -d %CTL% -I -i "%%X" >> ResultScript.txt
pause
Use parameter markers, where %1 refers to the first parameter, %2 the second, and so forth.:
for %%X in (*.SQL) do SQLCMD -S %1 -d %2 -I -i "%%X" >> ResultScript.txt
If your batch file was called ExecScript.bat, your user would run it as
ExecScript instancename databasename
You'll probably want to add a test above the for loop to make sure both parameters are passed in. Running SQLCMD with a blank instance and database wouldn't work too well.

Resources