I am unclear what I am doing wrong here or is there some type of bash issue?
I am declaring some static sting arrays and interactively building up another array of string values, all to be passed to a bash function. I get corruption of the array values it seems inside the function itself.
add_firewall_rich_rules() {
node_ips="${1}"
swarm_tcp_ports="${2}"
swarm_udp_ports="${3}"
echo "in: ${node_ips[#]}"
echo "in: ${swarm_tcp_ports[#]}"
echo "in: ${swarm_udp_ports[#]}"
for ip in "${node_ips[#]}"
do
for tcp_port in "${swarm_tcp_ports[#]}"
do
rule="'rule family=\"ipv4\" source address=\"$ip\" port protocol=\"tcp\" port=\"$tcp_port\" accept'"
cmd="firewall-cmd --permanent --zone=public --add-rich-rule=$rule"
echo "$cmd"
done
for udp_port in "${swarm_udp_ports[#]}"
do
rule="'rule family=\"ipv4\" source address=\"$ip\" port protocol=\"udp\" port=\"$udp_port\" accept'"
cmd="firewall-cmd --permanent --zone=public --add-rich-rule=$rule"
echo "$cmd"
done
done
}
declare -a swarm_tcp_ports=('2377' '7946' '4789' '8500' '4000')
declare -a swarm_udp_ports=('2377' '7946' '4789')
declare -a node_ips=()
echo "init: ${node_ips[#]}"
echo "init: ${swarm_tcp_ports[#]}"
echo "init: ${swarm_udp_ports[#]}"
node_ip=""
last_node_ip=""
while read -e -p "Enter ip of additional node in the cluster (hit enter twice to stop adding values): " -i "`echo $node_ip |sed 's/[^.]*$//'`" node_ip; do
if [ "$node_ip" == "$last_node_ip" ]; then
break
fi
if [[ $node_ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
node_ips=("${node_ips[#]}" $node_ip)
else
echo "hit return again to stop adding values"
fi
last_node_ip=$node_ip
echo "${swarm_tcp_ports[#]}"
done
if [ "${#node_ips[#]}" -gt 0 ]; then
echo "out: ${node_ips[#]}"
echo "out: ${swarm_tcp_ports[#]}"
echo "out: ${swarm_udp_ports[#]}"
add_firewall_rich_rules ${node_ips[#]} ${swarm_tcp_ports[#]} ${swarm_udp_ports[#]}
fi
From the Terminal:
# ./firewall_add_rich_rule.sh
init:
init: 2377 7946 4789 8500 4000
init: 2377 7946 4789
Enter ip of additional node in the cluster (hit enter twice to stop adding values): 192.168.1.105
2377 7946 4789 8500 4000
Enter ip of additional node in the cluster (hit enter twice to stop adding values): 192.168.1.106
2377 7946 4789 8500 4000
Enter ip of additional node in the cluster (hit enter twice to stop adding values): 192.168.1.
hit return again to stop adding values
2377 7946 4789 8500 4000
Enter ip of additional node in the cluster (hit enter twice to stop adding values): 192.168.1.
out: 192.168.1.105 192.168.1.106
out: 2377 7946 4789 8500 4000
out: 2377 7946 4789
in: 192.168.1.105 192.168.1.106
in: 192.168.1.106 7946 4789 8500 4000
in: 2377 7946 4789
firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.105" port protocol="tcp" port="192.168.1.106" accept'
firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.105" port protocol="tcp" port="7946" accept'
firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.105" port protocol="tcp" port="4789" accept'
firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.105" port protocol="tcp" port="8500" accept'
firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.105" port protocol="tcp" port="4000" accept'
firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.105" port protocol="udp" port="2377" accept'
firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.105" port protocol="udp" port="7946" accept'
firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.105" port protocol="udp" port="4789" accept'
firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.106" port protocol="tcp" port="192.168.1.106" accept'
firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.106" port protocol="tcp" port="7946" accept'
firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.106" port protocol="tcp" port="4789" accept'
firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.106" port protocol="tcp" port="8500" accept'
firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.106" port protocol="tcp" port="4000" accept'
firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.106" port protocol="udp" port="2377" accept'
firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.106" port protocol="udp" port="7946" accept'
firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.106" port protocol="udp" port="4789" accept'
It seems like the first element from the first array passed into the function is replacing the first element of the next array passed after the function call is made (and i have noticed sometimes the next array as well but not this time).
When you pass your arrays to the function, it expands the values.
arr1=( "1" "2" "3" )
arr2=( "a" "b" "c" )
someFunc "${arr1[#]}" "${arr2[#]}"
#same as someFunc "1" "2" "3" "a" "b" "c" "d"
So in your function you should take the name of the arrays rather than expanding them and declaring them as a new array inside your functions.
arrayTest(){
declare -a localArr1=("${!1}")
declare -a localArr2=("${!2}")
//process arrays
}
arr1=( "1" "2" "3" )
arr2=( "a" "b" "c" )
arrayTest "arr1[#]" "arr2[#]"
Related
I am having some issues with wireguard setup. The logic that I want to achieve is that I will be able to connect from Client 1 (laptop) to Client 2 (opensense) subnet 10.88.1.1/24.
For the moment all is OK from Client to Server but no ping or any other access between the Clients.
Current setup is
Main wireguard server (ubuntu server) IP eg. 5.123.456.678
[Interface]
Address = 203.0.113.5/24
SaveConfig = true
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
ListenPort = 51820
FwMark = 0xca6c
PrivateKey = XXXXXXXXXXXXXXXXXXXXXXX
[Peer]
PublicKey = XXXXXXXXXXXXXXXXXXXXXXX
AllowedIPs = 203.0.113.13/32
Endpoint = 89.xxx.xxx.xxx:33943
[Peer]
PublicKey = XXXXXXXXXXXXXXXXXXXXXXX
AllowedIPs = 203.0.113.15/32
Endpoint = 46.xxx.xxx.xxx:4109
Client 1 (laptop)
[Interface]
PrivateKey = XXXXXXXXXXXXXXXXXXXXXXX
Address = 203.0.113.15/24
ListenPort = 51820
[Peer]
PublicKey = XXXXXXXXXXXXXXXXXXXXXXX
AllowedIPs = 203.0.113.5/32
Endpoint = 5.123.456.678:51820
PersistentKeepalive = 5
Client 2 (opensense) with subnet 10.88.1.1.
interface: wg0
public key: XXXXXXXXXXXXXXXXXXXXXXX
private key: (hidden)
listening port: 51820
peer: XXXXXXXXXXXXXXXXXXXXXXX
endpoint: 5.123.456.678:51820
allowed ips: 203.0.113.0/24
I have done some more testing and the the logic is that you need to add
subnet or individual IP to the setting in my case setting now looks like this
Server
[Interface]
Address = 203.0.113.5/24
SaveConfig = true
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
ListenPort = 51820
FwMark = 0xca6c
PrivateKey = XXXXXXXXXXXXXXXXXXXXXXX
[Peer]
PublicKey = XXXXXXXXXXXXXXXXXXXXXXX
AllowedIPs = 203.0.113.13/32, 10.88.1.0/24
Endpoint = 89.xxx.xxx.xxx:33943
[Peer]
PublicKey = XXXXXXXXXXXXXXXXXXXXXXX
AllowedIPs = 203.0.113.15/32, 10.88.1.0/24
Endpoint = 46.xxx.xxx.xxx:4109
Client 1 (laptop)
[Interface]
PrivateKey = XXXXXXXXXXXXXXXXXXXXXXX
Address = 203.0.113.15/24
ListenPort = 51820
[Peer]
PublicKey = XXXXXXXXXXXXXXXXXXXXXXX
AllowedIPs = 203.0.113.0/24,10.88.1.0/24
Endpoint = 5.123.456.678:51820
PersistentKeepalive = 5
Client 2 (opensense) with subnet 10.88.1.1.
interface: wg0
public key: XXXXXXXXXXXXXXXXXXXXXXX
private key: (hidden)
listening port: 51820
peer: XXXXXXXXXXXXXXXXXXXXXXX
endpoint: 5.123.456.678:51820
allowed ips: 203.0.113.0/24,10.88.1.0/24
when i type fllow command: isql dsnOracle -v
i got :
[IM002][unixODBC][Driver Manager]Data source name not found, and no default driver specified
[ISQL]ERROR: Could not SQLConnect
my config file:
[root#localhost lib]# cat /etc/odbc.ini
[dsnOracle]
Description = data source to oracle
Driver = Oracle
Servername = PARA_ORACLE
port = 1521
[root#localhost lib]# cat /etc/odbcinst.ini
[Oracle]
Description = ODBC for Oracle
Driver = /usr/lib/libsqora.so.11.1
[root#localhost lib]# cat $TNS_ADMIN/tnsnames.ora
PARA_ORACLE =
(DESCRIPTION =
(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 172.100.2.13)(PORT = 1521)) )
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
[root#localhost lib]# rpm -qa |grep oracle
oracle-instantclient11.2-odbc-11.2.0.1.0-1.x86_64
oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64
[root#localhost lib]# rpm -qa |grep ODBC
unixODBC-2.2.14-12.el6_3.x86_64
i have try for a long time ,but i always get :data source name not found .
(it's there any relation with x86 or x64?).
any suggestion is fine for me.
finally i change my /etc/odbc.ini like this and it works.
[dsnOracle]
Application Attributes=T
Attributes=W
BatchAutocommitMode=IfAllSuccessful
BindAsFLOAT=F
CloseCursor=F
DisableDPM=F
DisableMTS=T
Driver=Oracle
DSN=OracleODBC-11g
EXECSchemaOpt=
EXECSyntax=T
Failover=T
FailoverDelay=10
FailoverRetryCount=10
FetchBufferSize=64000
ForceWCHAR=F
Lobs=T
Longs=T
MaxLargeData=0
MetadataIdDefault=F
QueryTimeout=T
ResultSets=T
SQLGetData extensions=F
Translation DLL=
Translation Option=0
DisableRULEHint=T
StatementCache=F
CacheBufferSize=20
UseOCIDescribeAny=F
UserID=userid
ServerName=PARA_ORACLE
Password=password
I have NMAP-output xml-file like this:
<host><status state="down" reason="no-response" reason_ttl="0"/>
<address addr="192.168.1.1" addrtype="ipv4"/>
</host>
<taskbegin task="SYN Stealth Scan" time="1457545799"/>
<taskend task="SYN Stealth Scan" time="1457545841" extrainfo="1600 total ports"/>
<host starttime="1457545794" endtime="1457545839"><status state="up" reason="echo-reply" reason_ttl="243"/>
<address addr="192.168.1.2" addrtype="ipv4"/>
<hostnames>
<hostname name="192-168-1-2.liibalaaba.com" type="PTR"/>
</hostnames>
<ports><extraports state="closed" count="100">
<extrareasons reason="resets" count="100"/>
</extraports>
</ports>
<times srtt="6454" rttvar="163" to="100000"/>
</host>
<host starttime="1457545794" endtime="1457545837"><status state="up" reason="echo-reply" reason_ttl="51"/>
<address addr="192.168.1.3" addrtype="ipv4"/>
<hostnames>
<hostname name="192-168-1-3.liibalaaba.com" type="PTR"/>
</hostnames>
<ports><extraports state="filtered" count="98">
<extrareasons reason="no-responses" count="98"/>
</extraports>
<port protocol="tcp" portid="80"></port>
<port protocol="tcp" portid="443"></port>
</ports>
<times srtt="6378" rttvar="191" to="100000"/>
</host>
<host starttime="1457545794" endtime="1457545841"><status state="up" reason="echo-reply" reason_ttl="115"/>
<address addr="192.168.1.4" addrtype="ipv4"/>
<hostnames>
<hostname name="192-168-1-4.liibalaaba.com" type="PTR"/>
</hostnames>
<ports><extraports state="filtered" count="97">
<extrareasons reason="no-responses" count="97"/>
</extraports>
<port protocol="tcp" portid="80"></port>
<port protocol="tcp" portid="81"></port>
<port protocol="tcp" portid="443"></port>
</ports>
<times srtt="6417" rttvar="113" to="100000"/>
</host>
How I can put only the hosts that have open ports to an array and then to readable format? (Do I even need an array?)
Something like this:
address:192.168.1.3 [0,0]
open ports:80 [0,1], 443 [0,2]
address:192.168.1.4 [1,0]
open ports:80[1,1], 81 [1,2], 443 [1,3]
Now i'm using xmllint xpath combo, but it just splits out every address that has open ports to same line:
`xmllint --xpath "//*[local-name()='host']/*[local-name()='address']/#addr | //*[local-name()='ports']/*[local-name()='port']/#portid" nmap-scan.xml > result.txt`
Thanks for help!
I have a program that will ping an IP address, then log the IP to a file if it pings successfully:
Invalid IP: 128.201.166.30
Proxy created: 66.25.173.128:80
Invalid IP: 225.195.111.59
Invalid IP: 249.133.221.70
Invalid IP: 40.21.11.99
Invalid IP: 201.27.136.108
Invalid IP: 152.77.109.45
Invalid IP: 120.76.159.122
Invalid IP: 108.244.67.42
Invalid IP: 73.231.16.193
Proxy created: 146.134.102.95:3128
Invalid IP: 133.216.1.59
Proxy created: 118.75.196.75:3128
Now what I would like to do is have the "good" IP address use each port in the following array: port = %w(80 3128 8080 8090 8888 8898 9999), for example:
Proxy created: 66.25.173.128:80
Proxy created: 66.25.173.128:3128
Proxy created: 66.25.173.128:8080
Proxy created: 66.25.173.128:8090
Proxy created: 66.25.173.128:8888
Proxy created: 66.25.173.128:8898
Proxy created: 66.25.173.128:9999
#Creates an IP with a port extension with each port number
I think I have a general idea on how I could do this:
File.open("example.txt", "a+"){
|s| s.puts("#{ip}:#{port[0]}",
"#{ip}:#{port[1]}",
"#{ip}:#{port[2]}"
#etc...
)}
I'm not entirely sure if that would work the way I'm expecting it to though, and even if it does, I'm 100% sure there's a better way to do, any help with this would be greatly appreciated, thank you.
Source:
require 'colored'
require 'timeout'
def create_possibles
port = %w(80 3128 8080 8090 8888 8898 9999).each do |port|
10.times do
ip = Array.new(4){rand(256)}.join('.')
Timeout::timeout(5) do
ping = `ping -n 1 #{ip}`
if ping =~ /Received = 1/
proxy = "#{ip}:#{port}"
puts "Proxy created: #{proxy}".green.bold
File.open("proxies.txt", "a+") {|s| s.puts(proxy)}
else
puts "Invalid IP: #{ip}".red.bold
next
end
end
end
end
end
create_possibles
EDIT:
I attempted my general idea of how to do this:
require 'colored'
require 'timeout'
def create_possibles
w%(80 3128 8080 8090 8888 8898 9999).each do |port|
1.times do
ip = Array.new(4){rand(256)}.join('.')
Timeout::timeout(5) do
ping = `ping -n 1 #{ip}`
if ping =~ /Received = 1/
# proxy = "#{ip}:#{port}"
puts "[SUCCESS]Proxy created for IP: #{ip}".green.bold
File.open("proxies.txt", "a+") {|s| s.puts("#{ip}:#{port[0]}",
"#{ip}:#{port[1]}",
"#{ip}:#{port[2]}",
"#{ip}:#{port[3]}",
"#{ip}:#{port[4]}",
"#{ip}:#{port[5]}",
"#{ip}:#{port[6]}",
"#{ip}:#{port[7]}")}
else
puts "[ERROR]IP failed to ping: #{ip}".red.bold
next
end
end
end
end
end
create_possibles
When run:
[ERROR]IP failed to ping: 185.105.73.104
[ERROR]IP failed to ping: 93.182.117.11
[ERROR]IP failed to ping: 112.210.73.187
[ERROR]IP failed to ping: 111.109.127.178
[SUCCESS]Proxy created for IP: 201.153.205.131
[ERROR]IP failed to ping: 128.236.57.123
[ERROR]IP failed to ping: 248.84.17.31
It ends up outputting information that looks like this:
201.153.205.131:0
201.153.205.131:0
201.153.205.131:0
201.153.205.131:1
201.153.205.131:1
201.153.205.131:1
201.153.205.131:0
201.153.205.131:1
I figured it out! The problem had to do with the .each do |ports|;
If I take that out like this:
require 'colored'
require 'timeout'
def create_possibles
ports = %w(80 3128 8080 8090 8888 8898 9999)
5.times do
ip = Array.new(4){rand(256)}.join('.')
Timeout::timeout(5) do
ping = `ping -n 1 #{ip}`
if ping =~ /Received = 1/
# proxy = "#{ip}:#{port}"
puts "[SUCCESS]Proxy created for IP: #{ip}".green.bold
File.open("proxies.txt", "a+") {|s| s.puts("#{ip}:#{ports[0]}",
"#{ip}:#{ports[1]}",
"#{ip}:#{ports[2]}",
"#{ip}:#{ports[3]}",
"#{ip}:#{ports[4]}",
"#{ip}:#{ports[5]}",
"#{ip}:#{ports[6]}")}
else
puts "[ERROR]IP failed to ping: #{ip}".red.bold
next
end
end
end
end
create_possibles
Then run it as is, it will output:
[ERROR]IP failed to ping: 111.20.77.200
[SUCCESS]Proxy created for IP: 217.252.149.35
[ERROR]IP failed to ping: 49.214.128.47
[ERROR]IP failed to ping: 116.101.28.115
[ERROR]IP failed to ping: 75.49.120.242
And will log:
217.252.149.35:80
217.252.149.35:3128
217.252.149.35:8080
217.252.149.35:8090
217.252.149.35:8888
217.252.149.35:8898
217.252.149.35:9999
I am creating a WinForms application to start and stop an OpenVPN connection on Windows. I am trying to achieve the same functionality as OpenVPN GUI for Windows (http://openvpn.se/) provides but using my own .NET based UI.
I am starting the connection using the following approach:
Process openVpnProcess = new Process();
openVpnProcess.StartInfo.CreateNoWindow = true;
openVpnProcess.EnableRaisingEvents = true;
openVpnProcess.StartInfo.Arguments = "--config client.ovpn";
openVpnProcess.StartInfo.FileName = "openvpn.exe";
openVpnProcess.StartInfo.WorkingDirectory = #"C:\Program Files\OpenVPN\config";
openVpnProcess.Start();
This invokes openvpn.exe and the connection is established successfully.
I am however unable to determine a way to terminate the connection once it is established. I have tried using Process.Kill()
foreach (var p in Process.GetProcessesByName("openvpn"))
{
p.Kill();
}
This kills the process, but does not restore the initial routing state. Effectively, I cannot access the network until I manually disable/enable my LAN card.
Output of 'openvpn --show-net' before the VPN connection is established:
SYSTEM ROUTING TABLE
0.0.0.0 0.0.0.0 10.31.0.254 p=0 i=1376258 t=4 pr=3 a=21 h=0 m=1/-1/-1/-1/-1
10.31.0.0 255.255.240.0 10.31.10.235 p=0 i=1376258 t=3 pr=2 a=26 h=0 m=20/-1/-1/-1/-1
10.31.10.235 255.255.255.255 127.0.0.1 p=0 i=1 t=3 pr=2 a=26 h=0 m=20/-1/-1/-1/-1
10.255.255.255 255.255.255.255 10.31.10.235 p=0 i=1376258 t=3 pr=2 a=26 h=0 m=20/-1/-1/-1/-1
127.0.0.0 255.0.0.0 127.0.0.1 p=0 i=1 t=3 pr=2 a=116753 h=0 m=1/-1/-1/-1/-1
224.0.0.0 240.0.0.0 10.31.10.235 p=0 i=1376258 t=3 pr=2 a=26 h=0 m=20/-1/-1/-1/-1
255.255.255.255 255.255.255.255 10.31.10.235 p=0 i=1376258 t=3 pr=2 a=26 h=0 m=1/-1/-1/-1/-1
255.255.255.255 255.255.255.255 10.31.10.235 p=0 i=1441796 t=3 pr=2 a=4 h=0 m=1/-1/-1/-1/-1
SYSTEM ADAPTER LIST
TAP-Win32 Adapter V8
Index = 1441796
GUID = {013AB57F-DFE6-4FD9-B25E-9589E77DA4EB}
IP = 0.0.0.0/0.0.0.0
MAC = 00:ff:01:3a:b5:7f
GATEWAY =
DHCP SERV = 172.16.0.0
DHCP LEASE OBTAINED = Tue Jul 07 16:35:20 2009
DHCP LEASE EXPIRES = Wed Jul 07 16:35:20 2010
D-Link DFE-538TX 10/100 Adapter
Index = 1376258
GUID = {FB6051A1-E970-4F46-BB85-F442A194BA3D}
IP = 10.31.10.235/255.255.240.0
MAC = 00:08:a1:65:70:93
GATEWAY = 10.31.0.254/0.0.0.0
'openvpn --show-net' after VPN connection is closed using Process.Kill():
SYSTEM ROUTING TABLE
10.31.0.0 255.255.240.0 10.31.10.235 p=0 i=1376258 t=3 pr=2 a=106 h=0 m=20/-1/-1/-1/-1
10.31.10.235 255.255.255.255 127.0.0.1 p=0 i=1 t=3 pr=2 a=106 h=0 m=20/-1/-1/-1/-1
10.255.255.255 255.255.255.255 10.31.10.235 p=0 i=1376258 t=3 pr=2 a=106 h=0 m=20/-1/-1/-1/-1
127.0.0.0 255.0.0.0 127.0.0.1 p=0 i=1 t=3 pr=2 a=116833 h=0 m=1/-1/-1/-1/-1
208.94.64.10 255.255.255.255 10.31.0.254 p=0 i=1376258 t=4 pr=3 a=21 h=0 m=1/-1/-1/-1/-1
224.0.0.0 240.0.0.0 10.31.10.235 p=0 i=1376258 t=3 pr=2 a=106 h=0 m=20/-1/-1/-1/-1
255.255.255.255 255.255.255.255 10.31.10.235 p=0 i=1376258 t=3 pr=2 a=106 h=0 m=1/-1/-1/-1/-1
255.255.255.255 255.255.255.255 10.31.10.235 p=0 i=1441796 t=3 pr=2 a=84 h=0 m=1/-1/-1/-1/-1
SYSTEM ADAPTER LIST
TAP-Win32 Adapter V8
Index = 1441796
GUID = {013AB57F-DFE6-4FD9-B25E-9589E77DA4EB}
IP = 0.0.0.0/0.0.0.0
MAC = 00:ff:01:3a:b5:7f
GATEWAY =
DHCP SERV = 172.16.0.0
DHCP LEASE OBTAINED = Tue Jul 07 17:02:30 2009
DHCP LEASE EXPIRES = Wed Jul 07 17:02:30 2010
D-Link DFE-538TX 10/100 Adapter
Index = 1376258
GUID = {FB6051A1-E970-4F46-BB85-F442A194BA3D}
IP = 10.31.10.235/255.255.240.0
MAC = 00:08:a1:65:70:93
GATEWAY =
I also tried sending the process WM_CLOSE / WM_QUIT / WM_ENDMESSAGE messages but these did not produce any result.
const int WM_CLOSE = 0x10;
const int WM_QUIT = 0x12;
const int WM_ENDSESSION = 0x0016;
[DllImport("user32.dll")]
public static extern int SendMessage(int hwnd, int msg, int wparam, int lparam);
foreach (var p in Process.GetProcessesByName("openvpn"))
{
SendMessage(p.Handle.ToInt32(), WM_CLOSE, 0, 0);
SendMessage(p.Handle.ToInt32(), WM_QUIT, 0, 0);
SendMessage(p.Handle.ToInt32(), WM_ENDSESSION, 0, 0);
}
Further info on the appropriate solution: See instructions in section titled Using the management interface in Controlling a running OpenVPN process.
More info on using Telnet from C#.
I haven't tried this on Windows but you can use the OpenVPN Management interface to send a SIGTERM signal with the signal command. You'll need to include the management interface configuration entries in your configuration file of course.
More information in the OpenVPN man page
You may want to look at the way OpenVPN-admin is doing things. It's working under Windows and Linux and developed with Mono.
You're sending messages to the process handle - these, however, are window messages, that's why they must be sent to a window handle.
EDIT
As you got the process already, you might try the following:
foreach (var p in Process.GetProcessesByName("openvpn"))
{
p.CloseMainWindow();
}
or
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hwnd, int msg, int wparam, int lparam);
foreach (var p in Process.GetProcessesByName("openvpn"))
{
IntPtr hWnd = p.MainWindowHandle;
// Send message to hWnd (mind SendMessage's changed signature)
}