How to open teraterm SSH/Telnet connection from windows command prompt? - cmd

I got a solution to open a teraterm COM connection and run a macro -
"C:\Program Files (x86)\teraterm\ttermpro.exe" /I /C=7 /BAUD=115200 /M="E:\old data\Desktop\TTL\RvR\test.ttl"
However, need help on arguments to be given to open a SSH/Telnet connection to a particular host from cmd using teraterm and run a macro.

here is 100 percent working code for telnet to a remote host ip
send username password and a command on remote machine.
If you want to send any argument x as variable inside u can use VAR %x and %x% as its value
contents of test.ttl file in local folder as follows
start copy from next line by chanimg ip username and password
connect 'IP:23'
pause 2
sendln 'Remoteusername'
pause 2
sendln 'Remotepassword'
pause 2
logopen 'C:\Users\nocbb\test.txt' 0 0
sendln 'command on remote host'
pause 3
sendln 'logout'
logclose
end
copy till above line
Save as all files and test.ttl and
run in dos command
run using command
C:\Users\nocbb>"C:\Program Files (x86)"\teraterm\ttermpro /M=C:\Users\nocbb\test.ttl /nossh
Note Change the path as per installation folder of Teraterm in
ur installation of teraterm folder.

Related

Scheduled batch file not running

