Answering a telnet menu through a batch or shell script - shell

I can connect to Lexmark printers via telnet to access the configuration menu and I'm trying to find a way to configure them scripting the commands (there is more than 200 printers).
After successful connection here is the menu in the telnet window :
MAIN MENU
1. Set IP address Options
2. Set IPv6 address Options
3. Set IP Protocol enables
4. Set MTU......................................... ( 1500 )
5. Set restricted server list
6. Set lpd options
7. Set SNMP community name......................... ( public )
A. Save Changes
X. Exit current menu
Selection: 1
Is there a way I can select for example the option 1 via a batch or shell script even if this is a telnet connection ?

On windows, you could run Telnet scripting tool by this command: "%_path%\TST10.exe" /r:"%_path%\telnet.txt" /o:%_out_file% with pre-created telnet.txt file:
echo %hostname% 23>telnet.txt
echo WAIT "Selection:">>telnet.txt
echo SEND "1\m">>telnet.txt
Description of telnet.exe file: First line establish connection to host, second line insctruct program to wait till string "Selection:" was received (this should guarantee whole menu was drawed in screen) and last line send keypress "1" and CRLF. Than you can continue by same way with another possible sub-menu screens.....

Related

Edit file until the selected port will be not used by another application

I wrote a simple script that asks a user to edit the file until the moment when the user will enter the unused port. But it doesn't work (
while ! nc -z localhost $port; do
echo port is already used. Please choose another one.; read -p "Press enter to edit configuration file"; nano config.env;
done
$port it is an environment that the script takes from the current configuration file.
While loop doesn't start at all.
I have tried to run the script with ! and without ! the result is the same, looks like something wrong with the condition expression.
In order to test your scenario, I started an Apache httpd server on my laptop. It listens on port 80. Therefore nc -z localhost 80 will exit with status code 0.
This is my script:
#!/bin/bash
port=80
while nc -z localhost $port
do
echo "Port is already used. Please choose another one."
read -p "Press enter to edit configuration file or CTRL-C to terminate."
/usr/bin/nano config.env
wait
echo ""
done
To simplify, I set port to 80 statically in my script. Your version would read the port from config.env
When the user presses enter, the read completes, and the editor is called.
While the user edits the file, you want the script to wait. So until the editor is closed, the script does nothing. No point checking again for the port anyway.
When the editor is closed, wait is done and another loop starts.
I added "or CTRL-C to terminate" to give the user an out of the loop and the script.
For readability, you should put your commands on separate lines. Long series of commands on a single line, separated by ; can be confusing, and it serves no purpose. Carriage returns are not expensive :)

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

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.

Applescript determine Internet reachability

I am recently experiencing unstable Internet connection I just want to make a program with applescript that reminds me when my Internet is reachable so I can go online. Basically the program checks the Internet by pinging google.com for example. I know I can run shell script using applescript, but the problem is, how to get the return value of the ping and put it in an if statement?
Pure AppleScript solution:
set testIP to chkUP("http://www.apple.com") or chkUP("http://www.google.com")
if testIP then
display dialog "Internet Connection is UP"
else
display dialog "Internet Connection is DOWN"
end if
to chkUP(theURL)
return (count (get ((theURL as URL)'s host & {dotted decimal form:""})'s dotted decimal form)) > 0
end chkUP
I've added two scripts.
The first one checks your internet connection once.
The second runs in the background every minute and displays a popup if it can reach the internet.
Script 1
try
set thePing to do shell script "/sbin/ping -o -c 5 8.8.8.8"
on error
set thePing to "no internet connection"
end try
Script 2
(*
1. Save as an Application: Script Editor > File > Export… > File Format: Application
2. Check "Stay open after run handler"
3. Run the app or add it to your login items: System Preferences > Users & Groups > User > Login Items > Press the "+" button
# http://stackoverflow.com/questions/43250408/applescript-determine-internet-reachability
*)
on idle
try
set thePing to do shell script "/sbin/ping -o -c 5 8.8.8.8"
if thePing does not contain "error" then
display dialog "The internet connection is up"
end if
on error
set thePing to "error: no internet connection"
end try
delay 60
end idle

Change PING script to TELNET script?

I need to turn this ping script into a telnet script which I'd like to configure the script to telnet to the address (on my separate target list *.txt file) & either:
connect/disconnect - write success results to file
or
fail - write fail results to file,
& go to next record, then end...
HELP? :)
#echo off
cls
echo Ping test in progress...
for /F %%i in (iplist.txt) do ping -n 3 -a %%i >> result.txt
echo .
echo .
echo Result is ready.
You cannot simply replace ping with telnet. For one thing, the telnet shipped with Windows isn't scriptable in the first place, so you'd have to jump through some hoops to make it work in a script. You'd be better off using a telnet that's actually scriptable, like plink from the PuTTY suite. Also telnet clients can talk to arbitrary services, so you need to specify where you want to connect to (a telnet server uses a different protocol than, say, a web server or a mail server).

Preparation of shell script

Recently I have been involved for the preparation of the shell script on SunOS with csh shell. I will have multiple queries but first the short program isn't working.
[username]% expect - << EOF
Spawn telnet 74.125.71.103
expect "Password:"
send "google\r"
EOF
The following error pops up:
/bin/csh: Event not found
[username]% expect: Command not found
Please advise.
This script should run in following manner :
Telent the IP and use the existing passwd (explicitly given in the script).
After the telnet, it shows MENU options
MB station
RC
ODU
AP
SU
Exit
type 1 // a "MB station" MENU options will open i.e.
1 - Show
2 - Unit Control
type 2 // UC MENU options will open i.e
1 - Change Password
2 - Reset
type 1 //change passwd MENU options will open i.e.
1 - Change PC Password
2 - Change LU Password
3 - Change Admin Password
type 3 // to change ADMIN passwd
MB station - Change Admin Password
Enter New Password : XYZ enter
Re-enter Password : XYZ enter
New password accepted
3 times escape // to escape from telnet
1.MB station
2. RC
3. ODU
4. AP
5. SU
6. Exit
type 6 // to exit
Exit? [Y/N] y
Connection to host lost.
then move to step with different IP. The IP values will be given by the user one-time while executing the script at the prompt e.g. ./pass-change IPs.txt
The Event not found message implies you're trying to do some kind of history substitution. This normally involves the ! character. Did you type something with a ! character in it at some point?
And expect: Command not found means just what it says: the shell wasn't able to find the expect command. Is it installed? If not, you should install it if you can, or ask a system administrator to install it for you, or, failing that, obtain the source and build and install it under your home directory.
Once you fix that, there's an expect command called spawn, and it's case-sensitive; Spawn won't be recognized.
If you're specifically asking how to accomplish the above without using expect, please update your question to make that clear.
I have a working script below, you are getting error because i believe you are trying to run the script using some sh or other command. Try to run it as ./script. I used below sample script for file transfer but i did teh key exchange manyally by ssh back and forth which is one time activity.
#!/usr/bin/expect -f
set timeout 130
spawn ssh "idk#server.com"
expect "password: "
send "pass#1234\r"
expect "$ "
send "sh /home/nathalok/HTML/run.sh\r"
expect "$ "
send "exit\r"

Resources