Translating Linux shell script to Windows10 MSDOS or Powershell
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Translating Linux shell script to Windows10 MSDOS or Powershell
Hello,
I managed to create a Linux shell script that is able to switch between two different wifi services that very often hang but which can be manually reconnected with success.
Code:
while (true) do
if ! [ "`ping -c 1 -W 1 www.google.it`" ]; then
if (nmcli connection show --active | grep -c "FirstWiFi"); then
nmcli connection up id "SecondWiFi"
echo "switched to SecondWiFi"
else
nmcli connection up id "FirstWiFi"
echo "switched to FirstWiFi"
fi
fi
done
I am not suggesting to use this script because I do not know if it safe.
Now I need the same functionality on a Windows10 system.
I know that MSDOS shell is more limited than the bash shell or the like. But Windows10 has also Powershell.
As you can see the script is very simple so I think it can be translated. I do not know how to do it.
Maybe some easy suggestions can be found here.
Thank you in advance
Hello,
I managed to create a Linux shell script that is able to switch between two different wifi services that very often hang but which can be manually reconnected with success.
Code:
while (true) do
if ! [ "`ping -c 1 -W 1 www.google.it`" ]; then
if (nmcli connection show --active | grep -c "FirstWiFi"); then
nmcli connection up id "SecondWiFi"
echo "switched to SecondWiFi"
else
nmcli connection up id "FirstWiFi"
echo "switched to FirstWiFi"
fi
fi
done
I am not suggesting to use this script because I do not know if it safe.
Now I need the same functionality on a Windows10 system.
I know that MSDOS shell is more limited than the bash shell or the like. But Windows10 has also Powershell. As you can see the script is very simple so I think it can be translated. I do not know how to do it. Maybe some easy suggestions can be found here.
Questions about Windows scripting would be much better on a Windows forum. While SOMEONE here may know an answer, you're far more likely to get help for Windows on a Windows forum. Much like your other thread where you didn't appear to look for any examples/samples, putting "windows powershell wifi script" into Google pulls up over 1.9 MILLION hits, with sample scripts and examples.
Safeness can be about the opportunity to have some script on the system in regard to performance or other issues, or about polling the network that way and so on.
Why do you answer questions when you have so poor technical knowledge?
We are all tired of you. No amusing at all anymore your histrionic and/or compulsive disorder.
Please reconsider subscribing to Quora to explain your experience.
Now I go writing on StackOverflow, please do not use your fake alias to answer me there or I will report you.
@TB0ne
Safeness can be about the opportunity to have some script on the system in regard to performance or other issues, or about polling the network that way and so on.
You wrote it; why don't you tell us what's unsafe about it then?
Quote:
Why do you answer questions when you have so poor technical knowledge?
If you say so.
Quote:
We are all tired of you. No amusing at all anymore your histrionic and/or compulsive disorder. Please reconsider subscribing to Quora to explain your experience. Now I go writing on StackOverflow, please do not use your fake alias to answer me there or I will report you.
Go wherever you'd like; if you think people are stalking you, seek help, as there's not much anyone on a technical forum can do for you.
AGAIN: you show no effort of your own, despite being told to numerous times. You post a script that you copied/pasted, and changed a small bit of and claimed it as your own, and now you want us to 'translate' the script to Windows for you. Again: 1.9+ million hits with examples for powershell wifi scripts...not sure how much more 'help' you need.
To sum up, I am here to be helped, not to demonstrate my good will. Your help was very useful to me, so you have to decide whether to drop the false expectations and help me further as a gift.
Now I need the same functionality on a Windows10 system.
Surely, it would have made more sense to ask in a Windows forum, but anyway...
First, you need to obtain the "profile name" associated with each SSID. And what may that be, you ask? Just an identifier Windows uses; run netsh wlan show profiles to see. On my system, the "profile" name is identical to the SSID.
Now put your profile names and SSIDs in this cmd script:
Code:
@echo off
set wlanssid1=FirstWiFi
set wlanprofile1=ProbablyAlsoFirstWiFi
set wlanssid2=SecondWifi
set wlanprofile2=ProbablyAlsoSecondWiFi
rem *** the cmd version of a "while true" loop
for /l %%i in (0,0,0) do (
ping -n 1 -w 1 www.google.it >nul
if errorlevel 1 (
set currentwlan=%wlanssid2%
netsh wlan show interfaces | find " SSID" >nul
if not errorlevel 1 (
for /f "tokens=2 delims=:" %%b in ('netsh wlan show interfaces ^| find " SSID"') do set currentwlan=%%b
set currentwlan=%currentwlan: =%
netsh wlan disconnect >nul
rem *** a pause is needed here, otherwise the connect
rem *** operation below may fail
ping -n 2 127.0.0.1 >nul
)
if "%currentwlan%" == "%wlanssid1%" (
netsh wlan connect ssid=%wlanssid2% name=%wlanprofile2% >nul
)
if "%currentwlan%" == "%wlanssid2%" (
netsh wlan connect ssid=%wlanssid1% name=%wlanprofile1% >nul
)
)
rem *** 5-ish second pause
ping -n 6 127.0.0.1 >nul
)
Now, who said the Windows cmd shell was limited? (Actually I say that all the time, but it's still capable of performing quite advanced tasks.)
Note that if you have more than one WLAN interface in your computer, all the netsh commands require an extra "interface=" parameter.
Just to get back on point:
Surely, it would have made more sense to ask in a Windows forum, but anyway...
OP was told that previously.
Quote:
First, you need to obtain the "profile name" associated with each SSID. And what may that be, you ask? Just an identifier Windows uses; run netsh wlan show profiles to see. On my system, the "profile" name is identical to the SSID. Now put your profile names and SSIDs in this cmd script:
Code:
@echo off
set wlanssid1=FirstWiFi
set wlanprofile1=ProbablyAlsoFirstWiFi
set wlanssid2=SecondWifi
set wlanprofile2=ProbablyAlsoSecondWiFi
rem *** the cmd version of a "while true" loop
for /l %%i in (0,0,0) do (
ping -n 1 -w 1 www.google.it >nul
if errorlevel 1 (
set currentwlan=%wlanssid2%
netsh wlan show interfaces | find " SSID" >nul
if not errorlevel 1 (
for /f "tokens=2 delims=:" %%b in ('netsh wlan show interfaces ^| find " SSID"') do set currentwlan=%%b
set currentwlan=%currentwlan: =%
netsh wlan disconnect >nul
rem *** a pause is needed here, otherwise the connect
rem *** operation below may fail
ping -n 2 127.0.0.1 >nul
)
if "%currentwlan%" == "%wlanssid1%" (
netsh wlan connect ssid=%wlanssid2% name=%wlanprofile2% >nul
)
if "%currentwlan%" == "%wlanssid2%" (
netsh wlan connect ssid=%wlanssid1% name=%wlanprofile1% >nul
)
)
rem *** 5-ish second pause
ping -n 6 127.0.0.1 >nul
)
Now, who said the Windows cmd shell was limited? (Actually I say that all the time, but it's still capable of performing quite advanced tasks.) Note that if you have more than one WLAN interface in your computer, all the netsh commands require an extra "interface=" parameter.
Yep...there are also TONS of sample scripts that do such things that the OP could have found (over 1.9 million, if they looked, the first one from Microsoft with a sample script), and based on their posting history, I was very reluctant to do their work for them.
This goes back to several of their previous threads, where they keep getting dropped from a free wifi connection...and their 'legal team' is going after them for 'fraud' (?). And whatever the situation, it has also been pointed out to them previously:
They DO NOT get dropped from their pay-for wifi connection
They DO get dropped from the free connection
They've been told that a well-managed network that supports a ton of users (like, say, a free public wifi?), has rules to prevent abuse. Sending a constant string of ping packets over and over, and instantly reconnecting from one to another over and over again, would most probably be the CAUSE of getting dropped. I've seen network rules that are far stricter than that, but they don't seem to take that into account.
@P5music - This appears to be closely related to your other threads here and here. Duplicate threads are not allowed per the LQ Rules, but it will remain separate so long as the discussion is confined to that of the translation to a different shell language.
It would be helpful to others and thus, in keeping with the LQ guidelines if you could provide a little more information about why you think the script might be unsafe, and show any effort you have made at translating it.
Please review the Site FAQ for guidance in posting your questions and general forum usage. Especially, read the link in that page, How To Ask Questions The Smart Way. The more effort you put into understanding your problem and framing your questions, the better others can help!
Also, note in the above links, personal attacks on other members are not tolerated. Please review your posts in this thread and consider an edit where appropriate.
@everyone - Please keep this discussion strictly on topic - translation of this script to another shell language - and let it stand on its own merits without repeated references to other threads and past deeds.
@astrogeek
Rules are applied at convenience here, I do not think it is possible to use this old fashioned website with trolls ruling every thread.
Your intervention is very late and it was me to be repeatedly harassed. If you are accustomed to other badly-behaving members should not be a moderator. Please consider quitting.
@astrogeek
Rules are applied at convenience here, I do not think it is possible to use this old fashioned website with trolls ruling every thread.
Your intervention is very late and it was me to be repeatedly harassed. If you are accustomed to other badly-behaving members should not be a moderator. Please consider quitting.
This ad hominem post has been reported.
To the subject of the thread: One does not "translate" a (assumed) bash script to Windows, one just has to re-write it.
As suggested, perhaps you need to seek help on a Windows or PowerShell forum. I suspect you won't get any further help here.
Distribution: Debian, Red Hat, Slackware, Fedora, Ubuntu
Posts: 13,602
Rep:
P5music,
As the LQ Rules note, "We are proud of the fact that despite of our growing numbers we continue to remain extremely friendly to both the newbie and the expert". This applies to all members. Please keep this in mind if you'd like to continue participating here.
@P5music - Please review the Site FAQ and LQ Rules for guidance in acceptable use of these forums.
From the LQ Rules:
Quote:
* Do not post any messages that are obscene, vulgar, sexually-orientated, hateful, threatening, hostile or insulting.
* Personal attacks on others will not be tolerated.
If you have other questions not answered in those links please contact us through contact links available on any page, or post to the Suggestions and Feedback forum.
If you have a complaint about another member's behavior, or want to dispute a moderator's decisions, the proper action is to use the Report button at bottom of the offending post to ask for a review. Resort to name calling and insults is not acceptable.
I specifically asked...
Quote:
Originally Posted by astrogeek
@everyone - Please keep this discussion strictly on topic - translation of this script to another shell language - and let it stand on its own merits without repeated references to other threads and past deeds.
As you have chosen to ignore that request, this thread is now closed.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.