I have a .bat file. If I run it manually (double click), it works. If I schedule it, it doesn't.
I tried solutions from similar questions on StackOverflow but none of them is working. Like:
Batch not scheduled
WIN 10 task scheduler
I changed general options.
Run no matter user connection
I changed the user starting the scheduled task.
Mine, BTW, has maximum permissions and when double clicked, it works.
I allowed maximum permissions.
Modified "Add argument" and "Start in"
I set no conditions in the "conditions" tab
I'm new to scripting, so it is possible I set something wrong.
That's my code:
#echo off
"C:\Program Files (x86)\WinSCP\winscp.com" /log=winscp.log /ini=nul /command ^
"open sftp://user:password#mySite.com -hostkey=""ssh-rsa 4096 xxxxxxxxx/yyyyw=""" ^
"option batch" ^
"option transfer binary" ^
"synchronize remote -filemask=""*.png>=2016-01-01<4M;*.jpg>=2016-01-01<4M "" \\FromFolder /destination/ -nopreservetime"
"exit"
If I put md C:\Temp & echo %TIME%>C:\Temp\Test.txt as 2° row, it writes the Test.txt file. If i put it in the end of the file it doesn't. No problems if i run it by double click.
I need the task to run the .bat file just as if I am double clicking it. I don't know if is it possible to open the shell or run it in background, as both of them are good for me. Let me know.
Thanks for suggestions.
Edit_1)
I removed the echo %TIME% >> C:\Temp\Test.txt and kept the "exit" >> C:\Temp\Test.txt .
If I double click my bat file, this is the log:
Host Search ...
Host connection ...
Authentication ...
Use username "xxxx".
Authentication with preset password
Authenticated.
Starting session ...
Session started.
Active session: [1] xxx#site.com
batch abort
transfer binary
Comparison...
Local 'file\MyFolder1' => remote '/ images'
Nothing to synchronize
Comparison...
Local 'H:\MyPath\MyFolder2' => remote '/ images'
Synchronization...
Local 'H:\MyPath\MyFolder2' => remote '/ images'
H:\MyPath\MyFolder2 image.JPG | 617 KB | 50.4 KB / s | binary | 100%
And it works perfectly.
If i run it from schedule (using my same user)
this is the log.
Host Search ...
Host connection ...
Authentication ...
Use username "xxxx".
Authentication with preset password
Authenticated.
Starting session ...
Session started.
Active session: [1] xxx#site.com
batch abort
transfer binary
Comparison...
Local 'file\MyFolder1' => remote '/ images'
Nothing to synchronize
Comparison...
Local 'H:\MyPath\MyFolder2' => remote '/ images'
Folder List Error Request 'H:\MyPath\MyFolder2\*.*'.
Error retrieving file list for "H:\MyPath\MyFolder2\*.*'.
System error. Code: 3.
The specified path could not be found
(I) nterrupt, (R) etry, (S) top: Stop
Folder List Error Request 'H:\MyPath\MyFolder2\*.*'
Error retrieving file list for "H:\MyPath\MyFolder2\*.*'
System error. Code: 3.
The specified path could not be found
I schedule the bat file using my user and max privileges so it should find the folder in the H:\ disk.
WinSCP most probably fails. I would guess that because the batch file working directory is not hat you think and winscp.log cannot be created there. Try using a path like for your debug test: C:\Temp\winscp.log.
The reason why the md C:\Temp & echo %TIME%>C:\Temp\Test.txt does not work at the end of your batch file is the exit command. You probably believe it's interpreted as WinSCP command. But it's not. You are missing ^ after the synchronize command. And additionally, you have a blank space after it. As a consequence, the exit is not part of WinSCP command-line, but it exits the batch file instead. Also the lines have to be indented (though I assume that it's rather due to your post formatting).
Try something like this:
#echo off
md C:\Temp
echo %TIME% > C:\Temp\Test.txt
"C:\Program Files (x86)\WinSCP\winscp.com" /log=C:\temp\winscp.log /ini=nul /command ^
"open sftp://user:password#mySite.com -hostkey=""ssh-rsa 4096 xxxxxxxxx/yyyyw=""" ^
"option batch" ^
"option transfer binary" ^
"synchronize remote -filemask=""*.png>=2016-01-01<4M;*.jpg>=2016-01-01<4M "" \\FromFolder /destination/ -nopreservetime" ^
"exit" >> C:\Temp\Test.txt
echo %TIME% >> C:\Temp\Test.txt
See also WinSCP FAQ Why are some WinSCP scripting commands specified in a batch file not executed/failing?

How to redirect Windows cmd output to a text file?

I am trying to monitor the cmd output of a certain executable .exe and use it for another process running at the same time.
The problem is that all cmd redirecting functions ( '>','>>','<' and '|') would only redirect the output after a successful return of the last command.
What I wanted to do is to generate some kind of streaming log of my cmd.
You can run in your process in background by using
start /b something.exe > somefile.txt
Windows 20H2: Redirection works fine when logged in as true administrator but will NOT work when logged in as a created administrative user. I have used it for years to redirect the output of a multi target backup system using Hobo copy to put the console output in a log file. I have never been able to get it to work successfully in Windows 10 ver 19 or 20 on a created administrative user.
You can prefix the command with "cmd /c" to start a new command prompt, and redirect the output of the command prompt:
cmd /c YOUR CODE > TextFileName.txt
Note : Use ONLY single greater than (>)
Since the output of cmd is going to TextFileName.txt.
Whereas this misses the error output, so you are not able to see : Request to UnKnown timed-out for each failed address.

Using ftp.exe to create some kind of log [duplicate]

I am writing a batch command to send data via FTP. Before sending the actual data I need to find if the FTP server is active/running. How do I check that in batch command?
The server responds with "220 server ready" message when it is connected.
Do something like this:
YourFTPCommand | find /i /v "220 server ready" && goto :ServerNotReady
Explanation:
Pipe the output of your FTP command to FIND
Do a case insensitive (/i) search for output that does not contain (/v) the string "220 server ready"
Go to someplace if such a string is found (&&)
I do not think there's a reliable way to do this with Windows ftp.exe.
It blindly keeps running the commands, no matter if connection or previous commands succeeded.
It won't even report the result via exit code.
All you can do is to parse the ftp.exe output.
You should better use a 3rd-party FTP client.
For example with WinSCP scripting you can use:
#echo off
winscp.com /log=ftp.log /command ^
"open ftp://user:password#example.com/" ^
"put c:\local\path\file.txt" ^
"exit"
If connection (the open command) fails, WinSCP won't execute the following commands (the put).
See also Converting Windows FTP script to WinSCP FTP script.
(I'm the author of WinSCP)

Writing pings to files batch

Im looking for the best way to write two instances of cmd ping commands to 2 seperate text files using a batch file.
Here is what I have but its not writing to the file im wondering why.
start cmd /k ping 8.8.8.8 -t >> c:\troubleshoot_connection_google.txt
start cmd /k ping 192.x.x.x -t >> c:\troubleshoot_connection_gateway.txt
Thanks for the help.
The problem with your commands, appart from the point that the root folder of the drive is usually not writteable, is when the redirection to the output file is done.
As you have it written, what is being redirected to the file is the output of the start command, but if you need to redirect the output of the ping command, the redirection must be part of the started command
start "" "cmd /c ping 8.8.8.8 -t >> c:\troubleshoot_connection_google.txt"

cmd terminate application after specified time of execution

I installed linphone application and i am writing a bat file doing a loop through file to make calls through executing this command
echo call %%H%%Z#%%G^|"C:\Program Files (x86)\Linphone\bin\linphonec.exe"
Now the loop is working fine and everything is ok but the problem that linphone is not terminating after making the first command to move on for the remaining of the loop , when i try to use linphone directly thorough the cmd command it remain open until i use quit command like this
C:\Users\administrator>cd C:\Program Files (x86)\Linphone\bin
C:\Program Files (x86)\Linphone\bin>linphonec -s 111#1.1.1.1
WARNING: no real random source present!
Ready
Warning: video is disabled in linphonec, use -V or -C or -D to enable.
linphonec> Establishing call id to sip:111#1.1.1.1, assigned id 1
Contacting sip:111#1.1.1.1
linphonec> Call 1 to sip:111#1.1.1.1 in progress.
linphonec> quit
Terminating...
Call ended
linphonec> Call 1 with sip:111#1.1.1.1 ended (No error).
No response.
linphonec>
C:\Program Files (x86)\Linphone\bin>
how i can terminate the program in my command above after executing the call command ?
I have not linphone to test, but if the program accepts piped commands, maybe, this could work
(echo call %%H%%Z#%%G&echo quit)|"C:\Program Files (x86)\Linphone\bin\linphonec.exe"
edited to make it wait
(
echo call %%H%%Z#%%G
ping -n 11 localhost >nul 2>nul
echo quit
) | "C:\Program Files (x86)\Linphone\bin\linphonec.exe"
or
(echo call %%H%%Z#%%G& ping -n 11 localhost >nul 2>nul & echo quit)|"C:\Program Files (x86)\Linphone\bin\linphonec.exe"

Resources