Interactive Putty after remote commands & remotely sudo login - batch-file

I'm trying to run commands in putty remotely & unfortunately i stuck on two problems.
Putty cli closed after running those commands.
I want sudo login remotely without prompt for password
Note: I already found solution for both problems & posting this question for future use.

First problem solution detail in here
Second problem solved by first two line of RemoteCommands.txt first line suggested here & after running that i run sudo -i to do sudo login but it didn't prompt for password. so it solve accidentally.
VBScript.vbs
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Exec("C:\Putty\putty.exe -ssh <username>#<ip> -pw <password> -P <port> -m ""E:\putty\RemoteCommands.txt"" -t")
RemoteCommands.txt
sudo -S <<< "<password>" ls
sudo -i
/bin/bash
BatchFile.bat to run vbscript easily
#echo off
start cmd /k "cd /d E:\putty & cscript VBScript.vbs & exit"
Edited
To run commands after sudo login you can write something like this
sudo -i -- bash -c 'cmd1; cmd2' or sudo -i -- bash -c 'cmd1 && cmd2'
I found this workaround from this link & with random tries. so i got no explanation about this :D ... if any one know detail about this please edit this answere & provide links
RemoteCommands.txt
sudo -S <<< "<password>" ls
sudo -i -- bash -c 'cd /home/shajji && npm start && /bin/bash'
/bin/bash

Related

Why my batch file stops working on Windows 10?

I have a batch file which calls .xlsm, access and sqlcmd.
I scheduled the batch file in task scheduler for every day.
When we used Windows 7 on computer, it worked perfectly.
After upgrading to Windows 10, most of the cases it stops running after opening .xlsm.
The command prompt windows is opened, but nothing happens.
The mistake is not with the macro, because on other computer (with Windows 7) it runs perfectly.
First I've tried this code in my batch file:
"\\xy\Foldername\KillExcel.bat"
"\\xy\Foldername\SpoolfileConvert.xlsm"
sqlcmd -S servername -d SCM -i "\\xy\Foldername\DeleteTable.sql" -o "\\xy\Foldername\DeleteTable.txt"
"\\xy\Foldername\UploadData.accdb"
sqlcmd -S servername -d SCM -i "\\xy\Foldername\Data_cleaning.sql" -o "\\xy\Foldername\Data_cleaning.txt"
Thanks for the information, i tried the following code.
I deleted all the not neccessary call commands from my code. Keeped the first one, because it calls another batch.
call "\\xy\Foldername\KillExcel.bat"
start "Excel" /wait /min Excel.exe "\\xy\Foldername\SpoolfileConvert.xlsm"
sqlcmd -S servername -d SCM -i "\\xy\Foldername\DeleteTable.sql" -o "\\xy\Foldername\DeleteTable.txt"
start "Access" /wait /min MSACCESS.exe "\\xy\Foldername\UploadData.accdb"
sqlcmd -S servername -d SCM -i "\\xy\Foldername\Data_cleaning.sql" -o "\\xy\Foldername\Data_cleaning.txt"
The Excel and Access closes themselves automatically. But the sqlcmd should not run before the Excel and Access finish their process.

How to I run a single sql command from the sqlcmd prompt?

In SQL Server, if I want to run a script from the command line, I can do this
/opt/mssql-tools/bin/sqlcmd -S $DB_HOST -U $DB_USER -P $DB_PASS -d $DB_NAME -i myscript.sql
Is it possible to run just a single command without a script and get the results? For instance, if I just wanted to run
SELECT 1
How would I do that from the command line?
I think you want the -q switch:
sqlcmd -S localhost -U MyUser -P MyPass -d MyDb -q "SELECT 1"
Documentation is here
Given that you want to:
run just a single command without a script and get the results
Another possible answer would be to run:
sqlcmd -S localhost -U MyUser -P MyPass -d MyDb -Q "SELECT 1"
That is with a capital -Q.
This will run the command and then exit, allowing you to read the error code result.
The answer above uses lowercase -q, which will run the command but leave the sqlcmd prompt open and running.
Depending on what you want, the case of the -Q/-q argument matters.

How can I install vs-code-server manually and tell vs-code-remote?

When I try use remote-ssh connect to my server to install install vs-code-server, it hangs with these message:
Install and start server if needed
bash: no job control in this shell
Installing...
Downloading with wget
It seems my server cannot use wget to download vs-code-server.
Can I install vs-code-server manually?
Download your current used version via
wget https://update.code.visualstudio.com/commit:c3f126316369cd610563c75b1b1725e0679adfb3/server-linux-x64/stable
You can check the commit id in vscode Help -> About
Copy it to your machine through ssh.
Unpack to ~/.vscode-server/bin/c3f126316369cd610563c75b1b1725e0679adfb3
And you're done
I used this bash script on my linux container and it works fine. You can try this too.
read -p 'What commit of vscode server do you wish to install? ' commit
echo ""
if [ ! -d "$HOME/.vscode-server/bin/$commit" ] ; then
mkdir -p install-vscode-server
cd install-vscode-server
wget -q https://update.code.visualstudio.com/commit:$commit/server-linux-x64/stable
tar -xf stable
mkdir -p ~/.vscode-server/bin
mv vscode-server-linux-x64 ~/.vscode-server/bin/$commit
cd ..
rm -rf install-vscode-server
echo "vscode server commit:$commit installed"
else
echo "Commit already installed"
fi
echo ""
This problem is caused by your terminal shell path isn't configured rightly.
Follow this issue
https://github.com/microsoft/vscode-remote-release/issues/220#issuecomment-490374437
Check which shell you are using: which $SHELL

Quickly shut down a remote server with batch file

I know how to use a shut down command (shutdown -r -f -f 0) to shut down a computer but I am trying to create a batch file to shut down remotely a computer/server in case of emergency.
Thanks.
Amir
shutdown with "/m" parameter
shutdown.exe /m \\hostname -s -t 0
or
wmic.exe OS Where Primary=TRUE Call Win32Shutdown 1
or psshutdown.exe from Sysinternals
or psexec.exe \\hostname -u [user] -p [password] -accepteula shutdown.exe -s -t 0
https://technet.microsoft.com/en-us/sysinternals/pxexec.aspx
https://technet.microsoft.com/en-us/sysinternals/psshutdown.aspx

Layout of command output executed automatically in PuTTY from a batch file is broken

I have followed this question to build a batch file to run the PuTTYwith my username and password:
How to run a command file in PuTTY using automatic login in a command prompt?
#echo off
START putty.exe -ssh [domain] -l [username] -pw [password] -m code.txt
#echo
And the PuTTY will try to run the code.txt file, which have the following code:
HResults -p -e "???" sil -e "???" sp -L labels/test lib/words3 results/*.rec
read
It will show a matrix. I try to run the batch file, it is able to open PuTTY, login and run the command in text file. But the output in PuTTY terminal is a mess. The layout of output is fine, when I doing those things manually. Is that mean some kind of setting is missing? It's not making any sense a batch file will change the output of another application...... Thanks
The -m switch imply a non-interactive session. While, when logging in manually, an interactive mode is used by default.
It may fundamentally affect output of some applications.
Try forcing the interactive mode using the -t switch:
START putty.exe -ssh [domain] -t -l [username] -pw [password] -m code.txt

Resources