Multiple commands using Plink and batch file for Cisco switch - batch-file

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)

Related

Batch script adjusting Aruba Switch configuration and download it

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

Keep the same file name on output file on a batch file

I'm currently working on an encrypting/decrypting automatization project and I need to create batch files for the task scheduler
And I'm facing the issue with the decrypting command
--passphrase "password" --batch -d --output ".JOAARPT.out" "*.JOAARPT"
the command decrypts all the JOAARPT files and changes the output file to JOAARPT.out but I cannot make the created decrypted file keep the same name as the source file
What wildcard should I use?
In cmd.exe a FOR loop is needed. Yes, it is awkward, but it is what it is. Use the FOR /? command to learn all about it.
FOR /F "delims=" %%A IN ('DIR /B "*.JOAARPT"') DO (
decry.exe --passphrase "password" --batch -d --output "%%~nA.JOAARPT.out" "%%~A"
)
If you wanted to step up to PowerShell, you could:
Get-ChildItem -File -Filter "*.JOAARPT" |
ForEach-Object {
& decry.exe --passphrase "password" --batch -d --output $($_.BaseName + '.JOAARPT.out') "$_.FullName"
}

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.

Adding a variable in CMD

I wrote a small script which intends to copy a file from our Servers, copy it to a local machine and run it. It works for me, however I would like to add parameters in order to make it easy for others to use it as well.
#echo off
pushd \\NetworkPath & copy batfile.bat \\ComputerName\c$\Users\UserName\Desktop & popd & psexec -i -s -d \\ComputerName -u UserName -p UserNamePassword "C:\Users\UserName\Desktop\batfile.bat"
As you can see it copies the file locally to the Desktop of the user and runs the file itself. Please tell Me how I can use variables for ComputerName,UserName and UserNamePassword in order to have a query each time asking me what are the values.
You need the SET /p command:
#echo off
SET /p pwd=password:
SET /p usr=user name:
SET /p compname=computer name:
pushd \\NetworkPath & copy batfile.bat \\%compname%\c$\Users\%usr%\Desktop & popd & psexec -i -s -d \\%compname% -u %usr% -p %pwd% "C:\Users\%usr%\Desktop\batfile.bat"

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

Resources